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

; 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.dataset = type { ptr, ptr, i64, i64 }
%struct.mlp = type { ptr, i64 }
%struct.layer = type { ptr, ptr, i64, i64 }
%struct.metrics = type { double, double }

@.str = external hidden unnamed_addr constant [61 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @train_step(ptr noundef, ptr noundef, ptr noundef, double noundef, i64 noundef) #3

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

42:                                               ; preds = %129, %5
  %43 = load i64, ptr %16, align 8, !tbaa !11
  %44 = load i64, ptr %10, align 8, !tbaa !11
  %45 = icmp ult i64 %43, %44
  br i1 %45, label %47, label %46

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

47:                                               ; preds = %42
  %48 = load ptr, ptr %13, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.metrics, ptr %48, i32 0, i32 0
  store double 0.000000e+00, ptr %49, align 8, !tbaa !19
  %50 = load ptr, ptr %13, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.metrics, ptr %50, i32 0, i32 1
  store double 0.000000e+00, ptr %51, align 8, !tbaa !21
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #5
  store i64 0, ptr %17, align 8, !tbaa !11
  br label %52

52:                                               ; preds = %103, %47
  %53 = load i64, ptr %17, align 8, !tbaa !11
  %54 = load i64, ptr %11, align 8, !tbaa !11
  %55 = icmp ult i64 %53, %54
  br i1 %55, label %56, label %106

56:                                               ; preds = %52
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.dataset, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !22
  %61 = load i64, ptr %17, align 8, !tbaa !11
  %62 = load i64, ptr %9, align 8, !tbaa !11
  %63 = mul i64 %61, %62
  %64 = load i64, ptr %14, align 8, !tbaa !11
  %65 = mul i64 %63, %64
  %66 = getelementptr inbounds double, ptr %60, i64 %65
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.dataset, ptr %67, i32 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !23
  %70 = load i64, ptr %17, align 8, !tbaa !11
  %71 = load i64, ptr %9, align 8, !tbaa !11
  %72 = mul i64 %70, %71
  %73 = load i64, ptr %15, align 8, !tbaa !11
  %74 = mul i64 %72, %73
  %75 = getelementptr inbounds double, ptr %69, i64 %74
  %76 = load double, ptr %8, align 8, !tbaa !9
  %77 = load i64, ptr %9, align 8, !tbaa !11
  %78 = call ptr @train_step(ptr noundef %57, ptr noundef %66, ptr noundef %75, double noundef %76, i64 noundef %77)
  store ptr %78, ptr %12, align 8, !tbaa !5
  %79 = load i64, ptr %16, align 8, !tbaa !11
  %80 = load i64, ptr %17, align 8, !tbaa !11
  %81 = load ptr, ptr %12, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.metrics, ptr %81, i32 0, i32 0
  %83 = load double, ptr %82, align 8, !tbaa !19
  %84 = load ptr, ptr %12, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.metrics, ptr %84, i32 0, i32 1
  %86 = load double, ptr %85, align 8, !tbaa !21
  %87 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %79, i64 noundef %80, double noundef %83, double noundef %86)
  %88 = load ptr, ptr %12, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.metrics, ptr %88, i32 0, i32 0
  %90 = load double, ptr %89, align 8, !tbaa !19
  %91 = load ptr, ptr %13, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.metrics, ptr %91, i32 0, i32 0
  %93 = load double, ptr %92, align 8, !tbaa !19
  %94 = fadd double %93, %90
  store double %94, ptr %92, align 8, !tbaa !19
  %95 = load ptr, ptr %12, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.metrics, ptr %95, i32 0, i32 1
  %97 = load double, ptr %96, align 8, !tbaa !21
  %98 = load ptr, ptr %13, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.metrics, ptr %98, i32 0, i32 1
  %100 = load double, ptr %99, align 8, !tbaa !21
  %101 = fadd double %100, %97
  store double %101, ptr %99, align 8, !tbaa !21
  %102 = load ptr, ptr %12, align 8, !tbaa !5
  call void @free(ptr noundef %102) #5
  br label %103

103:                                              ; preds = %56
  %104 = load i64, ptr %17, align 8, !tbaa !11
  %105 = add i64 %104, 1
  store i64 %105, ptr %17, align 8, !tbaa !11
  br label %52, !llvm.loop !24

106:                                              ; preds = %52
  %107 = load i64, ptr %11, align 8, !tbaa !11
  %108 = uitofp i64 %107 to double
  %109 = load ptr, ptr %13, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.metrics, ptr %109, i32 0, i32 0
  %111 = load double, ptr %110, align 8, !tbaa !19
  %112 = fdiv double %111, %108
  store double %112, ptr %110, align 8, !tbaa !19
  %113 = load i64, ptr %11, align 8, !tbaa !11
  %114 = uitofp i64 %113 to double
  %115 = load ptr, ptr %13, align 8, !tbaa !5
  %116 = getelementptr inbounds %struct.metrics, ptr %115, i32 0, i32 1
  %117 = load double, ptr %116, align 8, !tbaa !21
  %118 = fdiv double %117, %114
  store double %118, ptr %116, align 8, !tbaa !21
  %119 = load i64, ptr %16, align 8, !tbaa !11
  %120 = load i64, ptr %17, align 8, !tbaa !11
  %121 = load ptr, ptr %13, align 8, !tbaa !5
  %122 = getelementptr inbounds %struct.metrics, ptr %121, i32 0, i32 0
  %123 = load double, ptr %122, align 8, !tbaa !19
  %124 = load ptr, ptr %13, align 8, !tbaa !5
  %125 = getelementptr inbounds %struct.metrics, ptr %124, i32 0, i32 1
  %126 = load double, ptr %125, align 8, !tbaa !21
  %127 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %119, i64 noundef %120, double noundef %123, double noundef %126)
  %128 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #5
  br label %129

129:                                              ; preds = %106
  %130 = load i64, ptr %16, align 8, !tbaa !11
  %131 = add i64 %130, 1
  store i64 %131, ptr %16, align 8, !tbaa !11
  br label %42, !llvm.loop !26

132:                                              ; preds = %46
  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 void
}

declare i32 @printf(ptr noundef, ...) #4

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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 = { "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, !12, i64 16}
!14 = !{!"", !6, i64 0, !6, i64 8, !12, i64 16, !12, i64 24}
!15 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !12, i64 8}
!17 = !{!16, !12, i64 8}
!18 = !{!14, !12, i64 24}
!19 = !{!20, !10, i64 0}
!20 = !{!"metrics", !10, i64 0, !10, i64 8}
!21 = !{!20, !10, i64 8}
!22 = !{!14, !6, i64 0}
!23 = !{!14, !6, i64 8}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = distinct !{!26, !25}


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

@.str = external hidden unnamed_addr constant [61 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @fit(ptr noundef %0, ptr nocapture noundef readonly %1, double noundef %2, i64 noundef %3, i64 noundef %4) local_unnamed_addr #1 {
  %6 = getelementptr inbounds i8, ptr %1, i64 16
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = udiv i64 %7, %3
  %9 = load ptr, ptr %0, align 8, !tbaa !11
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !14
  %15 = getelementptr ptr, ptr %9, i64 %14
  %16 = getelementptr i8, ptr %15, i64 -8
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %17, i64 24
  %19 = load i64, ptr %18, align 8, !tbaa !15
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %._crit_edge46, label %.lr.ph45

.lr.ph45:                                         ; preds = %5
  %.not51 = icmp ult i64 %7, %3
  %20 = getelementptr inbounds i8, ptr %1, i64 8
  %21 = uitofp i64 %8 to double
  br i1 %.not51, label %.lr.ph45.split, label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph45, %._crit_edge.us
  %.04043.us = phi i64 [ %46, %._crit_edge.us ], [ 0, %.lr.ph45 ]
  br label %22

22:                                               ; preds = %.lr.ph.us, %22
  %.042.us = phi i64 [ 0, %.lr.ph.us ], [ %38, %22 ]
  %23 = phi <2 x double> [ zeroinitializer, %.lr.ph.us ], [ %37, %22 ]
  %24 = load ptr, ptr %1, align 8, !tbaa !16
  %25 = mul i64 %.042.us, %3
  %26 = mul i64 %25, %12
  %27 = getelementptr inbounds double, ptr %24, i64 %26
  %28 = load ptr, ptr %20, align 8, !tbaa !17
  %29 = mul i64 %25, %19
  %30 = getelementptr inbounds double, ptr %28, i64 %29
  %31 = tail call ptr @train_step(ptr noundef nonnull %0, ptr noundef %27, ptr noundef %30, double noundef %2, i64 noundef %3)
  %32 = load double, ptr %31, align 8, !tbaa !18
  %33 = getelementptr inbounds i8, ptr %31, i64 8
  %34 = load double, ptr %33, align 8, !tbaa !21
  %35 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %.04043.us, i64 noundef %.042.us, double noundef %32, double noundef %34)
  %36 = load <2 x double>, ptr %31, align 8, !tbaa !22
  %37 = fadd <2 x double> %36, %23
  tail call void @free(ptr noundef %31) #3
  %38 = add nuw i64 %.042.us, 1
  %39 = icmp ult i64 %38, %8
  br i1 %39, label %22, label %._crit_edge.us, !llvm.loop !23

._crit_edge.us:                                   ; preds = %22
  %40 = extractelement <2 x double> %37, i64 0
  %41 = fdiv double %40, %21
  %42 = extractelement <2 x double> %37, i64 1
  %43 = fdiv double %42, %21
  %44 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %.04043.us, i64 noundef %38, double noundef %41, double noundef %43)
  %45 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %46 = add nuw i64 %.04043.us, 1
  %exitcond.not = icmp eq i64 %46, %4
  br i1 %exitcond.not, label %._crit_edge46, label %.lr.ph.us, !llvm.loop !25

.lr.ph45.split:                                   ; preds = %.lr.ph45
  %47 = fdiv double 0.000000e+00, %21
  br label %48

._crit_edge46:                                    ; preds = %._crit_edge.us, %48, %5
  ret void

48:                                               ; preds = %.lr.ph45.split, %48
  %.04043 = phi i64 [ 0, %.lr.ph45.split ], [ %51, %48 ]
  %49 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i64 noundef %.04043, i64 noundef 0, double noundef %47, double noundef %47)
  %50 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %51 = add nuw i64 %.04043, 1
  %exitcond55.not = icmp eq i64 %51, %4
  br i1 %exitcond55.not, label %._crit_edge46, label %48, !llvm.loop !25
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

attributes #0 = { 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 #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 = { nofree 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 #3 = { 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, !10, i64 16}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"", !7, i64 0, !10, i64 8}
!13 = !{!7, !7, i64 0}
!14 = !{!12, !10, i64 8}
!15 = !{!6, !10, i64 24}
!16 = !{!6, !7, i64 0}
!17 = !{!6, !7, i64 8}
!18 = !{!19, !20, i64 0}
!19 = !{!"metrics", !20, i64 0, !20, i64 8}
!20 = !{!"double", !8, i64 0}
!21 = !{!19, !20, i64 8}
!22 = !{!20, !20, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = distinct !{!25, !24}


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