; LLVM IR from tezc/sc :: buffer (sc_buf)

; 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.__va_list_tag = type { i32, i32, ptr, ptr }
%struct.sc_buf = type { ptr, i64, i64, i64, i64, i32, i8 }

; 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 zeroext i1 @sc_buf_reserve(ptr noundef, i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @sc_buf_quota(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @sc_buf_wpos(ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sc_buf_put_fmt(ptr noundef %0, ptr noundef %1, ...) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca [1 x %struct.__va_list_tag], align 16
  %12 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %13 = call i64 @sc_buf_64_len(i64 noundef 0)
  store i64 %13, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = call i64 @sc_buf_wpos(ptr noundef %14)
  store i64 %15, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = call i64 @sc_buf_quota(ptr noundef %16)
  store i64 %17, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = call ptr @sc_buf_wbuf(ptr noundef %18)
  %20 = call i64 @sc_buf_64_len(i64 noundef 0)
  %21 = getelementptr inbounds i8, ptr %19, i64 %20
  store ptr %21, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 24, ptr %11) #5
  %22 = load i64, ptr %9, align 8, !tbaa !9
  %23 = load i64, ptr %5, align 8, !tbaa !9
  %24 = icmp ugt i64 %22, %23
  br i1 %24, label %25, label %29

25:                                               ; preds = %2
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = load i64, ptr %9, align 8, !tbaa !9
  %28 = sub i64 %27, %26
  store i64 %28, ptr %9, align 8, !tbaa !9
  br label %30

29:                                               ; preds = %2
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %29, %25
  %31 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %11, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %31)
  %32 = load ptr, ptr %10, align 8, !tbaa !5
  %33 = load i64, ptr %9, align 8, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %11, i64 0, i64 0
  %36 = call i32 @vsnprintf(ptr noundef %32, i64 noundef %33, ptr noundef %34, ptr noundef %35) #5
  store i32 %36, ptr %7, align 4, !tbaa !11
  %37 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %11, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %37)
  %38 = load i32, ptr %7, align 4, !tbaa !11
  %39 = icmp slt i32 %38, 0
  br i1 %39, label %40, label %45

40:                                               ; preds = %30
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.sc_buf, ptr %41, i32 0, i32 5
  %43 = load i32, ptr %42, align 8, !tbaa !13
  %44 = or i32 %43, 1
  store i32 %44, ptr %42, align 8, !tbaa !13
  store i32 1, ptr %12, align 4
  br label %98

45:                                               ; preds = %30
  %46 = load i32, ptr %7, align 4, !tbaa !11
  %47 = sext i32 %46 to i64
  store i64 %47, ptr %8, align 8, !tbaa !9
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = load i64, ptr %9, align 8, !tbaa !9
  %50 = icmp uge i64 %48, %49
  br i1 %50, label %51, label %89

51:                                               ; preds = %45
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = load i64, ptr %8, align 8, !tbaa !9
  %54 = load i64, ptr %5, align 8, !tbaa !9
  %55 = add i64 %53, %54
  %56 = call zeroext i1 @sc_buf_reserve(ptr noundef %52, i64 noundef %55)
  br i1 %56, label %58, label %57

57:                                               ; preds = %51
  store i32 1, ptr %12, align 4
  br label %98

58:                                               ; preds = %51
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = call ptr @sc_buf_wbuf(ptr noundef %59)
  %61 = load i64, ptr %5, align 8, !tbaa !9
  %62 = getelementptr inbounds i8, ptr %60, i64 %61
  store ptr %62, ptr %10, align 8, !tbaa !5
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = call i64 @sc_buf_quota(ptr noundef %63)
  %65 = load i64, ptr %5, align 8, !tbaa !9
  %66 = sub i64 %64, %65
  store i64 %66, ptr %9, align 8, !tbaa !9
  %67 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %11, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %67)
  %68 = load ptr, ptr %10, align 8, !tbaa !5
  %69 = load i64, ptr %9, align 8, !tbaa !9
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %11, i64 0, i64 0
  %72 = call i32 @vsnprintf(ptr noundef %68, i64 noundef %69, ptr noundef %70, ptr noundef %71) #5
  store i32 %72, ptr %7, align 4, !tbaa !11
  %73 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %11, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %73)
  %74 = load i32, ptr %7, align 4, !tbaa !11
  %75 = icmp slt i32 %74, 0
  br i1 %75, label %81, label %76

76:                                               ; preds = %58
  %77 = load i32, ptr %7, align 4, !tbaa !11
  %78 = sext i32 %77 to i64
  %79 = load i64, ptr %9, align 8, !tbaa !9
  %80 = icmp uge i64 %78, %79
  br i1 %80, label %81, label %86

81:                                               ; preds = %76, %58
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.sc_buf, ptr %82, i32 0, i32 5
  %84 = load i32, ptr %83, align 8, !tbaa !13
  %85 = or i32 %84, 3
  store i32 %85, ptr %83, align 8, !tbaa !13
  store i32 1, ptr %12, align 4
  br label %98

86:                                               ; preds = %76
  %87 = load i32, ptr %7, align 4, !tbaa !11
  %88 = sext i32 %87 to i64
  store i64 %88, ptr %8, align 8, !tbaa !9
  br label %89

89:                                               ; preds = %86, %45
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = load i64, ptr %6, align 8, !tbaa !9
  %92 = load i64, ptr %8, align 8, !tbaa !9
  call void @sc_buf_set_64_at(ptr noundef %90, i64 noundef %91, i64 noundef %92)
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = load i64, ptr %8, align 8, !tbaa !9
  %95 = load i64, ptr %5, align 8, !tbaa !9
  %96 = add i64 %94, %95
  %97 = add i64 %96, 1
  call void @sc_buf_mark_write(ptr noundef %93, i64 noundef %97)
  store i32 0, ptr %12, align 4
  br label %98

98:                                               ; preds = %89, %81, %57, %40
  call void @llvm.lifetime.end.p0(i64 24, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %99 = load i32, ptr %12, align 4
  switch i32 %99, label %101 [
    i32 0, label %100
    i32 1, label %100
  ]

100:                                              ; preds = %98, %98
  ret void

101:                                              ; preds = %98
  unreachable
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @sc_buf_64_len(i64 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #3

; Function Attrs: nounwind
declare i32 @vsnprintf(ptr noundef, i64 noundef, ptr noundef, ptr noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { 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 #5 = { 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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 40}
!14 = !{!"sc_buf", !6, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !12, i64 40, !15, i64 44}
!15 = !{!"_Bool", !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"

%struct.__va_list_tag = type { i32, i32, ptr, ptr }

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress 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 zeroext i1 @sc_buf_reserve(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @sc_buf_quota(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @sc_buf_mark_write(ptr noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i64 @sc_buf_wpos(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @sc_buf_wbuf(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @sc_buf_set_64_at(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @sc_buf_put_fmt(ptr noundef %0, ptr nocapture noundef readonly %1, ...) local_unnamed_addr #1 {
  %3 = alloca [1 x %struct.__va_list_tag], align 16
  %4 = tail call i64 @sc_buf_64_len(i64 noundef 0)
  %5 = tail call i64 @sc_buf_wpos(ptr noundef %0)
  %6 = tail call i64 @sc_buf_quota(ptr noundef %0)
  %7 = tail call ptr @sc_buf_wbuf(ptr noundef %0)
  %8 = tail call i64 @sc_buf_64_len(i64 noundef 0)
  %9 = getelementptr inbounds i8, ptr %7, i64 %8
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #6
  %.0 = tail call i64 @llvm.usub.sat.i64(i64 %6, i64 %4)
  call void @llvm.va_start.p0(ptr nonnull %3)
  %10 = call i32 @vsnprintf(ptr noundef %9, i64 noundef %.0, ptr noundef %1, ptr noundef nonnull %3) #6
  call void @llvm.va_end.p0(ptr nonnull %3)
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %16

12:                                               ; preds = %2
  %13 = getelementptr inbounds i8, ptr %0, i64 40
  %14 = load i32, ptr %13, align 8, !tbaa !5
  %15 = or i32 %14, 1
  store i32 %15, ptr %13, align 8, !tbaa !5
  br label %36

16:                                               ; preds = %2
  %17 = zext nneg i32 %10 to i64
  %.not = icmp ugt i64 %.0, %17
  br i1 %.not, label %33, label %18

18:                                               ; preds = %16
  %19 = add i64 %4, %17
  %20 = call zeroext i1 @sc_buf_reserve(ptr noundef %0, i64 noundef %19)
  br i1 %20, label %21, label %36

21:                                               ; preds = %18
  %22 = call ptr @sc_buf_wbuf(ptr noundef %0)
  %23 = getelementptr inbounds i8, ptr %22, i64 %4
  %24 = call i64 @sc_buf_quota(ptr noundef %0)
  %25 = sub i64 %24, %4
  call void @llvm.va_start.p0(ptr nonnull %3)
  %26 = call i32 @vsnprintf(ptr noundef %23, i64 noundef %25, ptr noundef %1, ptr noundef nonnull %3) #6
  call void @llvm.va_end.p0(ptr nonnull %3)
  %27 = icmp sgt i32 %26, -1
  %28 = zext nneg i32 %26 to i64
  %.not42 = icmp ugt i64 %25, %28
  %or.cond = select i1 %27, i1 %.not42, i1 false
  br i1 %or.cond, label %33, label %29

29:                                               ; preds = %21
  %30 = getelementptr inbounds i8, ptr %0, i64 40
  %31 = load i32, ptr %30, align 8, !tbaa !5
  %32 = or i32 %31, 3
  store i32 %32, ptr %30, align 8, !tbaa !5
  br label %36

33:                                               ; preds = %21, %16
  %.035 = phi i64 [ %17, %16 ], [ %28, %21 ]
  call void @sc_buf_set_64_at(ptr noundef %0, i64 noundef %5, i64 noundef %.035)
  %34 = add i64 %4, 1
  %35 = add i64 %34, %.035
  call void @sc_buf_mark_write(ptr noundef %0, i64 noundef %35)
  br label %36

36:                                               ; preds = %18, %33, %29, %12
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #6
  ret void
}

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #3

; Function Attrs: nofree nounwind
declare noundef i32 @vsnprintf(ptr nocapture noundef, i64 noundef, ptr nocapture noundef readonly, ptr noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.usub.sat.i64(i64, i64) #5

attributes #0 = { mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #4 = { nofree 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 #5 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #6 = { 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, !11, i64 40}
!6 = !{!"sc_buf", !7, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !11, i64 40, !12, i64 44}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"_Bool", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass