; LLVM IR from russellsayshi/CeeBeeServer :: main (read_string_until)

; 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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

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

17:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %72

18:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  store i64 0, ptr %11, align 8, !tbaa !10
  br label %19

19:                                               ; preds = %63, %18
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = load i64, ptr %11, align 8, !tbaa !10
  %22 = getelementptr inbounds i8, ptr %20, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %24 = sext i8 %23 to i32
  %25 = load i8, ptr %6, align 1, !tbaa !9
  %26 = sext i8 %25 to i32
  %27 = icmp ne i32 %24, %26
  br i1 %27, label %28, label %35

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

35:                                               ; preds = %28, %19
  %36 = phi i1 [ false, %19 ], [ %34, %28 ]
  br i1 %36, label %37, label %66

37:                                               ; preds = %35
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = load i64, ptr %11, align 8, !tbaa !10
  %40 = getelementptr inbounds i8, ptr %38, i64 %39
  %41 = load i8, ptr %40, align 1, !tbaa !9
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = load i64, ptr %11, align 8, !tbaa !10
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  store i8 %41, ptr %44, align 1, !tbaa !9
  %45 = load i64, ptr %11, align 8, !tbaa !10
  %46 = load i64, ptr %8, align 8, !tbaa !10
  %47 = sub i64 %46, 2
  %48 = icmp uge i64 %45, %47
  br i1 %48, label %49, label %59

49:                                               ; preds = %37
  %50 = load i64, ptr %8, align 8, !tbaa !10
  %51 = mul i64 %50, 2
  store i64 %51, ptr %8, align 8, !tbaa !10
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = load i64, ptr %8, align 8, !tbaa !10
  %54 = call ptr @realloc(ptr noundef %52, i64 noundef %53) #6
  store ptr %54, ptr %9, align 8, !tbaa !5
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = icmp eq ptr %55, null
  br i1 %56, label %57, label %58

57:                                               ; preds = %49
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %71

58:                                               ; preds = %49
  br label %59

59:                                               ; preds = %58, %37
  %60 = load ptr, ptr %7, align 8, !tbaa !5
  %61 = load i64, ptr %60, align 8, !tbaa !10
  %62 = add i64 %61, 1
  store i64 %62, ptr %60, align 8, !tbaa !10
  br label %63

63:                                               ; preds = %59
  %64 = load i64, ptr %11, align 8, !tbaa !10
  %65 = add i64 %64, 1
  store i64 %65, ptr %11, align 8, !tbaa !10
  br label %19, !llvm.loop !12

66:                                               ; preds = %35
  %67 = load ptr, ptr %9, align 8, !tbaa !5
  %68 = load i64, ptr %11, align 8, !tbaa !10
  %69 = getelementptr inbounds i8, ptr %67, i64 %68
  store i8 0, ptr %69, align 1, !tbaa !9
  %70 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %70, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %71

71:                                               ; preds = %66, %57
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  br label %72

72:                                               ; preds = %71, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %73 = load ptr, ptr %4, align 8
  ret ptr %73
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 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 allocsize(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) }
attributes #6 = { nounwind allocsize(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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !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: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noalias ptr @read_string_until(ptr nocapture noundef readonly %0, i8 noundef signext %1, ptr nocapture noundef %2) local_unnamed_addr #1 {
  %4 = tail call noalias dereferenceable_or_null(40) ptr @malloc(i64 noundef 40) #3
  %5 = icmp eq ptr %4, null
  br i1 %5, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %6 = load i8, ptr %0, align 1, !tbaa !5
  %.not34 = icmp eq i8 %6, %1
  %.not3035 = icmp eq i8 %6, 0
  %or.cond36 = or i1 %.not34, %.not3035
  br i1 %or.cond36, label %.critedge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %14
  %7 = phi i8 [ %19, %14 ], [ %6, %.preheader ]
  %.039 = phi i64 [ %17, %14 ], [ 0, %.preheader ]
  %.02238 = phi ptr [ %.1, %14 ], [ %4, %.preheader ]
  %.02337 = phi i64 [ %.124, %14 ], [ 40, %.preheader ]
  %8 = getelementptr inbounds i8, ptr %.02238, i64 %.039
  store i8 %7, ptr %8, align 1, !tbaa !5
  %9 = add i64 %.02337, -2
  %.not31 = icmp ult i64 %.039, %9
  br i1 %.not31, label %14, label %10

10:                                               ; preds = %.lr.ph
  %11 = shl i64 %.02337, 1
  %12 = tail call ptr @realloc(ptr noundef nonnull %.02238, i64 noundef %11) #4
  %13 = icmp eq ptr %12, null
  br i1 %13, label %.loopexit, label %14

14:                                               ; preds = %10, %.lr.ph
  %.124 = phi i64 [ %11, %10 ], [ %.02337, %.lr.ph ]
  %.1 = phi ptr [ %12, %10 ], [ %.02238, %.lr.ph ]
  %15 = load i64, ptr %2, align 8, !tbaa !8
  %16 = add i64 %15, 1
  store i64 %16, ptr %2, align 8, !tbaa !8
  %17 = add i64 %.039, 1
  %18 = getelementptr inbounds i8, ptr %0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %.not = icmp eq i8 %19, %1
  %.not30 = icmp eq i8 %19, 0
  %or.cond = or i1 %.not, %.not30
  br i1 %or.cond, label %.critedge, label %.lr.ph, !llvm.loop !10

.critedge:                                        ; preds = %14, %.preheader
  %.022.lcssa = phi ptr [ %4, %.preheader ], [ %.1, %14 ]
  %.0.lcssa = phi i64 [ 0, %.preheader ], [ %17, %14 ]
  %20 = getelementptr inbounds i8, ptr %.022.lcssa, i64 %.0.lcssa
  store i8 0, ptr %20, align 1, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %10, %.critedge, %3
  %.126 = phi ptr [ null, %3 ], [ %.022.lcssa, %.critedge ], [ null, %10 ]
  ret ptr %.126
}

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) 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 #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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, 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 #3 = { nounwind allocsize(0) }
attributes #4 = { nounwind allocsize(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 = !{!9, !9, i64 0}
!9 = !{!"long", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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