; LLVM IR from Montimage/mmt-security :: src/lib/expression.ll (parse_a_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"

@.str.3 = external hidden unnamed_addr constant [45 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [1 x i8], align 1

; 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: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #1

declare void @mmt_sec_log(i32 noundef, ptr noundef, ...) #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @_jump_space(ptr noundef, i64 noundef) #4

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @mmt_mem_dup(ptr noundef, i64 noundef) #4

; Function Attrs: nounwind uwtable
define dso_local i64 @_parse_a_string(ptr noundef %0, ptr noundef %1, i64 noundef %2) #5 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  store i64 0, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  store i64 0, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr null, ptr %12, align 8, !tbaa !5
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %18, label %15

15:                                               ; preds = %3
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = icmp eq i64 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %15, %3
  store i64 0, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %98

19:                                               ; preds = %15
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = load i64, ptr %7, align 8, !tbaa !9
  %22 = call i64 @_jump_space(ptr noundef %20, i64 noundef %21)
  store i64 %22, ptr %9, align 8, !tbaa !9
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = load i64, ptr %7, align 8, !tbaa !9
  %25 = icmp eq i64 %23, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %19
  %27 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %27, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %98

28:                                               ; preds = %19
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = load i64, ptr %9, align 8, !tbaa !9
  %31 = getelementptr inbounds i8, ptr %29, i64 %30
  store ptr %31, ptr %10, align 8, !tbaa !5
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = sext i8 %33 to i32
  %35 = icmp ne i32 %34, 34
  br i1 %35, label %36, label %43

36:                                               ; preds = %28
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = load i8, ptr %37, align 1, !tbaa !11
  %39 = sext i8 %38 to i32
  %40 = icmp ne i32 %39, 39
  br i1 %40, label %41, label %43

41:                                               ; preds = %36
  %42 = load i64, ptr %9, align 8, !tbaa !9
  store i64 %42, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %98

43:                                               ; preds = %36, %28
  store i64 1, ptr %8, align 8, !tbaa !9
  %44 = load i64, ptr %9, align 8, !tbaa !9
  %45 = add i64 %44, 1
  store i64 %45, ptr %9, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %62, %43
  %47 = load ptr, ptr %10, align 8, !tbaa !5
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = getelementptr inbounds i8, ptr %47, i64 %48
  %50 = load i8, ptr %49, align 1, !tbaa !11
  %51 = sext i8 %50 to i32
  %52 = load ptr, ptr %10, align 8, !tbaa !5
  %53 = load i8, ptr %52, align 1, !tbaa !11
  %54 = sext i8 %53 to i32
  %55 = icmp ne i32 %51, %54
  br i1 %55, label %56, label %60

56:                                               ; preds = %46
  %57 = load i64, ptr %9, align 8, !tbaa !9
  %58 = load i64, ptr %7, align 8, !tbaa !9
  %59 = icmp ult i64 %57, %58
  br label %60

60:                                               ; preds = %56, %46
  %61 = phi i1 [ false, %46 ], [ %59, %56 ]
  br i1 %61, label %62, label %67

62:                                               ; preds = %60
  %63 = load i64, ptr %8, align 8, !tbaa !9
  %64 = add i64 %63, 1
  store i64 %64, ptr %8, align 8, !tbaa !9
  %65 = load i64, ptr %9, align 8, !tbaa !9
  %66 = add i64 %65, 1
  store i64 %66, ptr %9, align 8, !tbaa !9
  br label %46, !llvm.loop !12

67:                                               ; preds = %60
  br label %68

68:                                               ; preds = %67
  %69 = load i64, ptr %9, align 8, !tbaa !9
  %70 = load i64, ptr %7, align 8, !tbaa !9
  %71 = icmp ult i64 %69, %70
  %72 = xor i1 %71, true
  %73 = xor i1 %72, true
  %74 = xor i1 %73, true
  %75 = zext i1 %74 to i32
  %76 = sext i32 %75 to i64
  %77 = call i64 @llvm.expect.i64(i64 %76, i64 0)
  %78 = icmp ne i64 %77, 0
  br i1 %78, label %79, label %81

79:                                               ; preds = %68
  %80 = load ptr, ptr %10, align 8, !tbaa !5
  call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef @.str.3, ptr noundef %80)
  call void @exit(i32 noundef 0) #7
  unreachable

81:                                               ; preds = %68
  %82 = load i64, ptr %8, align 8, !tbaa !9
  %83 = sub i64 %82, 1
  %84 = icmp eq i64 %83, 0
  br i1 %84, label %85, label %88

85:                                               ; preds = %81
  %86 = call ptr @mmt_mem_dup(ptr noundef @.str.4, i64 noundef 1)
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %86, ptr %87, align 8, !tbaa !5
  br label %95

88:                                               ; preds = %81
  %89 = load ptr, ptr %10, align 8, !tbaa !5
  %90 = getelementptr inbounds i8, ptr %89, i64 1
  %91 = load i64, ptr %8, align 8, !tbaa !9
  %92 = sub i64 %91, 1
  %93 = call ptr @mmt_mem_dup(ptr noundef %90, i64 noundef %92)
  %94 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %93, ptr %94, align 8, !tbaa !5
  br label %95

95:                                               ; preds = %88, %85
  %96 = load i64, ptr %9, align 8, !tbaa !9
  %97 = add i64 %96, 1
  store i64 %97, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %98

98:                                               ; preds = %95, %41, %26, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  %99 = load i64, ptr %4, align 8
  ret i64 %99
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #2 = { "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 #3 = { noreturn nounwind "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 = { inlinehint 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { noreturn 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 = !{!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"

@.str.3 = external hidden unnamed_addr constant [45 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [1 x i8], align 1

declare void @mmt_sec_log(i32 noundef, ptr noundef, ...) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @_jump_space(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @mmt_mem_dup(ptr noundef, i64 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local i64 @_parse_a_string(ptr nocapture noundef writeonly %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #3 {
  store ptr null, ptr %0, align 8, !tbaa !5
  %4 = icmp eq ptr %1, null
  %5 = icmp eq i64 %2, 0
  %or.cond = or i1 %4, %5
  br i1 %or.cond, label %31, label %6

6:                                                ; preds = %3
  %7 = tail call i64 @_jump_space(ptr noundef nonnull %1, i64 noundef %2)
  %8 = icmp eq i64 %7, %2
  br i1 %8, label %31, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %1, i64 %7
  %11 = load i8, ptr %10, align 1, !tbaa !9
  switch i8 %11, label %31 [
    i8 34, label %.preheader
    i8 39, label %.preheader
  ]

.preheader:                                       ; preds = %9, %9
  br label %12

12:                                               ; preds = %.preheader, %12
  %.032 = phi i64 [ %18, %12 ], [ 1, %.preheader ]
  %.031.in = phi i64 [ %.031, %12 ], [ %7, %.preheader ]
  %.031 = add i64 %.031.in, 1
  %13 = getelementptr inbounds i8, ptr %10, i64 %.032
  %14 = load i8, ptr %13, align 1, !tbaa !9
  %15 = icmp ne i8 %14, %11
  %16 = icmp ult i64 %.031, %2
  %17 = select i1 %15, i1 %16, i1 false
  %18 = add i64 %.032, 1
  br i1 %17, label %12, label %19, !llvm.loop !10

19:                                               ; preds = %12
  br i1 %16, label %21, label %20, !prof !12

20:                                               ; preds = %19
  tail call void (i32, ptr, ...) @mmt_sec_log(i32 noundef 4, ptr noundef nonnull @.str.3, ptr noundef nonnull %10) #4
  tail call void @exit(i32 noundef 0) #5
  unreachable

21:                                               ; preds = %19
  %22 = add i64 %.032, -1
  %23 = icmp eq i64 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %21
  %25 = tail call ptr @mmt_mem_dup(ptr noundef nonnull @.str.4, i64 noundef 1)
  br label %29

26:                                               ; preds = %21
  %27 = getelementptr inbounds i8, ptr %10, i64 1
  %28 = tail call ptr @mmt_mem_dup(ptr noundef nonnull %27, i64 noundef %22)
  br label %29

29:                                               ; preds = %26, %24
  %storemerge = phi ptr [ %28, %26 ], [ %25, %24 ]
  store ptr %storemerge, ptr %0, align 8, !tbaa !5
  %30 = add i64 %.031.in, 2
  br label %31

31:                                               ; preds = %9, %6, %3, %29
  %.0 = phi i64 [ %30, %29 ], [ 0, %3 ], [ %2, %6 ], [ %7, %9 ]
  ret i64 %.0
}

attributes #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 = { noreturn nounwind "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 = { inlinehint 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 #3 = { 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 #4 = { nounwind }
attributes #5 = { noreturn 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = !{!"branch_weights", !"expected", i32 2000, i32 1}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass