; LLVM IR from KhawajaAbaid/machine-learning-in-c :: logistic_regression (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.model = type { ptr, double, i64, i64 }
%struct.grad_and_metrics = type { ptr, ptr }

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

; 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) #2 {
  %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 ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  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) #6
  %15 = call noalias ptr @malloc(i64 noundef 16) #7
  store ptr %15, ptr %11, align 8, !tbaa !5
  %16 = load ptr, ptr %11, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.metrics, ptr %16, i32 0, i32 1
  store double 0.000000e+00, ptr %17, align 8, !tbaa !13
  %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 !15
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %20 = call noalias ptr @malloc(i64 noundef 32) #7
  store ptr %20, ptr %12, align 8, !tbaa !5
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.model, ptr %21, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !16
  %24 = load ptr, ptr %12, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.model, ptr %24, i32 0, i32 2
  store i64 %23, ptr %25, align 8, !tbaa !16
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.model, ptr %26, i32 0, i32 3
  %28 = load i64, ptr %27, align 8, !tbaa !18
  %29 = load ptr, ptr %12, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.model, ptr %29, i32 0, i32 3
  store i64 %28, ptr %30, align 8, !tbaa !18
  %31 = load ptr, ptr %12, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.model, ptr %31, i32 0, i32 2
  %33 = load i64, ptr %32, align 8, !tbaa !16
  %34 = call noalias ptr @calloc(i64 noundef %33, i64 noundef 8) #8
  %35 = load ptr, ptr %12, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.model, ptr %35, i32 0, i32 0
  store ptr %34, ptr %36, align 8, !tbaa !19
  %37 = load ptr, ptr %12, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.model, ptr %37, i32 0, i32 1
  store double 0.000000e+00, ptr %38, align 8, !tbaa !20
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #6
  store i32 0, ptr %14, align 4, !tbaa !21
  br label %39

39:                                               ; preds = %89, %5
  %40 = load i32, ptr %14, align 4, !tbaa !21
  %41 = sext i32 %40 to i64
  %42 = load i64, ptr %10, align 8, !tbaa !11
  %43 = icmp ult i64 %41, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %39
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #6
  br label %92

45:                                               ; preds = %39
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = load i32, ptr %14, align 4, !tbaa !21
  %49 = sext i32 %48 to i64
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.model, ptr %50, i32 0, i32 2
  %52 = load i64, ptr %51, align 8, !tbaa !16
  %53 = mul i64 %49, %52
  %54 = getelementptr inbounds double, ptr %47, i64 %53
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = load i32, ptr %14, align 4, !tbaa !21
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds double, ptr %55, i64 %57
  %59 = load double, ptr %58, align 8, !tbaa !9
  %60 = call ptr @backprop(ptr noundef %46, ptr noundef %54, double noundef %59)
  store ptr %60, ptr %13, align 8, !tbaa !5
  %61 = load ptr, ptr %12, align 8, !tbaa !5
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.grad_and_metrics, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !23
  call void @accumulate_gradient_(ptr noundef %61, ptr noundef %64)
  %65 = load ptr, ptr %13, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.grad_and_metrics, ptr %65, i32 0, i32 0
  %67 = load ptr, ptr %66, align 8, !tbaa !23
  call void @free_gradient(ptr noundef %67)
  %68 = load ptr, ptr %13, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.grad_and_metrics, ptr %68, i32 0, i32 1
  %70 = load ptr, ptr %69, align 8, !tbaa !25
  %71 = getelementptr inbounds %struct.metrics, ptr %70, i32 0, i32 1
  %72 = load double, ptr %71, align 8, !tbaa !13
  %73 = load ptr, ptr %11, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.metrics, ptr %73, i32 0, i32 1
  %75 = load double, ptr %74, align 8, !tbaa !13
  %76 = fadd double %75, %72
  store double %76, ptr %74, align 8, !tbaa !13
  %77 = load ptr, ptr %13, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.grad_and_metrics, ptr %77, i32 0, i32 1
  %79 = load ptr, ptr %78, align 8, !tbaa !25
  %80 = getelementptr inbounds %struct.metrics, ptr %79, i32 0, i32 0
  %81 = load double, ptr %80, align 8, !tbaa !15
  %82 = load ptr, ptr %11, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.metrics, ptr %82, i32 0, i32 0
  %84 = load double, ptr %83, align 8, !tbaa !15
  %85 = fadd double %84, %81
  store double %85, ptr %83, align 8, !tbaa !15
  %86 = load ptr, ptr %13, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.grad_and_metrics, ptr %86, i32 0, i32 1
  %88 = load ptr, ptr %87, align 8, !tbaa !25
  call void @free(ptr noundef %88) #6
  br label %89

89:                                               ; preds = %45
  %90 = load i32, ptr %14, align 4, !tbaa !21
  %91 = add nsw i32 %90, 1
  store i32 %91, ptr %14, align 4, !tbaa !21
  br label %39, !llvm.loop !26

92:                                               ; preds = %44
  %93 = load ptr, ptr %13, align 8, !tbaa !5
  call void @free(ptr noundef %93) #6
  %94 = load ptr, ptr %12, align 8, !tbaa !5
  %95 = load i64, ptr %10, align 8, !tbaa !11
  call void @divide_grad_by_batch_size_(ptr noundef %94, i64 noundef %95)
  %96 = load ptr, ptr %6, align 8, !tbaa !5
  %97 = load ptr, ptr %12, align 8, !tbaa !5
  %98 = load double, ptr %9, align 8, !tbaa !9
  call void @update_weights_(ptr noundef %96, ptr noundef %97, double noundef %98)
  %99 = load i64, ptr %10, align 8, !tbaa !11
  %100 = uitofp i64 %99 to double
  %101 = load ptr, ptr %11, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.metrics, ptr %101, i32 0, i32 1
  %103 = load double, ptr %102, align 8, !tbaa !13
  %104 = fdiv double %103, %100
  store double %104, ptr %102, align 8, !tbaa !13
  %105 = load i64, ptr %10, align 8, !tbaa !11
  %106 = uitofp i64 %105 to double
  %107 = load ptr, ptr %11, align 8, !tbaa !5
  %108 = getelementptr inbounds %struct.metrics, ptr %107, i32 0, i32 0
  %109 = load double, ptr %108, align 8, !tbaa !15
  %110 = fdiv double %109, %106
  store double %110, ptr %108, align 8, !tbaa !15
  %111 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free_gradient(ptr noundef %111)
  %112 = load ptr, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  ret ptr %112
}

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #3

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

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #5

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nounwind allocsize(0,1) "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 "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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { nounwind allocsize(0,1) }

!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 8}
!14 = !{!"metrics", !10, i64 0, !10, i64 8}
!15 = !{!14, !10, i64 0}
!16 = !{!17, !12, i64 16}
!17 = !{!"", !6, i64 0, !10, i64 8, !12, i64 16, !12, i64 24}
!18 = !{!17, !12, i64 24}
!19 = !{!17, !6, i64 0}
!20 = !{!17, !10, i64 8}
!21 = !{!22, !22, i64 0}
!22 = !{!"int", !7, i64 0}
!23 = !{!24, !6, i64 0}
!24 = !{!"", !6, i64 0, !6, i64 8}
!25 = !{!24, !6, i64 8}
!26 = distinct !{!26, !27}
!27 = !{!"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: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @train_step(ptr noundef %0, ptr noundef %1, ptr nocapture noundef readonly %2, double noundef %3, i64 noundef %4) local_unnamed_addr #1 {
  %6 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #6
  %7 = tail call noalias dereferenceable_or_null(32) ptr @malloc(i64 noundef 32) #6
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = getelementptr inbounds i8, ptr %7, i64 16
  %10 = load <2 x i64>, ptr %8, align 8, !tbaa !5
  store <2 x i64> %10, ptr %9, align 8, !tbaa !5
  %11 = extractelement <2 x i64> %10, i64 0
  %12 = tail call noalias ptr @calloc(i64 noundef %11, i64 noundef 8) #7
  store ptr %12, ptr %7, align 8, !tbaa !9
  %13 = getelementptr inbounds i8, ptr %7, i64 8
  store double 0.000000e+00, ptr %13, align 8, !tbaa !13
  %14 = icmp ne i64 %4, 0
  tail call void @llvm.assume(i1 %14)
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph
  tail call void @free(ptr noundef nonnull %25) #8
  tail call void @divide_grad_by_batch_size_(ptr noundef nonnull %7, i64 noundef %4)
  tail call void @update_weights_(ptr noundef nonnull %0, ptr noundef nonnull %7, double noundef %3)
  %15 = uitofp nneg i64 %4 to double
  %16 = insertelement <2 x double> poison, double %15, i64 0
  %17 = shufflevector <2 x double> %16, <2 x double> poison, <2 x i32> zeroinitializer
  %18 = fdiv <2 x double> %31, %17
  store <2 x double> %18, ptr %6, align 8, !tbaa !14
  tail call void @free_gradient(ptr noundef nonnull %7)
  ret ptr %6

.lr.ph:                                           ; preds = %5, %.lr.ph
  %indvars.iv = phi i64 [ 0, %5 ], [ %indvars.iv.next, %.lr.ph ]
  %19 = phi <2 x double> [ zeroinitializer, %5 ], [ %31, %.lr.ph ]
  %20 = load i64, ptr %8, align 8, !tbaa !15
  %21 = mul i64 %20, %indvars.iv
  %22 = getelementptr inbounds double, ptr %1, i64 %21
  %23 = getelementptr inbounds double, ptr %2, i64 %indvars.iv
  %24 = load double, ptr %23, align 8, !tbaa !14
  %25 = tail call ptr @backprop(ptr noundef nonnull %0, ptr noundef %22, double noundef %24)
  %26 = load ptr, ptr %25, align 8, !tbaa !16
  tail call void @accumulate_gradient_(ptr noundef nonnull %7, ptr noundef %26)
  %27 = load ptr, ptr %25, align 8, !tbaa !16
  tail call void @free_gradient(ptr noundef %27)
  %28 = getelementptr inbounds i8, ptr %25, i64 8
  %29 = load ptr, ptr %28, align 8, !tbaa !18
  %30 = load <2 x double>, ptr %29, align 8, !tbaa !14
  %31 = fadd <2 x double> %30, %19
  tail call void @free(ptr noundef %29) #8
  %indvars.iv.next = add nuw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %4
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !19
}

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

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

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #4

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #5

attributes #0 = { 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 #1 = { 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 #2 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #5 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
attributes #6 = { nounwind allocsize(0) }
attributes #7 = { nounwind allocsize(0,1) }
attributes #8 = { 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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0, !12, i64 8, !6, i64 16, !6, i64 24}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = !{!10, !12, i64 8}
!14 = !{!12, !12, i64 0}
!15 = !{!10, !6, i64 16}
!16 = !{!17, !11, i64 0}
!17 = !{!"", !11, i64 0, !11, i64 8}
!18 = !{!17, !11, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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