; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --include-generated-funcs
; RUN: if [ %llvmver -ge 12 ] && [ %llvmver -lt 16 ]; then %opt < %s %loadEnzyme -enzyme -enzyme-preopt=false -mem2reg -simplifycfg -early-cse -adce -S | FileCheck %s ; fi
; RUN: if [ %llvmver -ge 12 ]; then %opt < %s %newLoadEnzyme -passes="enzyme,function(mem2reg,%simplifycfg,early-cse,adce)" -enzyme-preopt=false -S | FileCheck %s; fi

; #include <stdio.h>
; #include <array>

; using namespace std;

; extern int enzyme_width;

; struct Gradients {
;     array<double,3> dx1, dx2, dx3;
; };

; extern Gradients __enzyme_fwddiff(void*, ...);

; array<double,3> square(double x) {
;     return {x * x, x * x * x, x};
; }
; Gradients dsquare(double x) {
;     // This returns the derivative of square or 2 * x
;     return __enzyme_fwddiff((void*)square, enzyme_width, 3, x, 1.0, 2.0, 3.0);
; }
; int main() {
;     printf("%f \n", dsquare(3).dx1[0]);
; }


%"struct.std::array" = type { [3 x double] }
%struct.Gradients = type { %"struct.std::array", %"struct.std::array", %"struct.std::array" }

$_ZNSt5arrayIdLm3EEixEm = comdat any

$_ZNSt14__array_traitsIdLm3EE6_S_refERA3_Kdm = comdat any

@enzyme_width = external dso_local local_unnamed_addr global i32, align 4
@.str = private unnamed_addr constant [5 x i8] c"%f \0A\00", align 1

define dso_local void @_Z6squared(%"struct.std::array"* noalias nocapture sret(%"struct.std::array") align 8 %agg.result, double %x) #0 {
entry:
  %arrayinit.begin = getelementptr inbounds %"struct.std::array", %"struct.std::array"* %agg.result, i64 0, i32 0, i64 0
  %mul = fmul double %x, %x
  store double %mul, double* %arrayinit.begin, align 8
  %arrayinit.element = getelementptr inbounds %"struct.std::array", %"struct.std::array"* %agg.result, i64 0, i32 0, i64 1
  %mul2 = fmul double %mul, %x
  store double %mul2, double* %arrayinit.element, align 8
  %arrayinit.element3 = getelementptr inbounds %"struct.std::array", %"struct.std::array"* %agg.result, i64 0, i32 0, i64 2
  store double %x, double* %arrayinit.element3, align 8
  ret void
}

define dso_local void @_Z7dsquared(%struct.Gradients* noalias sret(%struct.Gradients) align 8 %agg.result, double %x) local_unnamed_addr #1 {
entry:
  %0 = load i32, i32* @enzyme_width, align 4
  call void (%struct.Gradients*, i8*, ...) @_Z16__enzyme_fwddiffPvz(%struct.Gradients* sret(%struct.Gradients) align 8 %agg.result, i8* bitcast (void (%"struct.std::array"*, double)* @_Z6squared to i8*), i32 %0, i32 3, double %x, double 1.000000e+00, double 2.000000e+00, double 3.000000e+00)
  ret void
}

declare dso_local void @_Z16__enzyme_fwddiffPvz(%struct.Gradients* sret(%struct.Gradients) align 8, i8*, ...) local_unnamed_addr #2

define dso_local i32 @main() local_unnamed_addr #3 {
entry:
  %ref.tmp = alloca %struct.Gradients, align 8
  %0 = bitcast %struct.Gradients* %ref.tmp to i8*
  call void @llvm.lifetime.start.p0i8(i64 72, i8* nonnull %0) #7
  call void @_Z7dsquared(%struct.Gradients* nonnull sret(%struct.Gradients) align 8 %ref.tmp, double 3.000000e+00)
  %dx1 = getelementptr inbounds %struct.Gradients, %struct.Gradients* %ref.tmp, i64 0, i32 0
  %call = call nonnull align 8 dereferenceable(8) double* @_ZNSt5arrayIdLm3EEixEm(%"struct.std::array"* nonnull dereferenceable(24) %dx1, i64 0) #7
  %1 = load double, double* %call, align 8
  %call1 = call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), double %1)
  call void @llvm.lifetime.end.p0i8(i64 72, i8* nonnull %0) #7
  ret i32 0
}

declare dso_local noundef i32 @printf(i8* nocapture noundef readonly, ...) local_unnamed_addr #4

declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #5

define linkonce_odr dso_local nonnull align 8 dereferenceable(8) double* @_ZNSt5arrayIdLm3EEixEm(%"struct.std::array"* nonnull dereferenceable(24) %this, i64 %__n) local_unnamed_addr #6 comdat align 2 {
entry:
  %_M_elems = getelementptr inbounds %"struct.std::array", %"struct.std::array"* %this, i64 0, i32 0
  %call = call nonnull align 8 dereferenceable(8) double* @_ZNSt14__array_traitsIdLm3EE6_S_refERA3_Kdm([3 x double]* nonnull align 8 dereferenceable(24) %_M_elems, i64 %__n) #7
  ret double* %call
}

declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #5

define linkonce_odr dso_local nonnull align 8 dereferenceable(8) double* @_ZNSt14__array_traitsIdLm3EE6_S_refERA3_Kdm([3 x double]* nonnull align 8 dereferenceable(24) %__t, i64 %__n) local_unnamed_addr #6 comdat align 2 {
entry:
  %arrayidx = getelementptr inbounds [3 x double], [3 x double]* %__t, i64 0, i64 %__n
  ret double* %arrayidx
}

attributes #0 = { nofree norecurse nounwind uwtable willreturn writeonly mustprogress "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { uwtable mustprogress "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #2 = { "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #3 = { norecurse uwtable mustprogress "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #4 = { nofree nounwind "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #5 = { argmemonly nofree nosync nounwind willreturn }
attributes #6 = { nounwind uwtable willreturn mustprogress "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #7 = { nounwind }

; CHECK: define {{[^@]+}}@fwddiffe3_Z6squared(%"struct.std::array"* noalias nocapture align 8 "enzyme_sret" [[AGG_RESULT:%.*]], [3 x %"struct.std::array"*] "enzyme_sret_v" %"agg.result'", double [[X:%.*]], [3 x double] %"x'") 
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TMP0:%.*]] = extractvalue [3 x %"struct.std::array"*] %"agg.result'", 0
; CHECK-NEXT:    %"arrayinit.begin'ipg" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP0]], i64 0, i32 0, i64 0
; CHECK-NEXT:    [[TMP2:%.*]] = extractvalue [3 x %"struct.std::array"*] %"agg.result'", 1
; CHECK-NEXT:    %"arrayinit.begin'ipg1" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP2]], i64 0, i32 0, i64 0
; CHECK-NEXT:    [[TMP4:%.*]] = extractvalue [3 x %"struct.std::array"*] %"agg.result'", 2
; CHECK-NEXT:    %"arrayinit.begin'ipg2" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP4]], i64 0, i32 0, i64 0
; CHECK-NEXT:    [[MUL:%.*]] = fmul double [[X]], [[X]]
; CHECK-NEXT:    [[TMP5:%.*]] = extractvalue [3 x double] %"x'", 0
; CHECK-NEXT:    [[TMP6:%.*]] = fmul fast double [[TMP5]], [[X]]

; CHECK-NEXT:    [[TMP9:%.*]] = extractvalue [3 x double] %"x'", 1
; CHECK-NEXT:    [[TMP10:%.*]] = fmul fast double [[TMP9]], [[X]]

; CHECK-NEXT:    [[TMP13:%.*]] = extractvalue [3 x double] %"x'", 2
; CHECK-NEXT:    [[TMP14:%.*]] = fmul fast double [[TMP13]], [[X]]

; CHECK-NEXT:    [[TMP7:%.*]] = fadd fast double [[TMP6]], [[TMP6]]
; CHECK-NEXT:    [[TMP11:%.*]] = fadd fast double [[TMP10]], [[TMP10]]
; CHECK-NEXT:    [[TMP15:%.*]] = fadd fast double [[TMP14]], [[TMP14]]

; CHECK-NEXT:    store double [[TMP7]], double* %"arrayinit.begin'ipg", align 8
; CHECK-NEXT:    store double [[TMP11]], double* %"arrayinit.begin'ipg1", align 8
; CHECK-NEXT:    store double [[TMP15]], double* %"arrayinit.begin'ipg2", align 8
; CHECK-NEXT:    %"arrayinit.element'ipg" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP0]], i64 0, i32 0, i64 1
; CHECK-NEXT:    %"arrayinit.element'ipg3" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP2]], i64 0, i32 0, i64 1
; CHECK-NEXT:    %"arrayinit.element'ipg4" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP4]], i64 0, i32 0, i64 1
; CHECK-NEXT:    [[TMP18:%.*]] = fmul fast double [[TMP7]], [[X]]
; CHECK-NEXT:    [[TMP22:%.*]] = fmul fast double [[TMP11]], [[X]]
; CHECK-NEXT:    [[TMP26:%.*]] = fmul fast double [[TMP15]], [[X]]

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

; CHECK-NEXT:    [[TMP20:%.*]] = fadd fast double [[TMP18]], [[TMP19]]
; CHECK-NEXT:    [[TMP24:%.*]] = fadd fast double [[TMP22]], [[TMP23]]
; CHECK-NEXT:    [[TMP28:%.*]] = fadd fast double [[TMP26]], [[TMP27]]
; CHECK-NEXT:    store double [[TMP20]], double* %"arrayinit.element'ipg", align 8
; CHECK-NEXT:    store double [[TMP24]], double* %"arrayinit.element'ipg3", align 8
; CHECK-NEXT:    store double [[TMP28]], double* %"arrayinit.element'ipg4", align 8
; CHECK-NEXT:    %"arrayinit.element3'ipg" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP0]], i64 0, i32 0, i64 2
; CHECK-NEXT:    %"arrayinit.element3'ipg5" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP2]], i64 0, i32 0, i64 2
; CHECK-NEXT:    %"arrayinit.element3'ipg6" = getelementptr inbounds %"struct.std::array", %"struct.std::array"* [[TMP4]], i64 0, i32 0, i64 2
; CHECK-NEXT:    store double [[TMP5]], double* %"arrayinit.element3'ipg", align 8
; CHECK-NEXT:    store double [[TMP9]], double* %"arrayinit.element3'ipg5", align 8
; CHECK-NEXT:    store double [[TMP13]], double* %"arrayinit.element3'ipg6", align 8
; CHECK-NEXT:    ret void
;
