; LLVM IR from 6leetcode/6leetcode :: questions/Algorithms/0179. Largest Number (c)

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

7:                                                ; preds = %13, %2
  %8 = load i64, ptr %5, align 8, !tbaa !9
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = load i32, ptr %9, align 4, !tbaa !11
  %11 = sext i32 %10 to i64
  %12 = icmp sle i64 %8, %11
  br i1 %12, label %13, label %16

13:                                               ; preds = %7
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = mul nsw i64 %14, 10
  store i64 %15, ptr %5, align 8, !tbaa !9
  br label %7, !llvm.loop !13

16:                                               ; preds = %7
  br label %17

17:                                               ; preds = %23, %16
  %18 = load i64, ptr %6, align 8, !tbaa !9
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load i32, ptr %19, align 4, !tbaa !11
  %21 = sext i32 %20 to i64
  %22 = icmp sle i64 %18, %21
  br i1 %22, label %23, label %26

23:                                               ; preds = %17
  %24 = load i64, ptr %6, align 8, !tbaa !9
  %25 = mul nsw i64 %24, 10
  store i64 %25, ptr %6, align 8, !tbaa !9
  br label %17, !llvm.loop !15

26:                                               ; preds = %17
  %27 = load i64, ptr %5, align 8, !tbaa !9
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = sext i32 %29 to i64
  %31 = mul nsw i64 %27, %30
  %32 = load ptr, ptr %3, align 8, !tbaa !5
  %33 = load i32, ptr %32, align 4, !tbaa !11
  %34 = sext i32 %33 to i64
  %35 = add nsw i64 %31, %34
  %36 = load i64, ptr %6, align 8, !tbaa !9
  %37 = load ptr, ptr %3, align 8, !tbaa !5
  %38 = load i32, ptr %37, align 4, !tbaa !11
  %39 = sext i32 %38 to i64
  %40 = mul nsw i64 %36, %39
  %41 = sub nsw i64 %35, %40
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i32, ptr %42, align 4, !tbaa !11
  %44 = sext i32 %43 to i64
  %45 = sub nsw i64 %41, %44
  %46 = trunc i64 %45 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret i32 %46
}

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, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}


; 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(argmem: read) uwtable
define dso_local i32 @cmp(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = load i32, ptr %0, align 4, !tbaa !5
  %4 = sext i32 %3 to i64
  %.not17 = icmp slt i32 %3, 10
  br i1 %.not17, label %.preheader, label %.lr.ph

.preheader:                                       ; preds = %.lr.ph, %2
  %.011.lcssa = phi i64 [ 10, %2 ], [ %7, %.lr.ph ]
  %5 = load i32, ptr %1, align 4, !tbaa !5
  %6 = sext i32 %5 to i64
  %.not1320 = icmp slt i32 %5, 10
  br i1 %.not1320, label %._crit_edge, label %.lr.ph22

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.01118 = phi i64 [ %7, %.lr.ph ], [ 10, %2 ]
  %7 = mul nuw nsw i64 %.01118, 10
  %.not = icmp sgt i64 %7, %4
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !9

.lr.ph22:                                         ; preds = %.preheader, %.lr.ph22
  %.021 = phi i64 [ %8, %.lr.ph22 ], [ 10, %.preheader ]
  %8 = mul nuw nsw i64 %.021, 10
  %.not13 = icmp sgt i64 %8, %6
  br i1 %.not13, label %._crit_edge.loopexit, label %.lr.ph22, !llvm.loop !11

._crit_edge.loopexit:                             ; preds = %.lr.ph22
  %9 = sub nsw i64 1, %8
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %.0.lcssa = phi i64 [ -9, %.preheader ], [ %9, %._crit_edge.loopexit ]
  %10 = mul nsw i64 %.011.lcssa, %6
  %.neg15 = mul i64 %.0.lcssa, %4
  %11 = sub i64 %.neg15, %6
  %12 = add i64 %11, %10
  %13 = trunc i64 %12 to i32
  ret i32 %13
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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, !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"}
!11 = distinct !{!11, !10}


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