; LLVM IR from sbu-fsl/kernel-ml :: src/optimizers (sgd_optimizer)

; 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.sgd_optimizer = type { float, %union.val, %union.val, %union.val, ptr, ptr, ptr }
%union.val = type { double }
%struct.layers = type { ptr, ptr }
%struct.layer = type { ptr, i32, ptr, ptr }

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

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

declare ptr @kml_malloc(i64 noundef) #2

; 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 ptr @allocate_updates() #0

; Function Attrs: nounwind uwtable
declare dso_local void @add_updates(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @build_sgd_optimizer(float noundef %0, float noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca float, align 4
  %6 = alloca float, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  store float %0, ptr %5, align 4, !tbaa !5
  store float %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %11 = call ptr @kml_malloc(i64 noundef 56)
  store ptr %11, ptr %10, align 8, !tbaa !9
  %12 = load float, ptr %5, align 4, !tbaa !5
  %13 = load ptr, ptr %10, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.sgd_optimizer, ptr %13, i32 0, i32 0
  store float %12, ptr %14, align 8, !tbaa !11
  %15 = load float, ptr %6, align 4, !tbaa !5
  %16 = load ptr, ptr %10, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.sgd_optimizer, ptr %16, i32 0, i32 3
  store float %15, ptr %17, align 8, !tbaa !13
  %18 = load ptr, ptr %7, align 8, !tbaa !9
  %19 = load ptr, ptr %10, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.sgd_optimizer, ptr %19, i32 0, i32 4
  store ptr %18, ptr %20, align 8, !tbaa !14
  %21 = load ptr, ptr %8, align 8, !tbaa !9
  %22 = load ptr, ptr %10, align 8, !tbaa !9
  %23 = getelementptr inbounds %struct.sgd_optimizer, ptr %22, i32 0, i32 6
  store ptr %21, ptr %23, align 8, !tbaa !15
  %24 = load ptr, ptr %10, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.sgd_optimizer, ptr %24, i32 0, i32 2
  store float 0.000000e+00, ptr %25, align 8, !tbaa !13
  %26 = load ptr, ptr %10, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.sgd_optimizer, ptr %26, i32 0, i32 1
  store float 0.000000e+00, ptr %27, align 8, !tbaa !13
  %28 = call ptr @allocate_updates_list()
  %29 = load ptr, ptr %10, align 8, !tbaa !9
  %30 = getelementptr inbounds %struct.sgd_optimizer, ptr %29, i32 0, i32 5
  store ptr %28, ptr %30, align 8, !tbaa !16
  %31 = load ptr, ptr %7, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.layers, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  store ptr %33, ptr %9, align 8, !tbaa !9
  br label %34

34:                                               ; preds = %42, %4
  %35 = load ptr, ptr %9, align 8, !tbaa !9
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %46

37:                                               ; preds = %34
  %38 = load ptr, ptr %10, align 8, !tbaa !9
  %39 = getelementptr inbounds %struct.sgd_optimizer, ptr %38, i32 0, i32 5
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = call ptr @allocate_updates()
  call void @add_updates(ptr noundef %40, ptr noundef %41)
  br label %42

42:                                               ; preds = %37
  %43 = load ptr, ptr %9, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.layer, ptr %43, i32 0, i32 3
  %45 = load ptr, ptr %44, align 8, !tbaa !19
  store ptr %45, ptr %9, align 8, !tbaa !9
  br label %34, !llvm.loop !22

46:                                               ; preds = %34
  %47 = load ptr, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret ptr %47
}

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 = !{!"float", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"sgd_optimizer", !6, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!13 = !{!7, !7, i64 0}
!14 = !{!12, !10, i64 32}
!15 = !{!12, !10, i64 48}
!16 = !{!12, !10, i64 40}
!17 = !{!18, !10, i64 8}
!18 = !{!"layers", !10, i64 0, !10, i64 8}
!19 = !{!20, !10, i64 24}
!20 = !{!"layer", !10, i64 0, !21, i64 8, !10, i64 16, !10, i64 24}
!21 = !{!"int", !7, i64 0}
!22 = distinct !{!22, !23}
!23 = !{!"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
declare dso_local ptr @allocate_updates_list() local_unnamed_addr #0

declare ptr @kml_malloc(i64 noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @build_sgd_optimizer(float noundef %0, float noundef %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = tail call ptr @kml_malloc(i64 noundef 56) #2
  store float %0, ptr %5, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  store float %1, ptr %6, align 8, !tbaa !11
  %7 = getelementptr inbounds i8, ptr %5, i64 32
  store ptr %2, ptr %7, align 8, !tbaa !12
  %8 = getelementptr inbounds i8, ptr %5, i64 48
  store ptr %3, ptr %8, align 8, !tbaa !13
  %9 = getelementptr inbounds i8, ptr %5, i64 16
  store float 0.000000e+00, ptr %9, align 8, !tbaa !11
  %10 = getelementptr inbounds i8, ptr %5, i64 8
  store float 0.000000e+00, ptr %10, align 8, !tbaa !11
  %11 = tail call ptr @allocate_updates_list()
  %12 = getelementptr inbounds i8, ptr %5, i64 40
  store ptr %11, ptr %12, align 8, !tbaa !14
  %13 = getelementptr inbounds i8, ptr %2, i64 8
  %.016 = load ptr, ptr %13, align 8, !tbaa !15
  %.not17 = icmp eq ptr %.016, null
  br i1 %.not17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %.lr.ph
  %.018 = phi ptr [ %.0, %.lr.ph ], [ %.016, %4 ]
  %14 = load ptr, ptr %12, align 8, !tbaa !14
  %15 = tail call ptr @allocate_updates()
  tail call void @add_updates(ptr noundef %14, ptr noundef %15)
  %16 = getelementptr inbounds i8, ptr %.018, i64 24
  %.0 = load ptr, ptr %16, align 8, !tbaa !15
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph, %4
  ret ptr %5
}

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 0}
!6 = !{!"sgd_optimizer", !7, i64 0, !8, i64 8, !8, i64 16, !8, i64 24, !10, i64 32, !10, i64 40, !10, i64 48}
!7 = !{!"float", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!8, !8, i64 0}
!12 = !{!6, !10, i64 32}
!13 = !{!6, !10, i64 48}
!14 = !{!6, !10, i64 40}
!15 = !{!10, !10, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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