; LLVM IR from xuxping/codelife :: algorthms/string (str_to_int)

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

@g_nStatus = external dso_local global i32, align 4

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

; 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

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

7:                                                ; preds = %1
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = load i8, ptr %8, align 1, !tbaa !13
  %10 = sext i8 %9 to i32
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %46

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %16, %12
  %14 = load ptr, ptr %2, align 8, !tbaa !5
  %15 = icmp eq ptr %14, inttoptr (i64 32 to ptr)
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i32 1
  store ptr %18, ptr %2, align 8, !tbaa !5
  br label %13, !llvm.loop !14

19:                                               ; preds = %13
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = load i8, ptr %20, align 1, !tbaa !13
  %22 = sext i8 %21 to i32
  %23 = icmp eq i32 %22, 43
  br i1 %23, label %24, label %27

24:                                               ; preds = %19
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i32 1
  store ptr %26, ptr %2, align 8, !tbaa !5
  br label %36

27:                                               ; preds = %19
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = load i8, ptr %28, align 1, !tbaa !13
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %30, 45
  br i1 %31, label %32, label %35

32:                                               ; preds = %27
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds i8, ptr %33, i32 1
  store ptr %34, ptr %2, align 8, !tbaa !5
  store i32 -1, ptr %4, align 4, !tbaa !9
  br label %35

35:                                               ; preds = %32, %27
  br label %36

36:                                               ; preds = %35, %24
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = load i8, ptr %37, align 1, !tbaa !13
  %39 = sext i8 %38 to i32
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %41, label %45

41:                                               ; preds = %36
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = load i32, ptr %4, align 4, !tbaa !9
  %44 = call i64 @StrToIntCore(ptr noundef %42, i32 noundef %43)
  store i64 %44, ptr %3, align 8, !tbaa !11
  br label %45

45:                                               ; preds = %41, %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %46

46:                                               ; preds = %45, %7, %1
  %47 = load i64, ptr %3, align 8, !tbaa !11
  %48 = trunc i64 %47 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret i32 %48
}

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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long long", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@g_nStatus = external dso_local local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local i32 @StrToInt(ptr noundef %0) local_unnamed_addr #0 {
  store i32 1, ptr @g_nStatus, align 4, !tbaa !5
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %13, label %2

2:                                                ; preds = %1
  %3 = load i8, ptr %0, align 1, !tbaa !9
  %.not16 = icmp eq i8 %3, 0
  br i1 %.not16, label %13, label %.preheader.preheader

.preheader.preheader:                             ; preds = %2
  %4 = icmp eq ptr %0, inttoptr (i64 32 to ptr)
  br i1 %4, label %.loopexit, label %5

.loopexit:                                        ; preds = %.preheader.preheader
  %.pre = load i8, ptr inttoptr (i64 33 to ptr), align 1, !tbaa !9
  br label %5

5:                                                ; preds = %.loopexit, %.preheader.preheader
  %6 = phi i8 [ %3, %.preheader.preheader ], [ %.pre, %.loopexit ]
  %.012.lcssa = phi ptr [ %0, %.preheader.preheader ], [ inttoptr (i64 33 to ptr), %.loopexit ]
  switch i8 %6, label %8 [
    i8 43, label %thread-pre-split
    i8 45, label %7
  ]

7:                                                ; preds = %5
  br label %thread-pre-split

thread-pre-split:                                 ; preds = %5, %7
  %.0.ph = phi i32 [ -1, %7 ], [ 1, %5 ]
  %.113.ph = getelementptr inbounds i8, ptr %.012.lcssa, i64 1
  %.pr = load i8, ptr %.113.ph, align 1, !tbaa !9
  br label %8

8:                                                ; preds = %thread-pre-split, %5
  %9 = phi i8 [ %.pr, %thread-pre-split ], [ %6, %5 ]
  %.113 = phi ptr [ %.113.ph, %thread-pre-split ], [ %.012.lcssa, %5 ]
  %.0 = phi i32 [ %.0.ph, %thread-pre-split ], [ 1, %5 ]
  %.not17 = icmp eq i8 %9, 0
  br i1 %.not17, label %13, label %10

10:                                               ; preds = %8
  %11 = tail call i64 @StrToIntCore(ptr noundef nonnull %.113, i32 noundef %.0)
  %12 = trunc i64 %11 to i32
  br label %13

13:                                               ; preds = %8, %10, %2, %1
  %.1 = phi i32 [ 0, %2 ], [ 0, %1 ], [ %12, %10 ], [ 0, %8 ]
  ret i32 %.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" }

!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 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopFullUnrollPass,LoopSimplifyPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass