; LLVM IR from sbu-fsl/kernel-ml :: src/models (linear_regression)

; 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.square_loss_functions_struct = type { ptr, ptr, ptr, ptr }
%struct.linear_regression = type { i32, ptr, ptr, ptr, ptr, %struct.model_data, %struct.model_multithreading, %struct.model_state }
%struct.model_data = type { ptr, ptr, ptr, ptr }
%struct.model_multithreading = type { [32 x %struct.mt_buffer], [32 x i32], i32, i32, i32, i64, ptr }
%struct.mt_buffer = type { ptr, ptr }
%struct.model_state = type { i8, i32 }
%struct.layers = type { ptr, ptr }
%struct.layer = type { ptr, i32, ptr, ptr }
%struct.loss = type { ptr, i32 }

@square_loss_functions = external global %struct.square_loss_functions_struct, align 8

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

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

declare void @free_matrix(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @clean_linear_regression(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct.linear_regression, ptr %4, i32 0, i32 5
  %6 = getelementptr inbounds %struct.model_data, ptr %5, i32 0, i32 2
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  call void @free_matrix(ptr noundef %7)
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.linear_regression, ptr %8, i32 0, i32 5
  %10 = getelementptr inbounds %struct.model_data, ptr %9, i32 0, i32 3
  %11 = load ptr, ptr %10, align 8, !tbaa !16
  call void @free_matrix(ptr noundef %11)
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.linear_regression, ptr %12, i32 0, i32 3
  %14 = load ptr, ptr %13, align 8, !tbaa !17
  %15 = getelementptr inbounds %struct.layers, ptr %14, i32 0, i32 0
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  store ptr %16, ptr %3, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %30, %1
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %34

20:                                               ; preds = %17
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.layer, ptr %21, i32 0, i32 1
  %23 = load i32, ptr %22, align 8, !tbaa !20
  switch i32 %23, label %28 [
    i32 0, label %24
  ]

24:                                               ; preds = %20
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.layer, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !22
  call void @clean_linear_layer(ptr noundef %27)
  br label %29

28:                                               ; preds = %20
  br label %29

29:                                               ; preds = %28, %24
  br label %30

30:                                               ; preds = %29
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.layer, ptr %31, i32 0, i32 2
  %33 = load ptr, ptr %32, align 8, !tbaa !23
  store ptr %33, ptr %3, align 8, !tbaa !5
  br label %17, !llvm.loop !24

34:                                               ; preds = %17
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.linear_regression, ptr %35, i32 0, i32 6
  call void @clean_multithreading_execution(ptr noundef %36)
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.linear_regression, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !26
  call void @cleanup_sgd_optimizer(ptr noundef %39)
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.linear_regression, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !17
  call void @delete_layers(ptr noundef %42)
  %43 = load ptr, ptr getelementptr inbounds (%struct.square_loss_functions_struct, ptr @square_loss_functions, i32 0, i32 3), align 8, !tbaa !27
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.linear_regression, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !29
  %47 = getelementptr inbounds %struct.loss, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !30
  call void %43(ptr noundef %48)
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.linear_regression, ptr %49, i32 0, i32 2
  %51 = load ptr, ptr %50, align 8, !tbaa !29
  call void @kml_free(ptr noundef %51)
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  call void @kml_free(ptr noundef %52)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret void
}

declare void @clean_linear_layer(ptr noundef) #1

declare void @clean_multithreading_execution(ptr noundef) #1

declare void @cleanup_sgd_optimizer(ptr noundef) #1

declare void @delete_layers(ptr noundef) #1

declare void @kml_free(ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 }

!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 56}
!10 = !{!"linear_regression", !11, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !12, i64 40, !13, i64 72, !15, i64 744}
!11 = !{!"int", !7, i64 0}
!12 = !{!"model_data", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!13 = !{!"model_multithreading", !7, i64 0, !7, i64 512, !7, i64 640, !7, i64 644, !7, i64 648, !14, i64 656, !6, i64 664}
!14 = !{!"long", !7, i64 0}
!15 = !{!"model_state", !7, i64 0, !7, i64 4}
!16 = !{!10, !6, i64 64}
!17 = !{!10, !6, i64 24}
!18 = !{!19, !6, i64 0}
!19 = !{!"layers", !6, i64 0, !6, i64 8}
!20 = !{!21, !11, i64 8}
!21 = !{!"layer", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24}
!22 = !{!21, !6, i64 0}
!23 = !{!21, !6, i64 16}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!10, !6, i64 8}
!27 = !{!28, !6, i64 24}
!28 = !{!"square_loss_functions_struct", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24}
!29 = !{!10, !6, i64 16}
!30 = !{!31, !6, i64 0}
!31 = !{!"loss", !6, i64 0, !11, 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"

%struct.square_loss_functions_struct = type { ptr, ptr, ptr, ptr }

@square_loss_functions = external local_unnamed_addr global %struct.square_loss_functions_struct, align 8

declare void @free_matrix(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @clean_linear_regression(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 56
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  tail call void @free_matrix(ptr noundef %3) #2
  %4 = getelementptr inbounds i8, ptr %0, i64 64
  %5 = load ptr, ptr %4, align 8, !tbaa !15
  tail call void @free_matrix(ptr noundef %5) #2
  %6 = getelementptr inbounds i8, ptr %0, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !16
  %.013 = load ptr, ptr %7, align 8, !tbaa !17
  %.not14 = icmp eq ptr %.013, null
  br i1 %.not14, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %12
  %.015 = phi ptr [ %.0, %12 ], [ %.013, %1 ]
  %8 = getelementptr inbounds i8, ptr %.015, i64 8
  %9 = load i32, ptr %8, align 8, !tbaa !18
  %cond = icmp eq i32 %9, 0
  br i1 %cond, label %10, label %12

10:                                               ; preds = %.lr.ph
  %11 = load ptr, ptr %.015, align 8, !tbaa !20
  tail call void @clean_linear_layer(ptr noundef %11) #2
  br label %12

12:                                               ; preds = %10, %.lr.ph
  %13 = getelementptr inbounds i8, ptr %.015, i64 16
  %.0 = load ptr, ptr %13, align 8, !tbaa !17
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !21

._crit_edge:                                      ; preds = %12, %1
  %14 = getelementptr inbounds i8, ptr %0, i64 72
  tail call void @clean_multithreading_execution(ptr noundef nonnull %14) #2
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !23
  tail call void @cleanup_sgd_optimizer(ptr noundef %16) #2
  %17 = load ptr, ptr %6, align 8, !tbaa !16
  tail call void @delete_layers(ptr noundef %17) #2
  %18 = load ptr, ptr getelementptr inbounds (i8, ptr @square_loss_functions, i64 24), align 8, !tbaa !24
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !26
  %21 = load ptr, ptr %20, align 8, !tbaa !27
  tail call void %18(ptr noundef %21) #2
  %22 = load ptr, ptr %19, align 8, !tbaa !26
  tail call void @kml_free(ptr noundef %22) #2
  tail call void @kml_free(ptr noundef %0) #2
  ret void
}

declare void @clean_linear_layer(ptr noundef) local_unnamed_addr #0

declare void @clean_multithreading_execution(ptr noundef) local_unnamed_addr #0

declare void @cleanup_sgd_optimizer(ptr noundef) local_unnamed_addr #0

declare void @delete_layers(ptr noundef) local_unnamed_addr #0

declare void @kml_free(ptr noundef) local_unnamed_addr #0

attributes #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 = { 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 = { 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 56}
!6 = !{!"linear_regression", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !11, i64 40, !12, i64 72, !14, i64 744}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"model_data", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!12 = !{!"model_multithreading", !8, i64 0, !8, i64 512, !8, i64 640, !8, i64 644, !8, i64 648, !13, i64 656, !10, i64 664}
!13 = !{!"long", !8, i64 0}
!14 = !{!"model_state", !8, i64 0, !8, i64 4}
!15 = !{!6, !10, i64 64}
!16 = !{!6, !10, i64 24}
!17 = !{!10, !10, i64 0}
!18 = !{!19, !7, i64 8}
!19 = !{!"layer", !10, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!20 = !{!19, !10, i64 0}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!6, !10, i64 8}
!24 = !{!25, !10, i64 24}
!25 = !{!"square_loss_functions_struct", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24}
!26 = !{!6, !10, i64 16}
!27 = !{!28, !10, i64 0}
!28 = !{!"loss", !10, i64 0, !7, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass