; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --include-generated-funcs
; RUN: if [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -enzyme -enzyme-lower-globals -mem2reg -sroa -simplifycfg -instsimplify -S | FileCheck %s; fi
; RUN: %opt < %s %newLoadEnzyme -passes="enzyme,function(mem2reg,sroa,%simplifycfg,instsimplify)" -enzyme-lower-globals -S | FileCheck %s

%struct.Gradients = type { double, double, double }

; Function Attrs: nounwind
declare %struct.Gradients @__enzyme_fwddiff(double (double)*, ...)

@global = external dso_local local_unnamed_addr global double, align 8

; Function Attrs: noinline norecurse nounwind readonly uwtable
define double @mulglobal(double %x) {
entry:
  %l1 = load double, double* @global, align 8
  %mul = fmul fast double %l1, %x
  store double %mul, double* @global, align 8
  %l2 = load double, double* @global, align 8
  %mul2 = fmul fast double %l2, %l2
  store double %mul2, double* @global, align 8
  %l3 = load double, double* @global, align 8
  ret double %l3
}

; Function Attrs: noinline nounwind uwtable
define %struct.Gradients @derivative(double %x) {
entry:
  %0 = tail call %struct.Gradients (double (double)*, ...) @__enzyme_fwddiff(double (double)* nonnull @mulglobal, metadata !"enzyme_width", i64 3, double %x, double 1.0, double 2.0, double 3.0)
  ret %struct.Gradients %0
}

; CHECK: define {{[^@]+}}@fwddiffe3mulglobal(double [[X:%.*]], [3 x double] %"x'")
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[GLOBAL_LOCAL_0_COPYLOAD:%.*]] = load double, double* @global, align 8
; CHECK-NEXT:    [[MUL:%.*]] = fmul fast double [[GLOBAL_LOCAL_0_COPYLOAD]], [[X]]
; CHECK-NEXT:    [[TMP3:%.*]] = extractvalue [3 x double] %"x'", 0
; CHECK-NEXT:    [[TMP5:%.*]] = fmul fast double [[TMP3]], [[GLOBAL_LOCAL_0_COPYLOAD]]
; CHECK-NEXT:    [[TMP7:%.*]] = extractvalue [3 x double] %"x'", 1
; CHECK-NEXT:    [[TMP9:%.*]] = fmul fast double [[TMP7]], [[GLOBAL_LOCAL_0_COPYLOAD]]
; CHECK-NEXT:    [[TMP11:%.*]] = extractvalue [3 x double] %"x'", 2
; CHECK-NEXT:    [[TMP13:%.*]] = fmul fast double [[TMP11]], [[GLOBAL_LOCAL_0_COPYLOAD]]
; CHECK-NEXT:    [[MUL2:%.*]] = fmul fast double [[MUL]], [[MUL]]

; CHECK-NEXT:    [[TMP15:%.*]] = fmul fast double [[TMP5]], [[MUL]]
; CHECK-NEXT:    [[TMP19:%.*]] = fmul fast double [[TMP9]], [[MUL]]
; CHECK-NEXT:    [[TMP23:%.*]] = fmul fast double [[TMP13]], [[MUL]]

; CHECK-NEXT:    [[TMP16:%.*]] = fmul fast double [[TMP5]], [[MUL]]
; CHECK-NEXT:    [[TMP20:%.*]] = fmul fast double [[TMP9]], [[MUL]]
; CHECK-NEXT:    [[TMP24:%.*]] = fmul fast double [[TMP13]], [[MUL]]

; CHECK-NEXT:    [[TMP17:%.*]] = fadd fast double [[TMP15]], [[TMP16]]
; CHECK-NEXT:    [[TMP18:%.*]] = insertvalue [3 x double] undef, double [[TMP17]], 0
; CHECK-NEXT:    [[TMP21:%.*]] = fadd fast double [[TMP19]], [[TMP20]]
; CHECK-NEXT:    [[TMP22:%.*]] = insertvalue [3 x double] [[TMP18]], double [[TMP21]], 1
; CHECK-NEXT:    [[TMP25:%.*]] = fadd fast double [[TMP23]], [[TMP24]]
; CHECK-NEXT:    [[TMP26:%.*]] = insertvalue [3 x double] [[TMP22]], double [[TMP25]], 2
; CHECK-NEXT:    store double [[MUL2]], double* @global, align 8
; CHECK-NEXT:    ret [3 x double] [[TMP26]]
;
