; LLVM IR from sbu-fsl/kernel-ml :: src/functions (square_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"

%union.val = type { double }
%struct.square_loss = type { ptr, ptr, ptr }
%struct.matrix = type { i32, i32, i32, %union.anon }
%union.anon = type { ptr }

@__const.derivative_square_loss.constant_mult = external hidden unnamed_addr constant { float, [4 x i8] }, align 8

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

; Function Attrs: nounwind uwtable
define dso_local void @derivative_square_loss(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca %union.val, align 8
  %4 = alloca %union.val, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %3, ptr align 8 @__const.derivative_square_loss.constant_mult, i64 8, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.square_loss, ptr %6, i32 0, i32 1
  %8 = load ptr, ptr %7, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.matrix, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 8, !tbaa !11
  %11 = sitofp i32 %10 to float
  store float %11, ptr %4, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = call ptr @diff_square_loss(ptr noundef %12)
  store ptr %13, ptr %5, align 8, !tbaa !5
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.square_loss, ptr %14, i32 0, i32 2
  %16 = load ptr, ptr %15, align 8, !tbaa !15
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %31

18:                                               ; preds = %1
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.matrix, ptr %19, i32 0, i32 0
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.matrix, ptr %22, i32 0, i32 1
  %24 = load i32, ptr %23, align 4, !tbaa !16
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.matrix, ptr %25, i32 0, i32 2
  %27 = load i32, ptr %26, align 8, !tbaa !17
  %28 = call ptr @allocate_matrix(i32 noundef %21, i32 noundef %24, i32 noundef %27)
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.square_loss, ptr %29, i32 0, i32 2
  store ptr %28, ptr %30, align 8, !tbaa !15
  br label %68

31:                                               ; preds = %1
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.square_loss, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !15
  %35 = getelementptr inbounds %struct.matrix, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !11
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.matrix, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !11
  %40 = icmp ne i32 %36, %39
  br i1 %40, label %51, label %41

41:                                               ; preds = %31
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.square_loss, ptr %42, i32 0, i32 2
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = getelementptr inbounds %struct.matrix, ptr %44, i32 0, i32 1
  %46 = load i32, ptr %45, align 4, !tbaa !16
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.matrix, ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !16
  %50 = icmp ne i32 %46, %49
  br i1 %50, label %51, label %67

51:                                               ; preds = %41, %31
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.square_loss, ptr %52, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !15
  call void @free_matrix(ptr noundef %54)
  %55 = load ptr, ptr %5, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.matrix, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !11
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.matrix, ptr %58, i32 0, i32 1
  %60 = load i32, ptr %59, align 4, !tbaa !16
  %61 = load ptr, ptr %5, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.matrix, ptr %61, i32 0, i32 2
  %63 = load i32, ptr %62, align 8, !tbaa !17
  %64 = call ptr @allocate_matrix(i32 noundef %57, i32 noundef %60, i32 noundef %63)
  %65 = load ptr, ptr %2, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.square_loss, ptr %65, i32 0, i32 2
  store ptr %64, ptr %66, align 8, !tbaa !15
  br label %67

67:                                               ; preds = %51, %41
  br label %68

68:                                               ; preds = %67, %18
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = load ptr, ptr %2, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.square_loss, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %71, align 8, !tbaa !15
  call void @matrix_mult_constant(ptr noundef %69, ptr noundef %3, ptr noundef %72)
  %73 = load ptr, ptr %2, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.square_loss, ptr %73, i32 0, i32 2
  %75 = load ptr, ptr %74, align 8, !tbaa !15
  %76 = load ptr, ptr %2, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.square_loss, ptr %76, i32 0, i32 2
  %78 = load ptr, ptr %77, align 8, !tbaa !15
  call void @matrix_div_constant(ptr noundef %75, ptr noundef %4, ptr noundef %78)
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free_matrix(ptr noundef %79)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  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

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

declare void @free_matrix(ptr noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

declare void @matrix_mult_constant(ptr noundef, ptr noundef, ptr noundef) #2

declare void @matrix_div_constant(ptr noundef, ptr noundef, ptr noundef) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"square_loss", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!12, !13, i64 0}
!12 = !{!"matrix", !13, i64 0, !13, i64 4, !13, i64 8, !7, i64 16}
!13 = !{!"int", !7, i64 0}
!14 = !{!7, !7, i64 0}
!15 = !{!10, !6, i64 16}
!16 = !{!12, !13, i64 4}
!17 = !{!12, !13, 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"

%union.val = type { double }

@__const.derivative_square_loss.constant_mult = external hidden unnamed_addr constant { float, [4 x i8] }, align 8

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

; Function Attrs: nounwind uwtable
define dso_local void @derivative_square_loss(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca %union.val, align 8
  %3 = alloca %union.val, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %4 = load i64, ptr @__const.derivative_square_loss.constant_mult, align 8
  store i64 %4, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = load i32, ptr %6, align 8, !tbaa !10
  %8 = sitofp i32 %7 to float
  store float %8, ptr %3, align 8, !tbaa !13
  %9 = tail call ptr @diff_square_loss(ptr noundef %0)
  %10 = getelementptr inbounds i8, ptr %0, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !14
  %12 = icmp eq ptr %11, null
  br i1 %12, label %.sink.split, label %13

13:                                               ; preds = %1
  %14 = load i32, ptr %11, align 8, !tbaa !10
  %15 = load i32, ptr %9, align 8, !tbaa !10
  %.not = icmp eq i32 %14, %15
  br i1 %.not, label %16, label %21

16:                                               ; preds = %13
  %17 = getelementptr inbounds i8, ptr %11, i64 4
  %18 = load i32, ptr %17, align 4, !tbaa !15
  %19 = getelementptr inbounds i8, ptr %9, i64 4
  %20 = load i32, ptr %19, align 4, !tbaa !15
  %.not21 = icmp eq i32 %18, %20
  br i1 %.not21, label %28, label %21

21:                                               ; preds = %16, %13
  tail call void @free_matrix(ptr noundef nonnull %11) #3
  br label %.sink.split

.sink.split:                                      ; preds = %1, %21
  %22 = load i32, ptr %9, align 8, !tbaa !10
  %23 = getelementptr inbounds i8, ptr %9, i64 4
  %24 = load i32, ptr %23, align 4, !tbaa !15
  %25 = getelementptr inbounds i8, ptr %9, i64 8
  %26 = load i32, ptr %25, align 8, !tbaa !16
  %27 = tail call ptr @allocate_matrix(i32 noundef %22, i32 noundef %24, i32 noundef %26) #3
  store ptr %27, ptr %10, align 8, !tbaa !14
  br label %28

28:                                               ; preds = %.sink.split, %16
  %29 = phi ptr [ %11, %16 ], [ %27, %.sink.split ]
  call void @matrix_mult_constant(ptr noundef nonnull %9, ptr noundef nonnull %2, ptr noundef %29) #3
  %30 = load ptr, ptr %10, align 8, !tbaa !14
  call void @matrix_div_constant(ptr noundef %30, ptr noundef nonnull %3, ptr noundef %30) #3
  call void @free_matrix(ptr noundef nonnull %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  ret void
}

; Function Attrs: mustprogress 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) local_unnamed_addr #2

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

declare void @free_matrix(ptr noundef) local_unnamed_addr #2

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

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

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 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, !7, i64 8}
!6 = !{!"square_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 = !{!8, !8, i64 0}
!14 = !{!6, !7, i64 16}
!15 = !{!11, !12, i64 4}
!16 = !{!11, !12, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass