; LLVM IR from sbu-fsl/kernel-ml :: src/functions (binary_cross_entropy_loss)

; 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.binary_cross_entropy_loss = type { ptr, ptr, ptr }
%struct.matrix = type { i32, i32, i32, %union.anon }
%union.anon = type { ptr }

; Function Attrs: nounwind uwtable
define dso_local void @derivative_binary_cross_entropy_loss(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %4, i32 0, i32 2
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %27

8:                                                ; preds = %1
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %9, i32 0, i32 0
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = getelementptr inbounds %struct.matrix, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !14
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = getelementptr inbounds %struct.matrix, ptr %16, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !16
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = getelementptr inbounds %struct.matrix, ptr %21, i32 0, i32 2
  %23 = load i32, ptr %22, align 8, !tbaa !17
  %24 = call ptr @allocate_matrix(i32 noundef %13, i32 noundef %18, i32 noundef %23)
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %25, i32 0, i32 2
  store ptr %24, ptr %26, align 8, !tbaa !11
  br label %74

27:                                               ; preds = %1
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = getelementptr inbounds %struct.matrix, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !14
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %33, i32 0, i32 0
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.matrix, ptr %35, i32 0, i32 0
  %37 = load i32, ptr %36, align 8, !tbaa !14
  %38 = icmp ne i32 %32, %37
  br i1 %38, label %51, label %39

39:                                               ; preds = %27
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %40, i32 0, i32 2
  %42 = load ptr, ptr %41, align 8, !tbaa !11
  %43 = getelementptr inbounds %struct.matrix, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !16
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = getelementptr inbounds %struct.matrix, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !16
  %50 = icmp ne i32 %44, %49
  br i1 %50, label %51, label %73

51:                                               ; preds = %39, %27
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %52, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !11
  call void @free_matrix(ptr noundef %54)
  %55 = load ptr, ptr %2, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %55, i32 0, i32 0
  %57 = load ptr, ptr %56, align 8, !tbaa !13
  %58 = getelementptr inbounds %struct.matrix, ptr %57, i32 0, i32 0
  %59 = load i32, ptr %58, align 8, !tbaa !14
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %60, i32 0, i32 0
  %62 = load ptr, ptr %61, align 8, !tbaa !13
  %63 = getelementptr inbounds %struct.matrix, ptr %62, i32 0, i32 1
  %64 = load i32, ptr %63, align 4, !tbaa !16
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %65, i32 0, i32 0
  %67 = load ptr, ptr %66, align 8, !tbaa !13
  %68 = getelementptr inbounds %struct.matrix, ptr %67, i32 0, i32 2
  %69 = load i32, ptr %68, align 8, !tbaa !17
  %70 = call ptr @allocate_matrix(i32 noundef %59, i32 noundef %64, i32 noundef %69)
  %71 = load ptr, ptr %2, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %71, i32 0, i32 2
  store ptr %70, ptr %72, align 8, !tbaa !11
  br label %73

73:                                               ; preds = %51, %39
  br label %74

74:                                               ; preds = %73, %8
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %119, %74
  %76 = load i32, ptr %3, align 4, !tbaa !9
  %77 = load ptr, ptr %2, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %77, i32 0, i32 0
  %79 = load ptr, ptr %78, align 8, !tbaa !13
  %80 = getelementptr inbounds %struct.matrix, ptr %79, i32 0, i32 0
  %81 = load i32, ptr %80, align 8, !tbaa !14
  %82 = icmp slt i32 %76, %81
  br i1 %82, label %83, label %122

83:                                               ; preds = %75
  %84 = load ptr, ptr %2, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %84, i32 0, i32 0
  %86 = load ptr, ptr %85, align 8, !tbaa !13
  %87 = getelementptr inbounds %struct.matrix, ptr %86, i32 0, i32 3
  %88 = load ptr, ptr %87, align 8, !tbaa !18
  %89 = load i32, ptr %3, align 4, !tbaa !9
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds float, ptr %88, i64 %90
  %92 = load float, ptr %91, align 4, !tbaa !19
  %93 = load ptr, ptr %2, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %93, i32 0, i32 0
  %95 = load ptr, ptr %94, align 8, !tbaa !13
  %96 = getelementptr inbounds %struct.matrix, ptr %95, i32 0, i32 3
  %97 = load ptr, ptr %96, align 8, !tbaa !18
  %98 = load i32, ptr %3, align 4, !tbaa !9
  %99 = sext i32 %98 to i64
  %100 = getelementptr inbounds float, ptr %97, i64 %99
  %101 = load float, ptr %100, align 4, !tbaa !19
  %102 = fsub float 1.000000e+00, %101
  %103 = fmul float %92, %102
  %104 = load ptr, ptr %2, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %104, i32 0, i32 2
  %106 = load ptr, ptr %105, align 8, !tbaa !11
  %107 = getelementptr inbounds %struct.matrix, ptr %106, i32 0, i32 3
  %108 = load ptr, ptr %107, align 8, !tbaa !18
  %109 = load i32, ptr %3, align 4, !tbaa !9
  %110 = load ptr, ptr %2, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.binary_cross_entropy_loss, ptr %110, i32 0, i32 2
  %112 = load ptr, ptr %111, align 8, !tbaa !11
  %113 = getelementptr inbounds %struct.matrix, ptr %112, i32 0, i32 1
  %114 = load i32, ptr %113, align 4, !tbaa !16
  %115 = mul nsw i32 %109, %114
  %116 = add nsw i32 %115, 0
  %117 = sext i32 %116 to i64
  %118 = getelementptr inbounds float, ptr %108, i64 %117
  store float %103, ptr %118, align 4, !tbaa !19
  br label %119

119:                                              ; preds = %83
  %120 = load i32, ptr %3, align 4, !tbaa !9
  %121 = add nsw i32 %120, 1
  store i32 %121, ptr %3, align 4, !tbaa !9
  br label %75, !llvm.loop !21

122:                                              ; preds = %75
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret void
}

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

declare ptr @allocate_matrix(i32 noundef, i32 noundef, i32 noundef) #2

declare void @free_matrix(ptr noundef) #2

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

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 = { "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, !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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"binary_cross_entropy_loss", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 0}
!14 = !{!15, !10, i64 0}
!15 = !{!"matrix", !10, i64 0, !10, i64 4, !10, i64 8, !7, i64 16}
!16 = !{!15, !10, i64 4}
!17 = !{!15, !10, i64 8}
!18 = !{!7, !7, i64 0}
!19 = !{!20, !20, i64 0}
!20 = !{!"float", !7, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"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
define dso_local void @derivative_binary_cross_entropy_loss(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %.sink.split, label %5

5:                                                ; preds = %1
  %6 = load i32, ptr %3, align 8, !tbaa !10
  %7 = load ptr, ptr %0, align 8, !tbaa !13
  %8 = load i32, ptr %7, align 8, !tbaa !10
  %.not = icmp eq i32 %6, %8
  br i1 %.not, label %9, label %14

9:                                                ; preds = %5
  %10 = getelementptr inbounds i8, ptr %3, i64 4
  %11 = load i32, ptr %10, align 4, !tbaa !14
  %12 = getelementptr inbounds i8, ptr %7, i64 4
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %.not24 = icmp eq i32 %11, %13
  br i1 %.not24, label %22, label %14

14:                                               ; preds = %9, %5
  tail call void @free_matrix(ptr noundef nonnull %3) #2
  br label %.sink.split

.sink.split:                                      ; preds = %1, %14
  %15 = load ptr, ptr %0, align 8, !tbaa !13
  %16 = load i32, ptr %15, align 8, !tbaa !10
  %17 = getelementptr inbounds i8, ptr %15, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !14
  %19 = getelementptr inbounds i8, ptr %15, i64 8
  %20 = load i32, ptr %19, align 8, !tbaa !15
  %21 = tail call ptr @allocate_matrix(i32 noundef %16, i32 noundef %18, i32 noundef %20) #2
  store ptr %21, ptr %2, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %.sink.split, %9
  %23 = phi ptr [ %3, %9 ], [ %21, %.sink.split ]
  %24 = load ptr, ptr %0, align 8, !tbaa !13
  %25 = load i32, ptr %24, align 8, !tbaa !10
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %22
  %27 = getelementptr inbounds i8, ptr %24, i64 16
  %28 = getelementptr inbounds i8, ptr %23, i64 16
  %29 = getelementptr inbounds i8, ptr %23, i64 4
  %30 = load i32, ptr %29, align 4, !tbaa !14
  %31 = sext i32 %30 to i64
  %wide.trip.count = zext nneg i32 %25 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %32 = icmp eq i32 %25, 1
  br i1 %32, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %33

33:                                               ; preds = %33, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %33 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %33 ]
  %34 = load ptr, ptr %27, align 8, !tbaa !16
  %35 = getelementptr inbounds float, ptr %34, i64 %indvars.iv
  %36 = load float, ptr %35, align 4, !tbaa !17
  %37 = fsub float 1.000000e+00, %36
  %38 = fmul float %36, %37
  %39 = load ptr, ptr %28, align 8, !tbaa !16
  %40 = mul nsw i64 %indvars.iv, %31
  %41 = getelementptr inbounds float, ptr %39, i64 %40
  store float %38, ptr %41, align 4, !tbaa !17
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %42 = load ptr, ptr %27, align 8, !tbaa !16
  %43 = getelementptr inbounds float, ptr %42, i64 %indvars.iv.next
  %44 = load float, ptr %43, align 4, !tbaa !17
  %45 = fsub float 1.000000e+00, %44
  %46 = fmul float %44, %45
  %47 = load ptr, ptr %28, align 8, !tbaa !16
  %48 = mul nsw i64 %indvars.iv.next, %31
  %49 = getelementptr inbounds float, ptr %47, i64 %48
  store float %46, ptr %49, align 4, !tbaa !17
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %33, !llvm.loop !19

._crit_edge.loopexit.unr-lcssa:                   ; preds = %33, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %33 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %._crit_edge.loopexit.epilog-lcssa

._crit_edge.loopexit.epilog-lcssa:                ; preds = %._crit_edge.loopexit.unr-lcssa
  %50 = load ptr, ptr %27, align 8, !tbaa !16
  %51 = getelementptr inbounds float, ptr %50, i64 %indvars.iv.unr
  %52 = load float, ptr %51, align 4, !tbaa !17
  %53 = fsub float 1.000000e+00, %52
  %54 = fmul float %52, %53
  %55 = load ptr, ptr %28, align 8, !tbaa !16
  %56 = mul nsw i64 %indvars.iv.unr, %31
  %57 = getelementptr inbounds float, ptr %55, i64 %56
  store float %54, ptr %57, align 4, !tbaa !17
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.epilog-lcssa, %._crit_edge.loopexit.unr-lcssa, %22
  ret void
}

declare ptr @allocate_matrix(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

declare void @free_matrix(ptr noundef) local_unnamed_addr #1

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 = { "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 }

!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 16}
!6 = !{!"binary_cross_entropy_loss", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !12, i64 0}
!11 = !{!"matrix", !12, i64 0, !12, i64 4, !12, i64 8, !8, i64 16}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !7, i64 0}
!14 = !{!11, !12, i64 4}
!15 = !{!11, !12, i64 8}
!16 = !{!8, !8, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"float", !8, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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