; 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 }

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

@global = private unnamed_addr constant [1 x void (double*)*] [void (double*)* @ipmul]

@.str = private unnamed_addr constant [6 x i8] c"x=%f\0A\00", align 1
@.str.1 = private unnamed_addr constant [7 x i8] c"xp=%f\0A\00", align 1

define void @ipmul(double* %x) {
entry:
  %0 = load double, double* %x, !tbaa !2
  %mul = fmul fast double %0, %0
  store double %mul, double* %x
  ret void
}

; Function Attrs: noinline norecurse nounwind readonly uwtable
define dso_local double @mulglobal(double %x, i64 %idx) #0 {
entry:
  %alloc = alloca double
  store double %x, double* %alloc
  %arrayidx = getelementptr inbounds [1 x void (double*)*], [1 x void (double*)*]* @global, i64 0, i64 %idx
  %fp = load void (double*)*, void (double*)** %arrayidx
  call void %fp(double* %alloc)
  %ret = load double, double* %alloc, !tbaa !2
  ret double %ret
}

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

; Function Attrs: nounwind uwtable
define dso_local void @main(i32 %argc, i8** nocapture readonly %argv) local_unnamed_addr #3 {
entry:
  %arrayidx = getelementptr inbounds i8*, i8** %argv, i64 1
  %0 = load i8*, i8** %arrayidx, !tbaa !6
  %call.i = tail call fast double @strtod(i8* nocapture nonnull %0, i8** null) #2
  %call1 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), double %call.i)
  %call2 = tail call %struct.Gradients @derivative(double %call.i)
  %1 = extractvalue %struct.Gradients %call2, 0
  %call3 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), double %1)
  ret void
}

; Function Attrs: nounwind
declare dso_local i32 @printf(i8* nocapture readonly, ...) local_unnamed_addr #4

; Function Attrs: nounwind
declare dso_local double @strtod(i8* readonly, i8** nocapture) local_unnamed_addr #4

attributes #0 = { noinline norecurse nounwind readonly uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="true" "no-jump-tables"="false" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="true" "use-soft-float"="false" }
attributes #1 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="true" "no-jump-tables"="false" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="true" "use-soft-float"="false" }
attributes #2 = { nounwind }
attributes #3 = { nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="true" "no-jump-tables"="false" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="true" "use-soft-float"="false" }
attributes #4 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="true" "use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 7.1.0 "}
!2 = !{!3, !3, i64 0}
!3 = !{!"double", !4, i64 0}
!4 = !{!"omnipotent char", !5, i64 0}
!5 = !{!"Simple C/C++ TBAA"}
!6 = !{!7, !7, i64 0}
!7 = !{!"any pointer", !4, i64 0}


; CHECK: define {{[^@]+}}@fwddiffe3mulglobal(double [[X:%.*]], [3 x double] %"x'", i64 [[IDX:%.*]]) 
; CHECK-NEXT:  entry:
; CHECK-NEXT:    %"alloc'ipa" = alloca double
; CHECK-NEXT:    [[TMP0:%.*]] = insertvalue [3 x double*] undef, double* %"alloc'ipa", 0
; CHECK-NEXT:    %"alloc'ipa1" = alloca double
; CHECK-NEXT:    [[TMP1:%.*]] = insertvalue [3 x double*] [[TMP0]], double* %"alloc'ipa1", 1
; CHECK-NEXT:    %"alloc'ipa2" = alloca double
; CHECK-NEXT:    [[TMP2:%.*]] = insertvalue [3 x double*] [[TMP1]], double* %"alloc'ipa2", 2
; CHECK-NEXT:    store double 0.000000e+00, double* %"alloc'ipa"
; CHECK-NEXT:    store double 0.000000e+00, double* %"alloc'ipa1"
; CHECK-NEXT:    store double 0.000000e+00, double* %"alloc'ipa2"
; CHECK-NEXT:    [[ALLOC:%.*]] = alloca double
; CHECK-NEXT:    [[TMP3:%.*]] = extractvalue [3 x double] %"x'", 0
; CHECK-NEXT:    store double [[TMP3]], double* %"alloc'ipa"
; CHECK-NEXT:    [[TMP4:%.*]] = extractvalue [3 x double] %"x'", 1
; CHECK-NEXT:    store double [[TMP4]], double* %"alloc'ipa1"
; CHECK-NEXT:    [[TMP5:%.*]] = extractvalue [3 x double] %"x'", 2
; CHECK-NEXT:    store double [[TMP5]], double* %"alloc'ipa2"
; CHECK-NEXT:    store double [[X]], double* [[ALLOC]]
; CHECK-NEXT:    %"arrayidx'ipg" = getelementptr inbounds [1 x void (double*)*], [1 x void (double*)*]* @global_shadow, i64 0, i64 [[IDX]]
; CHECK-NEXT:    %arrayidx = getelementptr inbounds [1 x void (double*)*], [1 x void (double*)*]* @global, i64 0, i64 %idx
; CHECK-NEXT:    %"fp'ipl" = load void (double*)*, void (double*)** %"arrayidx'ipg"
; CHECK-NEXT:   %fp = load void (double*)*, void (double*)** %arrayidx
; CHECK-NEXT:   %6 = bitcast void (double*)* %fp to i8*
; CHECK-NEXT:   %7 = bitcast void (double*)* %"fp'ipl" to i8*
; CHECK-NEXT:   %8 = icmp eq i8* %6, %7
; CHECK-NEXT:   br i1 %8, label %error.i, label %__enzyme_runtimeinactiveerr.exit

; CHECK: error.i:                                          ; preds = %entry
; CHECK-NEXT:   %{{.*}} = call i32 @puts(i8* getelementptr inbounds ([79 x i8], [79 x i8]* @.str.3, i32 0, i32 0))
; CHECK-NEXT:   call void @exit(i32 1)
; CHECK-NEXT:   unreachable

; CHECK: __enzyme_runtimeinactiveerr.exit:                 ; preds = %entry
; CHECK-NEXT:    [[TMP6:%.*]] = bitcast void (double*)* %"fp'ipl" to void (double*, [3 x double*])**
; CHECK-NEXT:    [[TMP7:%.*]] = load void (double*, [3 x double*])*, void (double*, [3 x double*])** [[TMP6]]
; CHECK-NEXT:    call void [[TMP7]](double* [[ALLOC]], [3 x double*] [[TMP2]])
; CHECK-NEXT:    %"ret'ipl" = load double, double* %"alloc'ipa"
; CHECK-NEXT:    [[TMP8:%.*]] = insertvalue [3 x double] undef, double %"ret'ipl", 0
; CHECK-NEXT:    %"ret'ipl7" = load double, double* %"alloc'ipa1"
; CHECK-NEXT:    [[TMP9:%.*]] = insertvalue [3 x double] [[TMP8]], double %"ret'ipl7", 1
; CHECK-NEXT:    %"ret'ipl8" = load double, double* %"alloc'ipa2"
; CHECK-NEXT:    [[TMP10:%.*]] = insertvalue [3 x double] [[TMP9]], double %"ret'ipl8", 2
; CHECK-NEXT:    ret [3 x double] [[TMP10]]
;
;
; CHECK: define {{[^@]+}}@fwddiffe3ipmul(double* [[X:%.*]], [3 x double*] %"x'")
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = extractvalue [3 x double*] %"x'", 0
; CHECK-NEXT:    %"'ipl" = load double, double* [[TMP0]]
; CHECK-NEXT:    [[TMP1:%.*]] = extractvalue [3 x double*] %"x'", 1
; CHECK-NEXT:    %"'ipl1" = load double, double* [[TMP1]]
; CHECK-NEXT:    [[TMP2:%.*]] = extractvalue [3 x double*] %"x'", 2
; CHECK-NEXT:    %"'ipl2" = load double, double* [[TMP2]]
; CHECK-NEXT:    [[TMP3:%.*]] = load double, double* [[X]]
; CHECK-NEXT:    [[MUL:%.*]] = fmul fast double [[TMP3]], [[TMP3]]
; CHECK-NEXT:    [[TMP4:%.*]] = fmul fast double %"'ipl", [[TMP3]]
; CHECK-NEXT:    [[TMP7:%.*]] = fmul fast double %"'ipl1", [[TMP3]]
; CHECK-NEXT:    [[TMP10:%.*]] = fmul fast double %"'ipl2", [[TMP3]]
; CHECK-NEXT:    [[TMP5:%.*]] = fmul fast double %"'ipl", [[TMP3]]
; CHECK-NEXT:    [[TMP8:%.*]] = fmul fast double %"'ipl1", [[TMP3]]
; CHECK-NEXT:    [[TMP11:%.*]] = fmul fast double %"'ipl2", [[TMP3]]
; CHECK-NEXT:    [[TMP6:%.*]] = fadd fast double [[TMP4]], [[TMP5]]
; CHECK-NEXT:    [[TMP9:%.*]] = fadd fast double [[TMP7]], [[TMP8]]
; CHECK-NEXT:    [[TMP12:%.*]] = fadd fast double [[TMP10]], [[TMP11]]
; CHECK-NEXT:    [[TMP13:%.*]] = extractvalue [3 x double*] %"x'", 0
; CHECK-NEXT:    store double [[TMP6]], double* [[TMP13]]
; CHECK-NEXT:    [[TMP14:%.*]] = extractvalue [3 x double*] %"x'", 1
; CHECK-NEXT:    store double [[TMP9]], double* [[TMP14]]
; CHECK-NEXT:    [[TMP15:%.*]] = extractvalue [3 x double*] %"x'", 2
; CHECK-NEXT:    store double [[TMP12]], double* [[TMP15]]
; CHECK-NEXT:    store double [[MUL]], double* [[X]]
; CHECK-NEXT:    ret void
;
