; LLVM IR from KhawajaAbaid/machine-learning-in-c :: mlp (train_step)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.metrics = type { double, double }
%struct.mlp = type { ptr, i64 }
%struct.layer = type { ptr, ptr, i64, i64 }
%struct.grad_and_metrics = type { ptr, ptr }

; Function Attrs: nounwind uwtable
declare dso_local ptr @backprop(ptr noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare ptr @copy_mlp(ptr noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @train_step(ptr noundef %0, ptr noundef %1, ptr noundef %2, double noundef %3, i64 noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca double, align 8
  %10 = alloca i64, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca i64, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !5
  store double %3, ptr %9, align 8, !tbaa !9
  store i64 %4, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %17 = call noalias ptr @malloc(i64 noundef 16) #6
  store ptr %17, ptr %11, align 8, !tbaa !5
  %18 = load ptr, ptr %11, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.metrics, ptr %18, i32 0, i32 0
  store double 0.000000e+00, ptr %19, align 8, !tbaa !13
  %20 = load ptr, ptr %11, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.metrics, ptr %20, i32 0, i32 1
  store double 0.000000e+00, ptr %21, align 8, !tbaa !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.mlp, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = getelementptr inbounds ptr, ptr %24, i64 0
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.layer, ptr %26, i32 0, i32 2
  %28 = load i64, ptr %27, align 8, !tbaa !18
  store i64 %28, ptr %12, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.mlp, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mlp, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !20
  %35 = sub i64 %34, 1
  %36 = getelementptr inbounds ptr, ptr %31, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.layer, ptr %37, i32 0, i32 3
  %39 = load i64, ptr %38, align 8, !tbaa !21
  store i64 %39, ptr %13, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = call ptr @copy_mlp(ptr noundef %40)
  store ptr %41, ptr %14, align 8, !tbaa !5
  %42 = load ptr, ptr %14, align 8, !tbaa !5
  call void @initialize_mlp_zeros_(ptr noundef %42)
  %43 = load ptr, ptr %14, align 8, !tbaa !5
  %44 = load i64, ptr %10, align 8, !tbaa !11
  call void @divide_grad_by_batch_size_(ptr noundef %43, i64 noundef %44)
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #5
  store i64 0, ptr %16, align 8, !tbaa !11
  br label %45

45:                                               ; preds = %86, %5
  %46 = load i64, ptr %16, align 8, !tbaa !11
  %47 = load i64, ptr %10, align 8, !tbaa !11
  %48 = icmp ult i64 %46, %47
  br i1 %48, label %50, label %49

49:                                               ; preds = %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #5
  br label %89

50:                                               ; preds = %45
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = load ptr, ptr %7, align 8, !tbaa !5
  %53 = load i64, ptr %16, align 8, !tbaa !11
  %54 = load i64, ptr %12, align 8, !tbaa !11
  %55 = mul i64 %53, %54
  %56 = getelementptr inbounds double, ptr %52, i64 %55
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = load i64, ptr %16, align 8, !tbaa !11
  %59 = load i64, ptr %13, align 8, !tbaa !11
  %60 = mul i64 %58, %59
  %61 = getelementptr inbounds double, ptr %57, i64 %60
  %62 = call ptr @backprop(ptr noundef %51, ptr noundef %56, ptr noundef %61)
  store ptr %62, ptr %15, align 8, !tbaa !5
  %63 = load ptr, ptr %14, align 8, !tbaa !5
  %64 = load ptr, ptr %15, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.grad_and_metrics, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !22
  call void @accumulate_grad_(ptr noundef %63, ptr noundef %66)
  %67 = load ptr, ptr %15, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.grad_and_metrics, ptr %67, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !24
  %70 = getelementptr inbounds %struct.metrics, ptr %69, i32 0, i32 0
  %71 = load double, ptr %70, align 8, !tbaa !13
  %72 = load ptr, ptr %11, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.metrics, ptr %72, i32 0, i32 0
  %74 = load double, ptr %73, align 8, !tbaa !13
  %75 = fadd double %74, %71
  store double %75, ptr %73, align 8, !tbaa !13
  %76 = load ptr, ptr %15, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.grad_and_metrics, ptr %76, i32 0, i32 1
  %78 = load ptr, ptr %77, align 8, !tbaa !24
  %79 = getelementptr inbounds %struct.metrics, ptr %78, i32 0, i32 1
  %80 = load double, ptr %79, align 8, !tbaa !15
  %81 = load ptr, ptr %11, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.metrics, ptr %81, i32 0, i32 1
  %83 = load double, ptr %82, align 8, !tbaa !15
  %84 = fadd double %83, %80
  store double %84, ptr %82, align 8, !tbaa !15
  %85 = load ptr, ptr %15, align 8, !tbaa !5
  call void @free_grad_and_metrics(ptr noundef %85)
  br label %86

86:                                               ; preds = %50
  %87 = load i64, ptr %16, align 8, !tbaa !11
  %88 = add i64 %87, 1
  store i64 %88, ptr %16, align 8, !tbaa !11
  br label %45, !llvm.loop !25

89:                                               ; preds = %49
  %90 = load i64, ptr %10, align 8, !tbaa !11
  %91 = uitofp i64 %90 to double
  %92 = load ptr, ptr %11, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.metrics, ptr %92, i32 0, i32 0
  %94 = load double, ptr %93, align 8, !tbaa !13
  %95 = fdiv double %94, %91
  store double %95, ptr %93, align 8, !tbaa !13
  %96 = load i64, ptr %10, align 8, !tbaa !11
  %97 = uitofp i64 %96 to double
  %98 = load ptr, ptr %11, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.metrics, ptr %98, i32 0, i32 1
  %100 = load double, ptr %99, align 8, !tbaa !15
  %101 = fdiv double %100, %97
  store double %101, ptr %99, align 8, !tbaa !15
  %102 = load ptr, ptr %6, align 8, !tbaa !5
  %103 = load ptr, ptr %14, align 8, !tbaa !5
  %104 = load double, ptr %9, align 8, !tbaa !9
  call void @update_weights_(ptr noundef %102, ptr noundef %103, double noundef %104)
  %105 = load ptr, ptr %14, align 8, !tbaa !5
  call void @free_mlp(ptr noundef %105)
  %106 = load ptr, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  ret ptr %106
}

declare void @initialize_mlp_zeros_(ptr noundef) #3

declare void @divide_grad_by_batch_size_(ptr noundef, i64 noundef) #3

declare void @accumulate_grad_(ptr noundef, ptr noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_grad_and_metrics(ptr noundef) #4

declare void @update_weights_(ptr noundef, ptr noundef, double noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_mlp(ptr noundef) #4

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(0) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !10, i64 0}
!14 = !{!"metrics", !10, i64 0, !10, i64 8}
!15 = !{!14, !10, i64 8}
!16 = !{!17, !6, i64 0}
!17 = !{!"", !6, i64 0, !12, i64 8}
!18 = !{!19, !12, i64 16}
!19 = !{!"", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 24}
!20 = !{!17, !12, i64 8}
!21 = !{!19, !12, i64 24}
!22 = !{!23, !6, i64 0}
!23 = !{!"", !6, i64 0, !6, i64 8}
!24 = !{!23, !6, i64 8}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
declare dso_local ptr @backprop(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

declare ptr @copy_mlp(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @train_step(ptr noundef %0, ptr noundef %1, ptr noundef %2, double noundef %3, i64 noundef %4) local_unnamed_addr #0 {
  %6 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #4
  %7 = load ptr, ptr %0, align 8, !tbaa !5
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %8, i64 16
  %10 = load i64, ptr %9, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load i64, ptr %11, align 8, !tbaa !14
  %13 = getelementptr ptr, ptr %7, i64 %12
  %14 = getelementptr i8, ptr %13, i64 -8
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %15, i64 24
  %17 = load i64, ptr %16, align 8, !tbaa !15
  %18 = tail call ptr @copy_mlp(ptr noundef nonnull %0) #5
  tail call void @initialize_mlp_zeros_(ptr noundef %18) #5
  tail call void @divide_grad_by_batch_size_(ptr noundef %18, i64 noundef %4) #5
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %5
  %19 = phi <2 x double> [ zeroinitializer, %5 ], [ %34, %.lr.ph ]
  %20 = uitofp i64 %4 to double
  %21 = insertelement <2 x double> poison, double %20, i64 0
  %22 = shufflevector <2 x double> %21, <2 x double> poison, <2 x i32> zeroinitializer
  %23 = fdiv <2 x double> %19, %22
  store <2 x double> %23, ptr %6, align 8, !tbaa !16
  tail call void @update_weights_(ptr noundef nonnull %0, ptr noundef %18, double noundef %3) #5
  tail call void @free_mlp(ptr noundef %18)
  ret ptr %6

.lr.ph:                                           ; preds = %5, %.lr.ph
  %.035 = phi i64 [ %35, %.lr.ph ], [ 0, %5 ]
  %24 = phi <2 x double> [ %34, %.lr.ph ], [ zeroinitializer, %5 ]
  %25 = mul i64 %.035, %10
  %26 = getelementptr inbounds double, ptr %1, i64 %25
  %27 = mul i64 %.035, %17
  %28 = getelementptr inbounds double, ptr %2, i64 %27
  %29 = tail call ptr @backprop(ptr noundef nonnull %0, ptr noundef %26, ptr noundef %28)
  %30 = load ptr, ptr %29, align 8, !tbaa !18
  tail call void @accumulate_grad_(ptr noundef %18, ptr noundef %30) #5
  %31 = getelementptr inbounds i8, ptr %29, i64 8
  %32 = load ptr, ptr %31, align 8, !tbaa !20
  %33 = load <2 x double>, ptr %32, align 8, !tbaa !16
  %34 = fadd <2 x double> %33, %24
  tail call void @free_grad_and_metrics(ptr noundef nonnull %29)
  %35 = add nuw i64 %.035, 1
  %exitcond.not = icmp eq i64 %35, %4
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !21
}

declare void @initialize_mlp_zeros_(ptr noundef) local_unnamed_addr #2

declare void @divide_grad_by_batch_size_(ptr noundef, i64 noundef) local_unnamed_addr #2

declare void @accumulate_grad_(ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_grad_and_metrics(ptr noundef) local_unnamed_addr #3

declare void @update_weights_(ptr noundef, ptr noundef, double noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @free_mlp(ptr noundef) local_unnamed_addr #3

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { inlinehint nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { nounwind allocsize(0) }
attributes #5 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !7, i64 0}
!6 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 16}
!13 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!14 = !{!6, !10, i64 8}
!15 = !{!13, !10, i64 24}
!16 = !{!17, !17, i64 0}
!17 = !{!"double", !8, i64 0}
!18 = !{!19, !7, i64 0}
!19 = !{!"", !7, i64 0, !7, i64 8}
!20 = !{!19, !7, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass