; LLVM IR from KhawajaAbaid/machine-learning-in-c :: logistic_regression (backprop)

; 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.model = type { ptr, double, i64, i64 }
%struct.metrics = type { double, double }
%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 double @sigmoid(double noundef) #2

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

; Function Attrs: nounwind uwtable
define dso_local ptr @backprop(ptr noundef %0, ptr noundef %1, double noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca double, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca double, align 8
  %10 = alloca double, align 8
  %11 = alloca double, align 8
  %12 = alloca double, align 8
  %13 = alloca ptr, align 8
  %14 = alloca double, align 8
  %15 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store double %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %16 = call noalias ptr @malloc(i64 noundef 8) #5
  store ptr %16, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %17 = call noalias ptr @malloc(i64 noundef 32) #5
  store ptr %17, ptr %8, align 8, !tbaa !5
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.model, ptr %18, i32 0, i32 2
  %20 = load i64, ptr %19, align 8, !tbaa !11
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.model, ptr %21, i32 0, i32 2
  store i64 %20, ptr %22, align 8, !tbaa !11
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.model, ptr %23, i32 0, i32 3
  %25 = load i64, ptr %24, align 8, !tbaa !14
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.model, ptr %26, i32 0, i32 3
  store i64 %25, ptr %27, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.model, ptr %28, i32 0, i32 0
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.model, ptr %32, i32 0, i32 2
  %34 = load i64, ptr %33, align 8, !tbaa !11
  %35 = call double @vector_dot_product(ptr noundef %30, ptr noundef %31, i64 noundef %34)
  store double %35, ptr %9, align 8, !tbaa !9
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.model, ptr %36, i32 0, i32 1
  %38 = load double, ptr %37, align 8, !tbaa !16
  %39 = load double, ptr %9, align 8, !tbaa !9
  %40 = fadd double %39, %38
  store double %40, ptr %9, align 8, !tbaa !9
  %41 = load double, ptr %9, align 8, !tbaa !9
  %42 = call double @sigmoid(double noundef %41)
  store double %42, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %43 = load double, ptr %6, align 8, !tbaa !9
  %44 = load double, ptr %10, align 8, !tbaa !9
  %45 = call double @binary_crossentropy_loss(double noundef %43, double noundef %44)
  store double %45, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %46 = load double, ptr %10, align 8, !tbaa !9
  %47 = load double, ptr %6, align 8, !tbaa !9
  %48 = fsub double %46, %47
  store double %48, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = load double, ptr %12, align 8, !tbaa !9
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.model, ptr %51, i32 0, i32 2
  %53 = load i64, ptr %52, align 8, !tbaa !11
  %54 = call ptr @vector_scalar_product(ptr noundef %49, double noundef %50, i64 noundef %53)
  store ptr %54, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %55 = load double, ptr %12, align 8, !tbaa !9
  store double %55, ptr %14, align 8, !tbaa !9
  %56 = load ptr, ptr %13, align 8, !tbaa !5
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.model, ptr %57, i32 0, i32 0
  store ptr %56, ptr %58, align 8, !tbaa !15
  %59 = load double, ptr %14, align 8, !tbaa !9
  %60 = load ptr, ptr %8, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.model, ptr %60, i32 0, i32 1
  store double %59, ptr %61, align 8, !tbaa !16
  %62 = load double, ptr %11, align 8, !tbaa !9
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.metrics, ptr %63, i32 0, i32 0
  store double %62, ptr %64, align 8, !tbaa !17
  %65 = load double, ptr %6, align 8, !tbaa !9
  %66 = load double, ptr %10, align 8, !tbaa !9
  %67 = fcmp oge double %66, 5.000000e-01
  %68 = zext i1 %67 to i32
  %69 = sitofp i32 %68 to double
  %70 = fcmp oeq double %65, %69
  %71 = zext i1 %70 to i32
  %72 = sitofp i32 %71 to double
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.metrics, ptr %73, i32 0, i32 1
  store double %72, ptr %74, align 8, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  %75 = call noalias ptr @malloc(i64 noundef 16) #5
  store ptr %75, ptr %15, align 8, !tbaa !5
  %76 = load ptr, ptr %8, align 8, !tbaa !5
  %77 = load ptr, ptr %15, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.grad_and_metrics, ptr %77, i32 0, i32 0
  store ptr %76, ptr %78, align 8, !tbaa !20
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = load ptr, ptr %15, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.grad_and_metrics, ptr %80, i32 0, i32 1
  store ptr %79, ptr %81, align 8, !tbaa !22
  %82 = load ptr, ptr %15, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret ptr %82
}

declare double @vector_dot_product(ptr noundef, ptr noundef, i64 noundef) #3

declare ptr @vector_scalar_product(ptr noundef, double noundef, i64 noundef) #3

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 = { "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 }
attributes #5 = { 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, !13, i64 16}
!12 = !{!"", !6, i64 0, !10, i64 8, !13, i64 16, !13, i64 24}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 24}
!15 = !{!12, !6, i64 0}
!16 = !{!12, !10, i64 8}
!17 = !{!18, !10, i64 0}
!18 = !{!"metrics", !10, i64 0, !10, i64 8}
!19 = !{!18, !10, i64 8}
!20 = !{!21, !6, i64 0}
!21 = !{!"", !6, i64 0, !6, i64 8}
!22 = !{!21, !6, i64 8}


; 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 double @sigmoid(double noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @backprop(ptr nocapture noundef readonly %0, ptr noundef %1, double noundef %2) local_unnamed_addr #1 {
  %4 = tail call noalias dereferenceable_or_null(8) ptr @malloc(i64 noundef 8) #3
  %5 = tail call noalias dereferenceable_or_null(32) ptr @malloc(i64 noundef 32) #3
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = getelementptr inbounds i8, ptr %5, i64 16
  %8 = load <2 x i64>, ptr %6, align 8, !tbaa !5
  store <2 x i64> %8, ptr %7, align 8, !tbaa !5
  %9 = load ptr, ptr %0, align 8, !tbaa !9
  %10 = extractelement <2 x i64> %8, i64 0
  %11 = tail call double @vector_dot_product(ptr noundef %9, ptr noundef %1, i64 noundef %10) #4
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load double, ptr %12, align 8, !tbaa !13
  %14 = fadd double %11, %13
  %15 = tail call double @sigmoid(double noundef %14)
  %16 = tail call double @binary_crossentropy_loss(double noundef %2, double noundef %15)
  %17 = fsub double %15, %2
  %18 = load i64, ptr %6, align 8, !tbaa !14
  %19 = tail call ptr @vector_scalar_product(ptr noundef %1, double noundef %17, i64 noundef %18) #4
  store ptr %19, ptr %5, align 8, !tbaa !9
  %20 = getelementptr inbounds i8, ptr %5, i64 8
  store double %17, ptr %20, align 8, !tbaa !13
  %21 = fcmp oge double %15, 5.000000e-01
  %22 = uitofp i1 %21 to double
  %23 = fcmp oeq double %22, %2
  %24 = uitofp i1 %23 to double
  %25 = getelementptr inbounds i8, ptr %4, i64 8
  store double %24, ptr %25, align 8, !tbaa !15
  %26 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #3
  store ptr %5, ptr %26, align 8, !tbaa !17
  %27 = getelementptr inbounds i8, ptr %26, i64 8
  store ptr %4, ptr %27, align 8, !tbaa !19
  ret ptr %26
}

declare double @vector_dot_product(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #2

declare ptr @vector_scalar_product(ptr noundef, double noundef, i64 noundef) local_unnamed_addr #2

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 = { "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) }
attributes #4 = { 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 = !{!10, !6, i64 16}
!15 = !{!16, !12, i64 8}
!16 = !{!"metrics", !12, i64 0, !12, i64 8}
!17 = !{!18, !11, i64 0}
!18 = !{!"", !11, i64 0, !11, i64 8}
!19 = !{!18, !11, i64 8}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,TailCallElimPass