; LLVM IR from jspd-group/pegit :: src (strbuf)

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

%struct.strbuf = type { i64, i64, ptr }

; 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 void @strbuf_grow(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @strbuf_release(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i64 @strbuf_read(ptr noundef %0, i32 noundef %1, i64 noundef %2) #1 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.strbuf, ptr %13, i32 0, i32 1
  %15 = load i64, ptr %14, align 8, !tbaa !13
  store i64 %15, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.strbuf, ptr %16, i32 0, i32 0
  %18 = load i64, ptr %17, align 8, !tbaa !15
  store i64 %18, ptr %9, align 8, !tbaa !11
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load i64, ptr %7, align 8, !tbaa !11
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %24

22:                                               ; preds = %3
  %23 = load i64, ptr %7, align 8, !tbaa !11
  br label %25

24:                                               ; preds = %3
  br label %25

25:                                               ; preds = %24, %22
  %26 = phi i64 [ %23, %22 ], [ 8192, %24 ]
  call void @strbuf_grow(ptr noundef %19, i64 noundef %26)
  br label %27

27:                                               ; preds = %71, %25
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.strbuf, ptr %28, i32 0, i32 0
  %30 = load i64, ptr %29, align 8, !tbaa !15
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.strbuf, ptr %31, i32 0, i32 1
  %33 = load i64, ptr %32, align 8, !tbaa !13
  %34 = sub i64 %30, %33
  %35 = sub i64 %34, 1
  store i64 %35, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.strbuf, ptr %37, i32 0, i32 2
  %39 = load ptr, ptr %38, align 8, !tbaa !16
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.strbuf, ptr %40, i32 0, i32 1
  %42 = load i64, ptr %41, align 8, !tbaa !13
  %43 = getelementptr inbounds i8, ptr %39, i64 %42
  %44 = load i64, ptr %10, align 8, !tbaa !11
  %45 = call i64 @read(i32 noundef %36, ptr noundef %43, i64 noundef %44)
  store i64 %45, ptr %11, align 8, !tbaa !11
  %46 = load i64, ptr %11, align 8, !tbaa !11
  %47 = icmp slt i64 %46, 0
  br i1 %47, label %48, label %57

48:                                               ; preds = %27
  %49 = load i64, ptr %9, align 8, !tbaa !11
  %50 = icmp eq i64 %49, 0
  br i1 %50, label %51, label %53

51:                                               ; preds = %48
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  call void @strbuf_release(ptr noundef %52)
  br label %56

53:                                               ; preds = %48
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load i64, ptr %8, align 8, !tbaa !11
  call void @strbuf_setlen(ptr noundef %54, i64 noundef %55)
  br label %56

56:                                               ; preds = %53, %51
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %69

57:                                               ; preds = %27
  %58 = load i64, ptr %11, align 8, !tbaa !11
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.strbuf, ptr %59, i32 0, i32 1
  %61 = load i64, ptr %60, align 8, !tbaa !13
  %62 = add i64 %61, %58
  store i64 %62, ptr %60, align 8, !tbaa !13
  %63 = load i64, ptr %11, align 8, !tbaa !11
  %64 = load i64, ptr %10, align 8, !tbaa !11
  %65 = icmp slt i64 %63, %64
  br i1 %65, label %66, label %67

66:                                               ; preds = %57
  store i32 2, ptr %12, align 4
  br label %69

67:                                               ; preds = %57
  %68 = load ptr, ptr %5, align 8, !tbaa !5
  call void @strbuf_grow(ptr noundef %68, i64 noundef 8192)
  store i32 0, ptr %12, align 4
  br label %69

69:                                               ; preds = %67, %66, %56
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %70 = load i32, ptr %12, align 4
  switch i32 %70, label %85 [
    i32 0, label %71
    i32 2, label %72
  ]

71:                                               ; preds = %69
  br label %27

72:                                               ; preds = %69
  %73 = load ptr, ptr %5, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.strbuf, ptr %73, i32 0, i32 2
  %75 = load ptr, ptr %74, align 8, !tbaa !16
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.strbuf, ptr %76, i32 0, i32 1
  %78 = load i64, ptr %77, align 8, !tbaa !13
  %79 = getelementptr inbounds i8, ptr %75, i64 %78
  store i8 0, ptr %79, align 1, !tbaa !17
  %80 = load ptr, ptr %5, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.strbuf, ptr %80, i32 0, i32 1
  %82 = load i64, ptr %81, align 8, !tbaa !13
  %83 = load i64, ptr %8, align 8, !tbaa !11
  %84 = sub i64 %82, %83
  store i64 %84, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %85

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

declare i64 @read(i32 noundef, ptr noundef, i64 noundef) #3

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 = { 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 = { "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 }

!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", !7, i64 0}
!13 = !{!14, !12, i64 8}
!14 = !{!"strbuf", !12, i64 0, !12, i64 8, !6, i64 16}
!15 = !{!14, !12, i64 0}
!16 = !{!14, !6, i64 16}
!17 = !{!7, !7, i64 0}


; 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 void @strbuf_grow(ptr noundef, i64 noundef) local_unnamed_addr #0

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @strbuf_setlen(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local i64 @strbuf_read(ptr noundef %0, i32 noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = load i64, ptr %0, align 8, !tbaa !11
  %.not = icmp eq i64 %2, 0
  %7 = select i1 %.not, i64 8192, i64 %2
  tail call void @strbuf_grow(ptr noundef nonnull %0, i64 noundef %7)
  %8 = getelementptr inbounds i8, ptr %0, i64 16
  %9 = load i64, ptr %0, align 8, !tbaa !11
  %10 = load i64, ptr %4, align 8, !tbaa !5
  %11 = xor i64 %10, -1
  %12 = add i64 %9, %11
  %13 = load ptr, ptr %8, align 8, !tbaa !12
  %14 = getelementptr inbounds i8, ptr %13, i64 %10
  %15 = tail call i64 @read(i32 noundef %1, ptr noundef %14, i64 noundef %12) #3
  %16 = icmp slt i64 %15, 0
  br i1 %16, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %25, %3
  %17 = icmp eq i64 %6, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %._crit_edge
  tail call void @strbuf_release(ptr noundef nonnull %0)
  br label %.thread

19:                                               ; preds = %._crit_edge
  tail call void @strbuf_setlen(ptr noundef nonnull %0, i64 noundef %5)
  br label %.thread

.lr.ph:                                           ; preds = %3, %25
  %20 = phi i64 [ %32, %25 ], [ %15, %3 ]
  %21 = phi i64 [ %29, %25 ], [ %12, %3 ]
  %22 = load i64, ptr %4, align 8, !tbaa !5
  %23 = add i64 %22, %20
  store i64 %23, ptr %4, align 8, !tbaa !5
  %24 = icmp slt i64 %20, %21
  br i1 %24, label %34, label %25

25:                                               ; preds = %.lr.ph
  tail call void @strbuf_grow(ptr noundef nonnull %0, i64 noundef 8192)
  %26 = load i64, ptr %0, align 8, !tbaa !11
  %27 = load i64, ptr %4, align 8, !tbaa !5
  %28 = xor i64 %27, -1
  %29 = add i64 %26, %28
  %30 = load ptr, ptr %8, align 8, !tbaa !12
  %31 = getelementptr inbounds i8, ptr %30, i64 %27
  %32 = tail call i64 @read(i32 noundef %1, ptr noundef %31, i64 noundef %29) #3
  %33 = icmp slt i64 %32, 0
  br i1 %33, label %._crit_edge, label %.lr.ph

34:                                               ; preds = %.lr.ph
  %35 = load ptr, ptr %8, align 8, !tbaa !12
  %36 = getelementptr inbounds i8, ptr %35, i64 %23
  store i8 0, ptr %36, align 1, !tbaa !13
  %37 = load i64, ptr %4, align 8, !tbaa !5
  %38 = sub i64 %37, %5
  br label %.thread

.thread:                                          ; preds = %18, %19, %34
  %.2 = phi i64 [ %38, %34 ], [ -1, %19 ], [ -1, %18 ]
  ret i64 %.2
}

; Function Attrs: nofree
declare noundef i64 @read(i32 noundef, ptr nocapture noundef, i64 noundef) local_unnamed_addr #2

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 = { 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 #2 = { nofree "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 }

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


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