; LLVM IR from ponylang/ponyc :: src/libponyc/ast (printbuf)

; 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.printbuf_t = type { ptr, i64, i64 }

@.str = external hidden unnamed_addr constant [35 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@__func__.printbuf = external hidden unnamed_addr constant [9 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: nounwind uwtable
define dso_local void @printbuf(ptr noundef %0, ptr noundef %1, ...) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca [1 x %struct.__va_list_tag], align 16
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  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
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.printbuf_t, ptr %10, i32 0, i32 1
  %12 = load i64, ptr %11, align 8, !tbaa !9
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.printbuf_t, ptr %13, i32 0, i32 2
  %15 = load i64, ptr %14, align 8, !tbaa !12
  %16 = sub i64 %12, %15
  store i64 %16, ptr %5, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 24, ptr %6) #5
  %17 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %17)
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %18 = load ptr, ptr %3, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.printbuf_t, ptr %18, i32 0, i32 0
  %20 = load ptr, ptr %19, align 8, !tbaa !14
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.printbuf_t, ptr %21, i32 0, i32 2
  %23 = load i64, ptr %22, align 8, !tbaa !12
  %24 = getelementptr inbounds i8, ptr %20, i64 %23
  %25 = load i64, ptr %5, align 8, !tbaa !13
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  %28 = call i32 @vsnprintf(ptr noundef %24, i64 noundef %25, ptr noundef %26, ptr noundef %27) #5
  store i32 %28, ptr %7, align 4, !tbaa !15
  %29 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %29)
  %30 = load i32, ptr %7, align 4, !tbaa !15
  %31 = icmp slt i32 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %2
  store i32 1, ptr %8, align 4
  br label %98

33:                                               ; preds = %2
  %34 = load i32, ptr %7, align 4, !tbaa !15
  %35 = sext i32 %34 to i64
  %36 = load i64, ptr %5, align 8, !tbaa !13
  %37 = icmp uge i64 %35, %36
  br i1 %37, label %38, label %91

38:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.printbuf_t, ptr %39, i32 0, i32 1
  %41 = load i64, ptr %40, align 8, !tbaa !9
  %42 = load i32, ptr %7, align 4, !tbaa !15
  %43 = sext i32 %42 to i64
  %44 = add i64 %41, %43
  %45 = add i64 %44, 1
  store i64 %45, ptr %9, align 8, !tbaa !13
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.printbuf_t, ptr %46, i32 0, i32 1
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = load i64, ptr %9, align 8, !tbaa !13
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.printbuf_t, ptr %50, i32 0, i32 0
  %52 = load ptr, ptr %51, align 8, !tbaa !14
  %53 = call ptr @ponyint_pool_realloc_size(i64 noundef %48, i64 noundef %49, ptr noundef %52)
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.printbuf_t, ptr %54, i32 0, i32 0
  store ptr %53, ptr %55, align 8, !tbaa !14
  %56 = load i64, ptr %9, align 8, !tbaa !13
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.printbuf_t, ptr %57, i32 0, i32 1
  store i64 %56, ptr %58, align 8, !tbaa !9
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.printbuf_t, ptr %59, i32 0, i32 1
  %61 = load i64, ptr %60, align 8, !tbaa !9
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.printbuf_t, ptr %62, i32 0, i32 2
  %64 = load i64, ptr %63, align 8, !tbaa !12
  %65 = sub i64 %61, %64
  store i64 %65, ptr %5, align 8, !tbaa !13
  %66 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  call void @llvm.va_start.p0(ptr %66)
  %67 = load ptr, ptr %3, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.printbuf_t, ptr %67, i32 0, i32 0
  %69 = load ptr, ptr %68, align 8, !tbaa !14
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.printbuf_t, ptr %70, i32 0, i32 2
  %72 = load i64, ptr %71, align 8, !tbaa !12
  %73 = getelementptr inbounds i8, ptr %69, i64 %72
  %74 = load i64, ptr %5, align 8, !tbaa !13
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  %77 = call i32 @vsnprintf(ptr noundef %73, i64 noundef %74, ptr noundef %75, ptr noundef %76) #5
  store i32 %77, ptr %7, align 4, !tbaa !15
  %78 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %6, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %78)
  %79 = load i32, ptr %7, align 4, !tbaa !15
  %80 = icmp sgt i32 %79, 0
  br i1 %80, label %81, label %89

81:                                               ; preds = %38
  %82 = load i32, ptr %7, align 4, !tbaa !15
  %83 = sext i32 %82 to i64
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.printbuf_t, ptr %84, i32 0, i32 1
  %86 = load i64, ptr %85, align 8, !tbaa !9
  %87 = icmp ult i64 %83, %86
  br i1 %87, label %88, label %89

88:                                               ; preds = %81
  br label %90

89:                                               ; preds = %81, %38
  call void @ponyint_assert_fail(ptr noundef @.str, ptr noundef @.str.1, i64 noundef 57, ptr noundef @__func__.printbuf)
  br label %90

90:                                               ; preds = %89, %88
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  br label %91

91:                                               ; preds = %90, %33
  %92 = load i32, ptr %7, align 4, !tbaa !15
  %93 = sext i32 %92 to i64
  %94 = load ptr, ptr %3, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.printbuf_t, ptr %94, i32 0, i32 2
  %96 = load i64, ptr %95, align 8, !tbaa !12
  %97 = add i64 %96, %93
  store i64 %97, ptr %95, align 8, !tbaa !12
  store i32 0, ptr %8, align 4
  br label %98

98:                                               ; preds = %91, %32
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 24, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  %99 = load i32, ptr %8, 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: 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

declare ptr @ponyint_pool_realloc_size(i64 noundef, i64 noundef, ptr noundef) #4

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) #4

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 = { "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, !11, i64 8}
!10 = !{!"printbuf_t", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 16}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !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 }

@.str = external hidden unnamed_addr constant [35 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [76 x i8], align 1
@__func__.printbuf = external hidden unnamed_addr constant [9 x i8], align 1

; 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
define dso_local void @printbuf(ptr nocapture noundef %0, ptr nocapture noundef readonly %1, ...) local_unnamed_addr #1 {
  %3 = alloca [1 x %struct.__va_list_tag], align 16
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load i64, ptr %6, align 8, !tbaa !11
  %8 = sub i64 %5, %7
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %3) #5
  call void @llvm.va_start.p0(ptr nonnull %3)
  %9 = load ptr, ptr %0, align 8, !tbaa !12
  %10 = load i64, ptr %6, align 8, !tbaa !11
  %11 = getelementptr inbounds i8, ptr %9, i64 %10
  %12 = call i32 @vsnprintf(ptr noundef %11, i64 noundef %8, ptr noundef %1, ptr noundef nonnull %3) #5
  call void @llvm.va_end.p0(ptr nonnull %3)
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %38, label %14

14:                                               ; preds = %2
  %15 = zext nneg i32 %12 to i64
  %.not = icmp ugt i64 %8, %15
  br i1 %.not, label %34, label %16

16:                                               ; preds = %14
  %17 = load i64, ptr %4, align 8, !tbaa !5
  %18 = add nuw nsw i64 %15, 1
  %19 = add i64 %18, %17
  %20 = load ptr, ptr %0, align 8, !tbaa !12
  %21 = call ptr @ponyint_pool_realloc_size(i64 noundef %17, i64 noundef %19, ptr noundef %20) #5
  store ptr %21, ptr %0, align 8, !tbaa !12
  store i64 %19, ptr %4, align 8, !tbaa !5
  %22 = load i64, ptr %6, align 8, !tbaa !11
  %23 = sub i64 %19, %22
  call void @llvm.va_start.p0(ptr nonnull %3)
  %24 = load ptr, ptr %0, align 8, !tbaa !12
  %25 = load i64, ptr %6, align 8, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %24, i64 %25
  %27 = call i32 @vsnprintf(ptr noundef %26, i64 noundef %23, ptr noundef %1, ptr noundef nonnull %3) #5
  call void @llvm.va_end.p0(ptr nonnull %3)
  %28 = icmp sgt i32 %27, 0
  br i1 %28, label %29, label %33

29:                                               ; preds = %16
  %30 = zext nneg i32 %27 to i64
  %31 = load i64, ptr %4, align 8, !tbaa !5
  %32 = icmp ugt i64 %31, %30
  br i1 %32, label %34, label %33

33:                                               ; preds = %29, %16
  call void @ponyint_assert_fail(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, i64 noundef 57, ptr noundef nonnull @__func__.printbuf) #5
  br label %34

34:                                               ; preds = %33, %29, %14
  %.0 = phi i32 [ %12, %14 ], [ %27, %29 ], [ %27, %33 ]
  %35 = sext i32 %.0 to i64
  %36 = load i64, ptr %6, align 8, !tbaa !11
  %37 = add i64 %36, %35
  store i64 %37, ptr %6, align 8, !tbaa !11
  br label %38

38:                                               ; preds = %2, %34
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %3) #5
  ret void
}

; 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

declare ptr @ponyint_pool_realloc_size(i64 noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

declare void @ponyint_assert_fail(ptr noundef, ptr noundef, i64 noundef, ptr noundef) local_unnamed_addr #4

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


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