; LLVM IR from int32bit/leetcode :: algorithms/DivideTwoIntegers (solve)

; 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"

; Function Attrs: nounwind uwtable
declare dso_local i32 @getSign(i64 noundef, i64 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @divide(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp eq i32 %13, 0
  br i1 %14, label %15, label %20

15:                                               ; preds = %2
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = icmp sge i32 %16, 0
  %18 = zext i1 %17 to i64
  %19 = select i1 %17, i32 2147483647, i32 -2147483648
  store i32 %19, ptr %3, align 4
  br label %76

20:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  store i64 %22, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  store i64 %24, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %25 = load i64, ptr %6, align 8, !tbaa !9
  %26 = load i64, ptr %7, align 8, !tbaa !9
  %27 = call i32 @getSign(i64 noundef %25, i64 noundef %26)
  store i32 %27, ptr %8, align 4, !tbaa !5
  %28 = load i64, ptr %6, align 8, !tbaa !9
  %29 = call i64 @llvm.abs.i64(i64 %28, i1 true)
  store i64 %29, ptr %6, align 8, !tbaa !9
  %30 = load i64, ptr %7, align 8, !tbaa !9
  %31 = call i64 @llvm.abs.i64(i64 %30, i1 true)
  store i64 %31, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  br label %32

32:                                               ; preds = %48, %20
  %33 = load i64, ptr %6, align 8, !tbaa !9
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = icmp sge i64 %33, %34
  br i1 %35, label %36, label %55

36:                                               ; preds = %32
  %37 = load i64, ptr %7, align 8, !tbaa !9
  store i64 %37, ptr %10, align 8, !tbaa !9
  store i64 1, ptr %11, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %43, %36
  %39 = load i64, ptr %6, align 8, !tbaa !9
  %40 = load i64, ptr %10, align 8, !tbaa !9
  %41 = shl i64 %40, 1
  %42 = icmp sgt i64 %39, %41
  br i1 %42, label %43, label %48

43:                                               ; preds = %38
  %44 = load i64, ptr %10, align 8, !tbaa !9
  %45 = shl i64 %44, 1
  store i64 %45, ptr %10, align 8, !tbaa !9
  %46 = load i64, ptr %11, align 8, !tbaa !9
  %47 = shl i64 %46, 1
  store i64 %47, ptr %11, align 8, !tbaa !9
  br label %38, !llvm.loop !11

48:                                               ; preds = %38
  %49 = load i64, ptr %10, align 8, !tbaa !9
  %50 = load i64, ptr %6, align 8, !tbaa !9
  %51 = sub nsw i64 %50, %49
  store i64 %51, ptr %6, align 8, !tbaa !9
  %52 = load i64, ptr %11, align 8, !tbaa !9
  %53 = load i64, ptr %9, align 8, !tbaa !9
  %54 = add nsw i64 %53, %52
  store i64 %54, ptr %9, align 8, !tbaa !9
  br label %32, !llvm.loop !13

55:                                               ; preds = %32
  %56 = load i32, ptr %8, align 4, !tbaa !5
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %58, label %61

58:                                               ; preds = %55
  %59 = load i64, ptr %9, align 8, !tbaa !9
  %60 = sub nsw i64 0, %59
  br label %63

61:                                               ; preds = %55
  %62 = load i64, ptr %9, align 8, !tbaa !9
  br label %63

63:                                               ; preds = %61, %58
  %64 = phi i64 [ %60, %58 ], [ %62, %61 ]
  store i64 %64, ptr %9, align 8, !tbaa !9
  %65 = load i64, ptr %9, align 8, !tbaa !9
  %66 = icmp sgt i64 %65, 2147483647
  br i1 %66, label %67, label %68

67:                                               ; preds = %63
  store i32 2147483647, ptr %3, align 4
  store i32 1, ptr %12, align 4
  br label %75

68:                                               ; preds = %63
  %69 = load i64, ptr %9, align 8, !tbaa !9
  %70 = icmp slt i64 %69, -2147483648
  br i1 %70, label %71, label %72

71:                                               ; preds = %68
  store i32 -2147483648, ptr %3, align 4
  store i32 1, ptr %12, align 4
  br label %75

72:                                               ; preds = %68
  %73 = load i64, ptr %9, align 8, !tbaa !9
  %74 = trunc i64 %73 to i32
  store i32 %74, ptr %3, align 4
  store i32 1, ptr %12, align 4
  br label %75

75:                                               ; preds = %72, %71, %67
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %76

76:                                               ; preds = %75, %15
  %77 = load i32, ptr %3, align 4
  ret i32 %77
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.abs.i64(i64, i1 immarg) #2

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

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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 i32 @getSign(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @divide(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = icmp sgt i32 %0, -1
  %6 = select i1 %5, i32 2147483647, i32 -2147483648
  br label %24

7:                                                ; preds = %2
  %8 = sext i32 %0 to i64
  %9 = sext i32 %1 to i64
  %10 = tail call i32 @getSign(i64 noundef %8, i64 noundef %9)
  %11 = tail call i64 @llvm.abs.i64(i64 %8, i1 true)
  %12 = tail call i64 @llvm.abs.i64(i64 %9, i1 true)
  %.not36 = icmp ult i64 %11, %12
  br i1 %.not36, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %7, %17
  %.02738 = phi i64 [ %19, %17 ], [ 0, %7 ]
  %.02937 = phi i64 [ %18, %17 ], [ %11, %7 ]
  br label %13

13:                                               ; preds = %13, %.preheader
  %.026 = phi i64 [ %14, %13 ], [ %12, %.preheader ]
  %.0 = phi i64 [ %16, %13 ], [ 1, %.preheader ]
  %14 = shl nuw nsw i64 %.026, 1
  %15 = icmp sgt i64 %.02937, %14
  %16 = shl i64 %.0, 1
  br i1 %15, label %13, label %17, !llvm.loop !5

17:                                               ; preds = %13
  %18 = sub nsw i64 %.02937, %.026
  %19 = add nsw i64 %.0, %.02738
  %.not = icmp slt i64 %18, %12
  br i1 %.not, label %._crit_edge, label %.preheader, !llvm.loop !7

._crit_edge:                                      ; preds = %17, %7
  %.027.lcssa = phi i64 [ 0, %7 ], [ %19, %17 ]
  %.not34 = icmp eq i32 %10, 0
  %20 = sub nsw i64 0, %.027.lcssa
  %21 = select i1 %.not34, i64 %.027.lcssa, i64 %20
  %22 = icmp sgt i64 %21, 2147483647
  br i1 %22, label %24, label %23

23:                                               ; preds = %._crit_edge
  %spec.select35 = tail call i64 @llvm.smax.i64(i64 %21, i64 -2147483648)
  %spec.select = trunc i64 %spec.select35 to i32
  br label %24

24:                                               ; preds = %23, %._crit_edge, %4
  %.1 = phi i32 [ %6, %4 ], [ 2147483647, %._crit_edge ], [ %spec.select, %23 ]
  ret i32 %.1
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.abs.i64(i64, i1 immarg) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smax.i64(i64, i64) #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 speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass