; LLVM IR from xuxping/codelife :: leetcode (7_reverse-integer)

; 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
define dso_local i32 @reverse(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 2147483647, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 -2147483648, ptr %7, align 4, !tbaa !5
  br label %9

9:                                                ; preds = %44, %1
  %10 = load i32, ptr %3, align 4, !tbaa !5
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %49

12:                                               ; preds = %9
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = srem i32 %13, 10
  store i32 %14, ptr %5, align 4, !tbaa !5
  %15 = load i32, ptr %3, align 4, !tbaa !5
  %16 = sdiv i32 %15, 10
  store i32 %16, ptr %3, align 4, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !5
  %18 = load i32, ptr %6, align 4, !tbaa !5
  %19 = sdiv i32 %18, 10
  %20 = icmp sgt i32 %17, %19
  br i1 %20, label %29, label %21

21:                                               ; preds = %12
  %22 = load i32, ptr %4, align 4, !tbaa !5
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = sdiv i32 %23, 10
  %25 = icmp eq i32 %22, %24
  br i1 %25, label %26, label %30

26:                                               ; preds = %21
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = icmp sgt i32 %27, 7
  br i1 %28, label %29, label %30

29:                                               ; preds = %26, %12
  store i32 0, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %51

30:                                               ; preds = %26, %21
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = load i32, ptr %7, align 4, !tbaa !5
  %33 = sdiv i32 %32, 10
  %34 = icmp slt i32 %31, %33
  br i1 %34, label %43, label %35

35:                                               ; preds = %30
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = load i32, ptr %7, align 4, !tbaa !5
  %38 = sdiv i32 %37, 10
  %39 = icmp eq i32 %36, %38
  br i1 %39, label %40, label %44

40:                                               ; preds = %35
  %41 = load i32, ptr %5, align 4, !tbaa !5
  %42 = icmp slt i32 %41, -8
  br i1 %42, label %43, label %44

43:                                               ; preds = %40, %30
  store i32 0, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %51

44:                                               ; preds = %40, %35
  %45 = load i32, ptr %4, align 4, !tbaa !5
  %46 = mul nsw i32 %45, 10
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = add nsw i32 %46, %47
  store i32 %48, ptr %4, align 4, !tbaa !5
  br label %9, !llvm.loop !9

49:                                               ; preds = %9
  %50 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %50, ptr %2, align 4
  store i32 1, ptr %8, align 4
  br label %51

51:                                               ; preds = %49, %43, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %52 = load i32, ptr %2, align 4
  ret i32 %52
}

; 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 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 = { 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 = distinct !{!9, !10}
!10 = !{!"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: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local i32 @reverse(i32 noundef %0) local_unnamed_addr #0 {
  %.not23 = icmp eq i32 %0, 0
  br i1 %.not23, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %12
  %.02025 = phi i32 [ %3, %12 ], [ %0, %1 ]
  %.02124 = phi i32 [ %14, %12 ], [ 0, %1 ]
  %2 = srem i32 %.02025, 10
  %3 = sdiv i32 %.02025, 10
  %4 = icmp sgt i32 %.02124, 214748364
  br i1 %4, label %._crit_edge, label %5

5:                                                ; preds = %.lr.ph
  %6 = icmp eq i32 %.02124, 214748364
  %7 = icmp sgt i32 %2, 7
  %or.cond = and i1 %6, %7
  %8 = icmp slt i32 %.02124, -214748364
  %or.cond22 = or i1 %8, %or.cond
  br i1 %or.cond22, label %._crit_edge, label %9

9:                                                ; preds = %5
  %10 = icmp eq i32 %.02124, -214748364
  %11 = icmp slt i32 %2, -8
  %or.cond3 = and i1 %10, %11
  br i1 %or.cond3, label %._crit_edge, label %12

12:                                               ; preds = %9
  %13 = mul nsw i32 %.02124, 10
  %14 = add nsw i32 %2, %13
  %.02025.off = add i32 %.02025, 9
  %.not = icmp ult i32 %.02025.off, 19
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %5, %.lr.ph, %9, %12, %1
  %.0 = phi i32 [ 0, %1 ], [ %14, %12 ], [ 0, %9 ], [ 0, %.lr.ph ], [ 0, %5 ]
  ret i32 %.0
}

attributes #0 = { nofree norecurse nosync nounwind memory(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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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