; LLVM IR from JessonYue/LeetCodeLearning :: Array/yangxin/第七周练习7_13-7_19/405. 数字转换为十六进制数 (toHex)

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

@__const.toHex.map = external hidden unnamed_addr constant [16 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local ptr @toHex(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca [16 x i8], align 16
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %3, ptr align 16 @__const.toHex.map, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %8 = call noalias ptr @calloc(i64 noundef 9, i64 noundef 1) #5
  store ptr %8, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 7, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  store i32 8, ptr %7, align 4, !tbaa !5
  br label %9

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

12:                                               ; preds = %9
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, 0
  br label %15

15:                                               ; preds = %12, %9
  %16 = phi i1 [ false, %9 ], [ %14, %12 ]
  br i1 %16, label %17, label %33

17:                                               ; preds = %15
  %18 = load i32, ptr %2, align 4, !tbaa !5
  %19 = and i32 %18, 15
  store i32 %19, ptr %6, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [16 x i8], ptr %3, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !11
  %24 = load ptr, ptr %4, align 8, !tbaa !9
  %25 = load i32, ptr %5, align 4, !tbaa !5
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %25 to i64
  %28 = getelementptr inbounds i8, ptr %24, i64 %27
  store i8 %23, ptr %28, align 1, !tbaa !11
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = ashr i32 %29, 4
  store i32 %30, ptr %2, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = add nsw i32 %31, -1
  store i32 %32, ptr %7, align 4, !tbaa !5
  br label %9, !llvm.loop !12

33:                                               ; preds = %15
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 7
  br i1 %35, label %36, label %42

36:                                               ; preds = %33
  %37 = load ptr, ptr %4, align 8, !tbaa !9
  %38 = load i32, ptr %5, align 4, !tbaa !5
  %39 = add nsw i32 %38, -1
  store i32 %39, ptr %5, align 4, !tbaa !5
  %40 = sext i32 %38 to i64
  %41 = getelementptr inbounds i8, ptr %37, i64 %40
  store i8 48, ptr %41, align 1, !tbaa !11
  br label %42

42:                                               ; preds = %36, %33
  %43 = load ptr, ptr %4, align 8, !tbaa !9
  %44 = load i32, ptr %5, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i8, ptr %43, i64 %45
  %47 = getelementptr inbounds i8, ptr %46, i64 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #4
  ret ptr %47
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #3

; 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 nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind allocsize(0,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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0,1) }

!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 = !{!"any pointer", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@__const.toHex.map = external hidden unnamed_addr constant [16 x i8], align 16

; Function Attrs: nofree nounwind memory(write, argmem: none, inaccessiblemem: readwrite) uwtable
define dso_local noalias nonnull ptr @toHex(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(9) ptr @calloc(i64 noundef 9, i64 noundef 1) #2
  %.not = icmp eq i32 %0, 0
  br i1 %.not, label %._crit_edge.thread, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %3 = and i32 %0, 15
  %4 = zext nneg i32 %3 to i64
  %5 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %4
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %2, i64 7
  store i8 %6, ptr %7, align 1, !tbaa !5
  %8 = icmp ugt i32 %0, 15
  br i1 %8, label %.lr.ph.1, label %._crit_edge, !llvm.loop !8

.lr.ph.1:                                         ; preds = %.lr.ph
  %9 = ashr i32 %0, 4
  %10 = and i32 %9, 15
  %11 = zext nneg i32 %10 to i64
  %12 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %2, i64 6
  store i8 %13, ptr %14, align 1, !tbaa !5
  %15 = icmp ugt i32 %9, 15
  br i1 %15, label %.lr.ph.2, label %._crit_edge, !llvm.loop !8

.lr.ph.2:                                         ; preds = %.lr.ph.1
  %16 = ashr i32 %0, 8
  %17 = and i32 %16, 15
  %18 = zext nneg i32 %17 to i64
  %19 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %2, i64 5
  store i8 %20, ptr %21, align 1, !tbaa !5
  %22 = icmp ugt i32 %16, 15
  br i1 %22, label %.lr.ph.3, label %._crit_edge, !llvm.loop !8

.lr.ph.3:                                         ; preds = %.lr.ph.2
  %23 = ashr i32 %0, 12
  %24 = and i32 %23, 15
  %25 = zext nneg i32 %24 to i64
  %26 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !5
  %28 = getelementptr inbounds i8, ptr %2, i64 4
  store i8 %27, ptr %28, align 1, !tbaa !5
  %29 = icmp ugt i32 %23, 15
  br i1 %29, label %.lr.ph.4, label %._crit_edge, !llvm.loop !8

.lr.ph.4:                                         ; preds = %.lr.ph.3
  %30 = ashr i32 %0, 16
  %31 = and i32 %30, 15
  %32 = zext nneg i32 %31 to i64
  %33 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %35 = getelementptr inbounds i8, ptr %2, i64 3
  store i8 %34, ptr %35, align 1, !tbaa !5
  %36 = icmp ugt i32 %30, 15
  br i1 %36, label %.lr.ph.5, label %._crit_edge, !llvm.loop !8

.lr.ph.5:                                         ; preds = %.lr.ph.4
  %37 = ashr i32 %0, 20
  %38 = and i32 %37, 15
  %39 = zext nneg i32 %38 to i64
  %40 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %39
  %41 = load i8, ptr %40, align 1, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %2, i64 2
  store i8 %41, ptr %42, align 1, !tbaa !5
  %43 = icmp ugt i32 %37, 15
  br i1 %43, label %.lr.ph.6, label %._crit_edge, !llvm.loop !8

.lr.ph.6:                                         ; preds = %.lr.ph.5
  %44 = ashr i32 %0, 24
  %45 = and i32 %44, 15
  %46 = zext nneg i32 %45 to i64
  %47 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %2, i64 1
  store i8 %48, ptr %49, align 1, !tbaa !5
  %50 = icmp ugt i32 %44, 15
  br i1 %50, label %.lr.ph.7, label %._crit_edge, !llvm.loop !8

.lr.ph.7:                                         ; preds = %.lr.ph.6
  %51 = lshr i32 %0, 28
  %52 = zext nneg i32 %51 to i64
  %53 = getelementptr inbounds [16 x i8], ptr @__const.toHex.map, i64 0, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !5
  store i8 %54, ptr %2, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge.thread:                               ; preds = %1
  %55 = getelementptr inbounds i8, ptr %2, i64 7
  store i8 48, ptr %55, align 1, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph.1, %.lr.ph.2, %.lr.ph.3, %.lr.ph.4, %.lr.ph.5, %.lr.ph.6, %.lr.ph.7, %._crit_edge.thread
  %.1 = phi i64 [ 25769803776, %._crit_edge.thread ], [ 25769803776, %.lr.ph ], [ 21474836480, %.lr.ph.1 ], [ 17179869184, %.lr.ph.2 ], [ 12884901888, %.lr.ph.3 ], [ 8589934592, %.lr.ph.4 ], [ 4294967296, %.lr.ph.5 ], [ 0, %.lr.ph.6 ], [ -4294967296, %.lr.ph.7 ]
  %56 = ashr exact i64 %.1, 32
  %57 = getelementptr inbounds i8, ptr %2, i64 %56
  %58 = getelementptr inbounds i8, ptr %57, i64 1
  ret ptr %58
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #1

attributes #0 = { nofree nounwind memory(write, argmem: none, inaccessiblemem: readwrite) 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 nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0,1) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass