; 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-preopt=false -mem2reg -instsimplify -simplifycfg -S | FileCheck %s; fi
; RUN: %opt < %s %newLoadEnzyme -passes="enzyme,function(mem2reg,instsimplify,%simplifycfg)" -enzyme-preopt=false -S | FileCheck %s

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

@global = external dso_local local_unnamed_addr global double, align 8, !enzyme_shadow !{[3 x double]* @dglobal }
@dglobal = external dso_local local_unnamed_addr global [3 x double], align 8

declare dso_local %struct.Gradients @_Z22__enzyme_fwddiffPFddEz(double (double)*, ...)

define dso_local double @_Z9mulglobald(double %x) {
entry:
  %0 = load double, double* @global, align 8
  %mul = fmul double %0, %x
  ret double %mul
}

define dso_local void @_Z10derivatived(double %x) {
entry:
  call %struct.Gradients (double (double)*, ...) @_Z22__enzyme_fwddiffPFddEz(double (double)* nonnull @_Z9mulglobald, metadata !"enzyme_width", i64 3, double %x, double 1.0 , double 2.0, double 3.0)
  ret void
}

; CHECK: define {{[^@]+}}@fwddiffe3_Z9mulglobald(double [[X:%.*]], [3 x double] %"x'")
; CHECK-NEXT:  entry:
; CHECK-NEXT:    %"'ipl" = load double, double* getelementptr inbounds ([3 x double], [3 x double]* @dglobal, i32 0, i32 0), align 8
; CHECK-NEXT:    %"'ipl1" = load double, double* getelementptr inbounds ([3 x double], [3 x double]* @dglobal, i32 0, i32 1), align 8
; CHECK-NEXT:    %"'ipl2" = load double, double* getelementptr inbounds ([3 x double], [3 x double]* @dglobal, i32 0, i32 2), align 8
; CHECK-NEXT:    [[TMP0:%.*]] = load double, double* @global, align 8
; CHECK-NEXT:    [[TMP2:%.*]] = fmul fast double %"'ipl", [[X]]
; CHECK-NEXT:    [[TMP7:%.*]] = fmul fast double %"'ipl1", [[X]]
; CHECK-NEXT:    [[TMP12:%.*]] = fmul fast double %"'ipl2", [[X]]
; CHECK-NEXT:    [[TMP1:%.*]] = extractvalue [3 x double] %"x'", 0
; CHECK-NEXT:    [[TMP3:%.*]] = fmul fast double [[TMP1]], [[TMP0]]
; CHECK-NEXT:    [[TMP6:%.*]] = extractvalue [3 x double] %"x'", 1
; CHECK-NEXT:    [[TMP8:%.*]] = fmul fast double [[TMP6]], [[TMP0]]
; CHECK-NEXT:    [[TMP11:%.*]] = extractvalue [3 x double] %"x'", 2
; CHECK-NEXT:    [[TMP13:%.*]] = fmul fast double [[TMP11]], [[TMP0]]
; CHECK-NEXT:    [[TMP4:%.*]] = fadd fast double [[TMP2]], [[TMP3]]
; CHECK-NEXT:    [[TMP5:%.*]] = insertvalue [3 x double] undef, double [[TMP4]], 0
; CHECK-NEXT:    [[TMP9:%.*]] = fadd fast double [[TMP7]], [[TMP8]]
; CHECK-NEXT:    [[TMP10:%.*]] = insertvalue [3 x double] [[TMP5]], double [[TMP9]], 1
; CHECK-NEXT:    [[TMP14:%.*]] = fadd fast double [[TMP12]], [[TMP13]]
; CHECK-NEXT:    [[TMP15:%.*]] = insertvalue [3 x double] [[TMP10]], double [[TMP14]], 2
; CHECK-NEXT:    ret [3 x double] [[TMP15]]
;
