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

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sc_buf_put_text(ptr noundef %0, ptr noundef %1, ...) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca [1 x %struct.__va_list_tag], align 16
  %11 = 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 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = call i64 @sc_buf_size(ptr noundef %12)
  %14 = icmp ugt i64 %13, 0
  %15 = zext i1 %14 to i64
  %16 = select i1 %14, i32 1, i32 0
  store i32 %16, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.start.p0(i64 24, ptr %10) #4
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = call ptr @sc_buf_wbuf(ptr noundef %17)
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = sub i64 0, %20
  %22 = getelementptr inbounds i8, ptr %18, i64 %21
  store ptr %22, ptr %9, align 8, !tbaa !5
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = call i64 @sc_buf_quota(ptr noundef %23)
  store i64 %24, ptr %8, align 8, !tbaa !11
  %25 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %10, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %25)
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = load i64, ptr %8, align 8, !tbaa !11
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %10, i64 0, i64 0
  %30 = call i32 @vsnprintf(ptr noundef %26, i64 noundef %27, ptr noundef %28, ptr noundef %29) #4
  store i32 %30, ptr %5, align 4, !tbaa !9
  %31 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %10, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %31)
  %32 = load i32, ptr %5, align 4, !tbaa !9
  %33 = icmp slt i32 %32, 0
  br i1 %33, label %34, label %39

34:                                               ; preds = %2
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.sc_buf, ptr %35, i32 0, i32 5
  %37 = load i32, ptr %36, align 8, !tbaa !13
  %38 = or i32 %37, 1
  store i32 %38, ptr %36, align 8, !tbaa !13
  br label %85

39:                                               ; preds = %2
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  store i64 %41, ptr %7, align 8, !tbaa !11
  %42 = load i64, ptr %7, align 8, !tbaa !11
  %43 = load i64, ptr %8, align 8, !tbaa !11
  %44 = icmp uge i64 %42, %43
  br i1 %44, label %45, label %78

45:                                               ; preds = %39
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = load i64, ptr %7, align 8, !tbaa !11
  %48 = add i64 %47, 1
  %49 = call zeroext i1 @sc_buf_reserve(ptr noundef %46, i64 noundef %48)
  br i1 %49, label %51, label %50

50:                                               ; preds = %45
  br label %85

51:                                               ; preds = %45
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = call ptr @sc_buf_wbuf(ptr noundef %52)
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = sext i32 %54 to i64
  %56 = sub i64 0, %55
  %57 = getelementptr inbounds i8, ptr %53, i64 %56
  store ptr %57, ptr %9, align 8, !tbaa !5
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = call i64 @sc_buf_quota(ptr noundef %58)
  store i64 %59, ptr %8, align 8, !tbaa !11
  %60 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %10, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %60)
  %61 = load ptr, ptr %9, align 8, !tbaa !5
  %62 = load i64, ptr %8, align 8, !tbaa !11
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %10, i64 0, i64 0
  %65 = call i32 @vsnprintf(ptr noundef %61, i64 noundef %62, ptr noundef %63, ptr noundef %64) #4
  store i32 %65, ptr %5, align 4, !tbaa !9
  %66 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %10, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %66)
  %67 = load i32, ptr %5, align 4, !tbaa !9
  %68 = icmp slt i32 %67, 0
  br i1 %68, label %74, label %69

69:                                               ; preds = %51
  %70 = load i32, ptr %5, align 4, !tbaa !9
  %71 = sext i32 %70 to i64
  %72 = load i64, ptr %8, align 8, !tbaa !11
  %73 = icmp uge i64 %71, %72
  br i1 %73, label %74, label %77

74:                                               ; preds = %69, %51
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.sc_buf, ptr %75, i32 0, i32 5
  store i32 3, ptr %76, align 8, !tbaa !13
  br label %85

77:                                               ; preds = %69
  br label %78

78:                                               ; preds = %77, %39
  %79 = load ptr, ptr %3, align 8, !tbaa !5
  %80 = load i32, ptr %5, align 4, !tbaa !9
  %81 = load i32, ptr %6, align 4, !tbaa !9
  %82 = sub nsw i32 %80, %81
  %83 = add nsw i32 %82, 1
  %84 = sext i32 %83 to i64
  call void @sc_buf_mark_write(ptr noundef %79, i64 noundef %84)
  store i32 1, ptr %11, align 4
  br label %87

85:                                               ; preds = %74, %50, %34
  %86 = load ptr, ptr %3, align 8, !tbaa !5
  call void @sc_buf_set_wpos(ptr noundef %86, i64 noundef 0)
  store i32 0, ptr %11, align 4
  br label %87

87:                                               ; preds = %85, %78
  call void @llvm.lifetime.end.p0(i64 24, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  %88 = load i32, ptr %11, align 4
  switch i32 %88, label %90 [
    i32 0, label %89
    i32 1, label %89
  ]

89:                                               ; preds = %87, %87
  ret void

90:                                               ; preds = %87
  unreachable
}

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 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 = { 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, !10, i64 40}
!14 = !{!"sc_buf", !6, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !10, 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 i64 @sc_buf_size(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 void @sc_buf_set_wpos(ptr noundef, i64 noundef) local_unnamed_addr #1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @sc_buf_put_text(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_size(ptr noundef %0)
  %.not = icmp ne i64 %4, 0
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #4
  %5 = tail call ptr @sc_buf_wbuf(ptr noundef %0)
  %.neg = sext i1 %.not to i64
  %6 = getelementptr inbounds i8, ptr %5, i64 %.neg
  %7 = tail call i64 @sc_buf_quota(ptr noundef %0)
  call void @llvm.va_start.p0(ptr nonnull %3)
  %8 = call i32 @vsnprintf(ptr noundef %6, i64 noundef %7, ptr noundef %1, ptr noundef nonnull %3) #4
  call void @llvm.va_end.p0(ptr nonnull %3)
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %10, label %14

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

14:                                               ; preds = %2
  %15 = zext nneg i32 %8 to i64
  %.not30 = icmp ugt i64 %7, %15
  br i1 %.not30, label %28, label %16

16:                                               ; preds = %14
  %17 = add nuw nsw i64 %15, 1
  %18 = call zeroext i1 @sc_buf_reserve(ptr noundef %0, i64 noundef %17)
  br i1 %18, label %19, label %32

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

26:                                               ; preds = %19
  %27 = getelementptr inbounds i8, ptr %0, i64 40
  store i32 3, ptr %27, align 8, !tbaa !5
  br label %32

28:                                               ; preds = %19, %14
  %.0 = phi i32 [ %8, %14 ], [ %23, %19 ]
  %not..not = xor i1 %.not, true
  %29 = zext i1 %not..not to i32
  %30 = add nuw i32 %.0, %29
  %31 = zext nneg i32 %30 to i64
  call void @sc_buf_mark_write(ptr noundef %0, i64 noundef %31)
  br label %33

32:                                               ; preds = %16, %26, %10
  call void @sc_buf_set_wpos(ptr noundef %0, i64 noundef 0)
  br label %33

33:                                               ; preds = %32, %28
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #4
  ret void
}

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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 #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, !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