; LLVM IR from xing1357/SimpleOS :: src (string)

; 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
declare dso_local i32 @strlen(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @str_to_int(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = call i32 @strlen(ptr noundef %7)
  store i32 %8, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %9 = load i32, ptr %5, align 4, !tbaa !9
  %10 = sub nsw i32 %9, 1
  store i32 %10, ptr %6, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %28, %1
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = icmp sge i32 %12, 0
  br i1 %13, label %14, label %31

14:                                               ; preds = %11
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = load i32, ptr %6, align 4, !tbaa !9
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i8, ptr %15, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !11
  %20 = sext i8 %19 to i32
  %21 = sub nsw i32 %20, 48
  %22 = load i32, ptr %4, align 4, !tbaa !9
  %23 = mul nsw i32 %21, %22
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = add nsw i32 %24, %23
  store i32 %25, ptr %3, align 4, !tbaa !9
  %26 = load i32, ptr %4, align 4, !tbaa !9
  %27 = mul nsw i32 %26, 10
  store i32 %27, ptr %4, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %14
  %29 = load i32, ptr %6, align 4, !tbaa !9
  %30 = add nsw i32 %29, -1
  store i32 %30, ptr %6, align 4, !tbaa !9
  br label %11, !llvm.loop !12

31:                                               ; preds = %11
  %32 = load i32, ptr %3, align 4, !tbaa !9
  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
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %32
}

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 = !{!"int", !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"

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

; Function Attrs: nounwind uwtable
define dso_local i32 @str_to_int(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call i32 @strlen(ptr noundef %0)
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %4 = zext nneg i32 %2 to i64
  %xtraiter = and i64 %4, 3
  %5 = icmp ult i32 %2, 4
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %4, 2147483644
  %invariant.gep = getelementptr i8, ptr %0, i64 -1
  %invariant.gep19 = getelementptr i8, ptr %0, i64 -2
  %invariant.gep21 = getelementptr i8, ptr %0, i64 -3
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ %4, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.0912 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %29, %.lr.ph ]
  %.01011 = phi i32 [ 1, %.lr.ph.preheader.new ], [ %30, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %gep = getelementptr i8, ptr %invariant.gep, i64 %indvars.iv
  %6 = load i8, ptr %gep, align 1, !tbaa !5
  %7 = sext i8 %6 to i32
  %8 = add nsw i32 %7, -48
  %9 = mul nsw i32 %8, %.01011
  %10 = add nsw i32 %9, %.0912
  %11 = mul nuw nsw i32 %.01011, 10
  %gep20 = getelementptr i8, ptr %invariant.gep19, i64 %indvars.iv
  %12 = load i8, ptr %gep20, align 1, !tbaa !5
  %13 = sext i8 %12 to i32
  %14 = add nsw i32 %13, -48
  %15 = mul nsw i32 %14, %11
  %16 = add nsw i32 %15, %10
  %17 = mul nuw i32 %.01011, 100
  %gep22 = getelementptr i8, ptr %invariant.gep21, i64 %indvars.iv
  %18 = load i8, ptr %gep22, align 1, !tbaa !5
  %19 = sext i8 %18 to i32
  %20 = add nsw i32 %19, -48
  %21 = mul nsw i32 %20, %17
  %22 = add nsw i32 %21, %16
  %23 = mul nuw i32 %.01011, 1000
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, -4
  %24 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.3
  %25 = load i8, ptr %24, align 1, !tbaa !5
  %26 = sext i8 %25 to i32
  %27 = add nsw i32 %26, -48
  %28 = mul nsw i32 %27, %23
  %29 = add nsw i32 %28, %22
  %30 = mul nuw i32 %.01011, 10000
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3.not = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3.not, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !8

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %29, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ %4, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %.0912.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %29, %.lr.ph ]
  %.01011.unr = phi i32 [ 1, %.lr.ph.preheader ], [ %30, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.0912.epil = phi i32 [ %36, %.lr.ph.epil ], [ %.0912.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.01011.epil = phi i32 [ %37, %.lr.ph.epil ], [ %.01011.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %indvars.iv.next.epil = add nsw i64 %indvars.iv.epil, -1
  %31 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next.epil
  %32 = load i8, ptr %31, align 1, !tbaa !5
  %33 = sext i8 %32 to i32
  %34 = add nsw i32 %33, -48
  %35 = mul nsw i32 %34, %.01011.epil
  %36 = add nsw i32 %35, %.0912.epil
  %37 = mul nuw nsw i32 %.01011.epil, 10
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !10

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %1
  %.09.lcssa = phi i32 [ 0, %1 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %36, %.lr.ph.epil ]
  ret i32 %.09.lcssa
}

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


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