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

; 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.xor_net = 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_xor_net(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.xor_net, 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.xor_net, 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.xor_net, 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 = %34, %1
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %38

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 %32 [
    i32 0, label %24
    i32 1, label %28
  ]

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 %33

28:                                               ; preds = %20
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.layer, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !22
  call void @clean_sigmoid_layer(ptr noundef %31)
  br label %32

32:                                               ; preds = %28, %20
  br label %33

33:                                               ; preds = %32, %24
  br label %34

34:                                               ; preds = %33
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.layer, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !23
  store ptr %37, ptr %3, align 8, !tbaa !5
  br label %17, !llvm.loop !24

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

declare void @clean_linear_layer(ptr noundef) #1

declare void @clean_sigmoid_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 = !{!"xor_net", !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_xor_net(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
  %.014 = load ptr, ptr %7, align 8, !tbaa !17
  %.not15 = icmp eq ptr %.014, null
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %14
  %.016 = phi ptr [ %.0, %14 ], [ %.014, %1 ]
  %8 = getelementptr inbounds i8, ptr %.016, i64 8
  %9 = load i32, ptr %8, align 8, !tbaa !18
  switch i32 %9, label %14 [
    i32 0, label %10
    i32 1, label %12
  ]

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

12:                                               ; preds = %.lr.ph
  %13 = load ptr, ptr %.016, align 8, !tbaa !20
  tail call void @clean_sigmoid_layer(ptr noundef %13) #2
  br label %14

14:                                               ; preds = %10, %12, %.lr.ph
  %15 = getelementptr inbounds i8, ptr %.016, i64 16
  %.0 = load ptr, ptr %15, 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 = %14, %1
  %16 = getelementptr inbounds i8, ptr %0, i64 72
  tail call void @clean_multithreading_execution(ptr noundef nonnull %16) #2
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load ptr, ptr %17, align 8, !tbaa !23
  tail call void @cleanup_sgd_optimizer(ptr noundef %18) #2
  %19 = load ptr, ptr %6, align 8, !tbaa !16
  tail call void @delete_layers(ptr noundef %19) #2
  %20 = load ptr, ptr getelementptr inbounds (i8, ptr @square_loss_functions, i64 24), align 8, !tbaa !24
  %21 = getelementptr inbounds i8, ptr %0, i64 16
  %22 = load ptr, ptr %21, align 8, !tbaa !26
  %23 = load ptr, ptr %22, align 8, !tbaa !27
  tail call void %20(ptr noundef %23) #2
  %24 = load ptr, ptr %21, align 8, !tbaa !26
  tail call void @kml_free(ptr noundef %24) #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_sigmoid_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 = !{!"xor_net", !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