; LLVM IR from KhawajaAbaid/machine-learning-in-c :: nn (divide_grad_by_batch_size_)

; 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.mlp = type { ptr, i64 }
%struct.layer = type { ptr, ptr, i64, i64 }

; 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

; Function Attrs: nounwind uwtable
define dso_local void @divide_grad_by_batch_size_(ptr noundef %0, i64 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %82, %2
  %10 = load i64, ptr %5, align 8, !tbaa !9
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mlp, ptr %11, i32 0, i32 1
  %13 = load i64, ptr %12, align 8, !tbaa !11
  %14 = icmp ult i64 %10, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %9
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %85

16:                                               ; preds = %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %17

17:                                               ; preds = %78, %16
  %18 = load i64, ptr %7, align 8, !tbaa !9
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.mlp, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !13
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = getelementptr inbounds ptr, ptr %21, i64 %22
  %24 = load ptr, ptr %23, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.layer, ptr %24, i32 0, i32 3
  %26 = load i64, ptr %25, align 8, !tbaa !14
  %27 = icmp ult i64 %18, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %17
  store i32 5, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %81

29:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %60, %29
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.mlp, ptr %32, i32 0, i32 0
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  %35 = load i64, ptr %5, align 8, !tbaa !9
  %36 = getelementptr inbounds ptr, ptr %34, i64 %35
  %37 = load ptr, ptr %36, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.layer, ptr %37, i32 0, i32 2
  %39 = load i64, ptr %38, align 8, !tbaa !16
  %40 = icmp ult i64 %31, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %30
  store i32 8, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %63

42:                                               ; preds = %30
  %43 = load i64, ptr %4, align 8, !tbaa !9
  %44 = uitofp i64 %43 to double
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.mlp, ptr %45, i32 0, i32 0
  %47 = load ptr, ptr %46, align 8, !tbaa !13
  %48 = load i64, ptr %5, align 8, !tbaa !9
  %49 = getelementptr inbounds ptr, ptr %47, i64 %48
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.layer, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !17
  %53 = load i64, ptr %7, align 8, !tbaa !9
  %54 = getelementptr inbounds ptr, ptr %52, i64 %53
  %55 = load ptr, ptr %54, align 8, !tbaa !5
  %56 = load i64, ptr %8, align 8, !tbaa !9
  %57 = getelementptr inbounds double, ptr %55, i64 %56
  %58 = load double, ptr %57, align 8, !tbaa !18
  %59 = fdiv double %58, %44
  store double %59, ptr %57, align 8, !tbaa !18
  br label %60

60:                                               ; preds = %42
  %61 = load i64, ptr %8, align 8, !tbaa !9
  %62 = add i64 %61, 1
  store i64 %62, ptr %8, align 8, !tbaa !9
  br label %30, !llvm.loop !20

63:                                               ; preds = %41
  %64 = load i64, ptr %4, align 8, !tbaa !9
  %65 = uitofp i64 %64 to double
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.mlp, ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !13
  %69 = load i64, ptr %5, align 8, !tbaa !9
  %70 = getelementptr inbounds ptr, ptr %68, i64 %69
  %71 = load ptr, ptr %70, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.layer, ptr %71, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !22
  %74 = load i64, ptr %7, align 8, !tbaa !9
  %75 = getelementptr inbounds double, ptr %73, i64 %74
  %76 = load double, ptr %75, align 8, !tbaa !18
  %77 = fdiv double %76, %65
  store double %77, ptr %75, align 8, !tbaa !18
  br label %78

78:                                               ; preds = %63
  %79 = load i64, ptr %7, align 8, !tbaa !9
  %80 = add i64 %79, 1
  store i64 %80, ptr %7, align 8, !tbaa !9
  br label %17, !llvm.loop !23

81:                                               ; preds = %28
  br label %82

82:                                               ; preds = %81
  %83 = load i64, ptr %5, align 8, !tbaa !9
  %84 = add i64 %83, 1
  store i64 %84, ptr %5, align 8, !tbaa !9
  br label %9, !llvm.loop !24

85:                                               ; preds = %15
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"", !6, i64 0, !10, i64 8}
!13 = !{!12, !6, i64 0}
!14 = !{!15, !10, i64 24}
!15 = !{!"", !6, i64 0, !6, i64 8, !10, i64 16, !10, i64 24}
!16 = !{!15, !10, i64 16}
!17 = !{!15, !6, i64 0}
!18 = !{!19, !19, i64 0}
!19 = !{!"double", !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!15, !6, i64 8}
!23 = distinct !{!23, !21}
!24 = distinct !{!24, !21}


; 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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @divide_grad_by_batch_size_(ptr nocapture noundef readonly %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %.not = icmp eq i64 %4, 0
  br i1 %.not, label %._crit_edge, label %.preheader21.lr.ph

.preheader21.lr.ph:                               ; preds = %2
  %5 = load ptr, ptr %0, align 8, !tbaa !11
  %6 = uitofp i64 %1 to double
  %broadcast.splatinsert44 = insertelement <2 x double> poison, double %6, i64 0
  %broadcast.splat45 = shufflevector <2 x double> %broadcast.splatinsert44, <2 x double> poison, <2 x i32> zeroinitializer
  %broadcast.splatinsert = insertelement <2 x double> poison, double %6, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <2 x double> poison, <2 x i32> zeroinitializer
  br label %.preheader21

.preheader21:                                     ; preds = %.preheader21.lr.ph, %._crit_edge24
  %.02025 = phi i64 [ 0, %.preheader21.lr.ph ], [ %36, %._crit_edge24 ]
  %7 = getelementptr inbounds ptr, ptr %5, i64 %.02025
  %8 = load ptr, ptr %7, align 8, !tbaa !12
  %9 = getelementptr inbounds i8, ptr %8, i64 24
  %10 = load i64, ptr %9, align 8, !tbaa !13
  %.not26 = icmp eq i64 %10, 0
  br i1 %.not26, label %._crit_edge24, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %.preheader21
  %11 = getelementptr inbounds i8, ptr %8, i64 16
  %12 = load i64, ptr %11, align 8, !tbaa !15
  %.not27 = icmp eq i64 %12, 0
  %13 = getelementptr inbounds i8, ptr %8, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !16
  br i1 %.not27, label %.preheader.preheader, label %.preheader.lr.ph.split.us

.preheader.preheader:                             ; preds = %.preheader.lr.ph
  %min.iters.check = icmp eq i64 %10, 1
  br i1 %min.iters.check, label %.preheader.preheader48, label %vector.ph

.preheader.preheader48:                           ; preds = %middle.block, %.preheader.preheader
  %.01923.ph = phi i64 [ 0, %.preheader.preheader ], [ %n.vec, %middle.block ]
  br label %.preheader

vector.ph:                                        ; preds = %.preheader.preheader
  %n.vec = and i64 %10, -2
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %15 = getelementptr inbounds double, ptr %14, i64 %index
  %wide.load = load <2 x double>, ptr %15, align 8, !tbaa !17
  %16 = fdiv <2 x double> %wide.load, %broadcast.splat
  store <2 x double> %16, ptr %15, align 8, !tbaa !17
  %index.next = add nuw i64 %index, 2
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !19

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %10, %n.vec
  br i1 %cmp.n, label %._crit_edge24, label %.preheader.preheader48

.preheader.lr.ph.split.us:                        ; preds = %.preheader.lr.ph
  %18 = load ptr, ptr %8, align 8, !tbaa !23
  %min.iters.check36 = icmp eq i64 %12, 1
  %n.vec39 = and i64 %12, -2
  %cmp.n47 = icmp eq i64 %12, %n.vec39
  br label %.preheader.us

.preheader.us:                                    ; preds = %._crit_edge.us, %.preheader.lr.ph.split.us
  %.01923.us = phi i64 [ 0, %.preheader.lr.ph.split.us ], [ %31, %._crit_edge.us ]
  %19 = getelementptr inbounds ptr, ptr %18, i64 %.01923.us
  %20 = load ptr, ptr %19, align 8, !tbaa !12
  br i1 %min.iters.check36, label %scalar.ph35.preheader, label %vector.body41

vector.body41:                                    ; preds = %.preheader.us, %vector.body41
  %index42 = phi i64 [ %index.next46, %vector.body41 ], [ 0, %.preheader.us ]
  %21 = getelementptr inbounds double, ptr %20, i64 %index42
  %wide.load43 = load <2 x double>, ptr %21, align 8, !tbaa !17
  %22 = fdiv <2 x double> %wide.load43, %broadcast.splat45
  store <2 x double> %22, ptr %21, align 8, !tbaa !17
  %index.next46 = add nuw i64 %index42, 2
  %23 = icmp eq i64 %index.next46, %n.vec39
  br i1 %23, label %middle.block34, label %vector.body41, !llvm.loop !24

middle.block34:                                   ; preds = %vector.body41
  br i1 %cmp.n47, label %._crit_edge.us, label %scalar.ph35.preheader

scalar.ph35.preheader:                            ; preds = %middle.block34, %.preheader.us
  %.022.us.ph = phi i64 [ 0, %.preheader.us ], [ %n.vec39, %middle.block34 ]
  br label %scalar.ph35

scalar.ph35:                                      ; preds = %scalar.ph35.preheader, %scalar.ph35
  %.022.us = phi i64 [ %27, %scalar.ph35 ], [ %.022.us.ph, %scalar.ph35.preheader ]
  %24 = getelementptr inbounds double, ptr %20, i64 %.022.us
  %25 = load double, ptr %24, align 8, !tbaa !17
  %26 = fdiv double %25, %6
  store double %26, ptr %24, align 8, !tbaa !17
  %27 = add nuw i64 %.022.us, 1
  %exitcond.not = icmp eq i64 %27, %12
  br i1 %exitcond.not, label %._crit_edge.us, label %scalar.ph35, !llvm.loop !25

._crit_edge.us:                                   ; preds = %scalar.ph35, %middle.block34
  %28 = getelementptr inbounds double, ptr %14, i64 %.01923.us
  %29 = load double, ptr %28, align 8, !tbaa !17
  %30 = fdiv double %29, %6
  store double %30, ptr %28, align 8, !tbaa !17
  %31 = add nuw i64 %.01923.us, 1
  %exitcond29.not = icmp eq i64 %31, %10
  br i1 %exitcond29.not, label %._crit_edge24, label %.preheader.us, !llvm.loop !26

._crit_edge:                                      ; preds = %._crit_edge24, %2
  ret void

.preheader:                                       ; preds = %.preheader.preheader48, %.preheader
  %.01923 = phi i64 [ %35, %.preheader ], [ %.01923.ph, %.preheader.preheader48 ]
  %32 = getelementptr inbounds double, ptr %14, i64 %.01923
  %33 = load double, ptr %32, align 8, !tbaa !17
  %34 = fdiv double %33, %6
  store double %34, ptr %32, align 8, !tbaa !17
  %35 = add nuw i64 %.01923, 1
  %exitcond30.not = icmp eq i64 %35, %10
  br i1 %exitcond30.not, label %._crit_edge24, label %.preheader, !llvm.loop !27

._crit_edge24:                                    ; preds = %._crit_edge.us, %.preheader, %middle.block, %.preheader21
  %36 = add nuw i64 %.02025, 1
  %exitcond31.not = icmp eq i64 %36, %4
  br i1 %exitcond31.not, label %._crit_edge, label %.preheader21, !llvm.loop !28
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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" }

!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 8}
!6 = !{!"", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !10, i64 24}
!14 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24}
!15 = !{!14, !10, i64 16}
!16 = !{!14, !7, i64 8}
!17 = !{!18, !18, i64 0}
!18 = !{!"double", !8, i64 0}
!19 = distinct !{!19, !20, !21, !22}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!"llvm.loop.isvectorized", i32 1}
!22 = !{!"llvm.loop.unroll.runtime.disable"}
!23 = !{!14, !7, i64 0}
!24 = distinct !{!24, !20, !21, !22}
!25 = distinct !{!25, !20, !22, !21}
!26 = distinct !{!26, !20}
!27 = distinct !{!27, !20, !22, !21}
!28 = distinct !{!28, !20}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass