; LLVM IR from attractivechaos/klib :: kstring (kvsprintf)

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

; Function Attrs: nounwind uwtable
define dso_local i32 @kvsprintf(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca [1 x %struct.__va_list_tag], align 16
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 24, ptr %7) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %9 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %10 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.va_copy.p0(ptr %9, ptr %10)
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.__kstring_t, ptr %11, i32 0, i32 2
  %13 = load ptr, ptr %12, align 8, !tbaa !9
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.__kstring_t, ptr %14, i32 0, i32 0
  %16 = load i64, ptr %15, align 8, !tbaa !12
  %17 = getelementptr inbounds i8, ptr %13, i64 %16
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.__kstring_t, ptr %18, i32 0, i32 1
  %20 = load i64, ptr %19, align 8, !tbaa !13
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.__kstring_t, ptr %21, i32 0, i32 0
  %23 = load i64, ptr %22, align 8, !tbaa !12
  %24 = sub i64 %20, %23
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %27 = call i32 @vsnprintf(ptr noundef %17, i64 noundef %24, ptr noundef %25, ptr noundef %26) #5
  store i32 %27, ptr %8, align 4, !tbaa !14
  %28 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %28)
  %29 = load i32, ptr %8, align 4, !tbaa !14
  %30 = add nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.__kstring_t, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !13
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.__kstring_t, ptr %35, i32 0, i32 0
  %37 = load i64, ptr %36, align 8, !tbaa !12
  %38 = sub i64 %34, %37
  %39 = icmp ugt i64 %31, %38
  br i1 %39, label %40, label %127

40:                                               ; preds = %3
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.__kstring_t, ptr %41, i32 0, i32 0
  %43 = load i64, ptr %42, align 8, !tbaa !12
  %44 = load i32, ptr %8, align 4, !tbaa !14
  %45 = sext i32 %44 to i64
  %46 = add i64 %43, %45
  %47 = add i64 %46, 2
  %48 = load ptr, ptr %4, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.__kstring_t, ptr %48, i32 0, i32 1
  store i64 %47, ptr %49, align 8, !tbaa !13
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.__kstring_t, ptr %50, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !13
  %53 = add i64 %52, -1
  store i64 %53, ptr %51, align 8, !tbaa !13
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.__kstring_t, ptr %54, i32 0, i32 1
  %56 = load i64, ptr %55, align 8, !tbaa !13
  %57 = lshr i64 %56, 1
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.__kstring_t, ptr %58, i32 0, i32 1
  %60 = load i64, ptr %59, align 8, !tbaa !13
  %61 = or i64 %60, %57
  store i64 %61, ptr %59, align 8, !tbaa !13
  %62 = load ptr, ptr %4, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.__kstring_t, ptr %62, i32 0, i32 1
  %64 = load i64, ptr %63, align 8, !tbaa !13
  %65 = lshr i64 %64, 2
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.__kstring_t, ptr %66, i32 0, i32 1
  %68 = load i64, ptr %67, align 8, !tbaa !13
  %69 = or i64 %68, %65
  store i64 %69, ptr %67, align 8, !tbaa !13
  %70 = load ptr, ptr %4, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.__kstring_t, ptr %70, i32 0, i32 1
  %72 = load i64, ptr %71, align 8, !tbaa !13
  %73 = lshr i64 %72, 4
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.__kstring_t, ptr %74, i32 0, i32 1
  %76 = load i64, ptr %75, align 8, !tbaa !13
  %77 = or i64 %76, %73
  store i64 %77, ptr %75, align 8, !tbaa !13
  %78 = load ptr, ptr %4, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.__kstring_t, ptr %78, i32 0, i32 1
  %80 = load i64, ptr %79, align 8, !tbaa !13
  %81 = lshr i64 %80, 8
  %82 = load ptr, ptr %4, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.__kstring_t, ptr %82, i32 0, i32 1
  %84 = load i64, ptr %83, align 8, !tbaa !13
  %85 = or i64 %84, %81
  store i64 %85, ptr %83, align 8, !tbaa !13
  %86 = load ptr, ptr %4, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.__kstring_t, ptr %86, i32 0, i32 1
  %88 = load i64, ptr %87, align 8, !tbaa !13
  %89 = lshr i64 %88, 16
  %90 = load ptr, ptr %4, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.__kstring_t, ptr %90, i32 0, i32 1
  %92 = load i64, ptr %91, align 8, !tbaa !13
  %93 = or i64 %92, %89
  store i64 %93, ptr %91, align 8, !tbaa !13
  %94 = load ptr, ptr %4, align 8, !tbaa !5
  %95 = getelementptr inbounds %struct.__kstring_t, ptr %94, i32 0, i32 1
  %96 = load i64, ptr %95, align 8, !tbaa !13
  %97 = add i64 %96, 1
  store i64 %97, ptr %95, align 8, !tbaa !13
  %98 = load ptr, ptr %4, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.__kstring_t, ptr %98, i32 0, i32 2
  %100 = load ptr, ptr %99, align 8, !tbaa !9
  %101 = load ptr, ptr %4, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.__kstring_t, ptr %101, i32 0, i32 1
  %103 = load i64, ptr %102, align 8, !tbaa !13
  %104 = call ptr @realloc(ptr noundef %100, i64 noundef %103) #6
  %105 = load ptr, ptr %4, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.__kstring_t, ptr %105, i32 0, i32 2
  store ptr %104, ptr %106, align 8, !tbaa !9
  %107 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %108 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.va_copy.p0(ptr %107, ptr %108)
  %109 = load ptr, ptr %4, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.__kstring_t, ptr %109, i32 0, i32 2
  %111 = load ptr, ptr %110, align 8, !tbaa !9
  %112 = load ptr, ptr %4, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.__kstring_t, ptr %112, i32 0, i32 0
  %114 = load i64, ptr %113, align 8, !tbaa !12
  %115 = getelementptr inbounds i8, ptr %111, i64 %114
  %116 = load ptr, ptr %4, align 8, !tbaa !5
  %117 = getelementptr inbounds %struct.__kstring_t, ptr %116, i32 0, i32 1
  %118 = load i64, ptr %117, align 8, !tbaa !13
  %119 = load ptr, ptr %4, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.__kstring_t, ptr %119, i32 0, i32 0
  %121 = load i64, ptr %120, align 8, !tbaa !12
  %122 = sub i64 %118, %121
  %123 = load ptr, ptr %5, align 8, !tbaa !5
  %124 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  %125 = call i32 @vsnprintf(ptr noundef %115, i64 noundef %122, ptr noundef %123, ptr noundef %124) #5
  store i32 %125, ptr %8, align 4, !tbaa !14
  %126 = getelementptr inbounds [1 x %struct.__va_list_tag], ptr %7, i64 0, i64 0
  call void @llvm.va_end.p0(ptr %126)
  br label %127

127:                                              ; preds = %40, %3
  %128 = load i32, ptr %8, align 4, !tbaa !14
  %129 = sext i32 %128 to i64
  %130 = load ptr, ptr %4, align 8, !tbaa !5
  %131 = getelementptr inbounds %struct.__kstring_t, ptr %130, i32 0, i32 0
  %132 = load i64, ptr %131, align 8, !tbaa !12
  %133 = add i64 %132, %129
  store i64 %133, ptr %131, align 8, !tbaa !12
  %134 = load i32, ptr %8, align 4, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 24, ptr %7) #5
  ret i32 %134
}

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_copy.p0(ptr, 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 allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #4

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #5 = { nounwind }
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 = !{!10, !6, i64 16}
!10 = !{!"__kstring_t", !11, i64 0, !11, i64 8, !6, i64 16}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 0}
!13 = !{!10, !11, i64 8}
!14 = !{!15, !15, i64 0}
!15 = !{!"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 }

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @kvsprintf(ptr nocapture noundef %0, ptr nocapture noundef readonly %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca [1 x %struct.__va_list_tag], align 16
  call void @llvm.lifetime.start.p0(i64 24, ptr nonnull %4) #5
  call void @llvm.va_copy.p0(ptr nonnull %4, ptr %2)
  %5 = getelementptr inbounds i8, ptr %0, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !5
  %7 = load i64, ptr %0, align 8, !tbaa !11
  %8 = getelementptr inbounds i8, ptr %6, i64 %7
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !12
  %11 = sub i64 %10, %7
  %12 = call i32 @vsnprintf(ptr noundef %8, i64 noundef %11, ptr noundef %1, ptr noundef nonnull %4) #5
  call void @llvm.va_end.p0(ptr nonnull %4)
  %13 = add nsw i32 %12, 1
  %14 = sext i32 %13 to i64
  %15 = load i64, ptr %9, align 8, !tbaa !12
  %16 = load i64, ptr %0, align 8, !tbaa !11
  %17 = sub i64 %15, %16
  %18 = icmp ult i64 %17, %14
  br i1 %18, label %19, label %42

19:                                               ; preds = %3
  %20 = sext i32 %12 to i64
  %21 = add nsw i64 %20, 1
  %22 = add i64 %21, %16
  %23 = lshr i64 %22, 1
  %24 = or i64 %23, %22
  %25 = lshr i64 %24, 2
  %26 = or i64 %25, %24
  %27 = lshr i64 %26, 4
  %28 = or i64 %27, %26
  %29 = lshr i64 %28, 8
  %30 = or i64 %29, %28
  %31 = lshr i64 %30, 16
  %32 = or i64 %31, %30
  %33 = add i64 %32, 1
  store i64 %33, ptr %9, align 8, !tbaa !12
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = call ptr @realloc(ptr noundef %34, i64 noundef %33) #6
  store ptr %35, ptr %5, align 8, !tbaa !5
  call void @llvm.va_copy.p0(ptr nonnull %4, ptr %2)
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i64, ptr %0, align 8, !tbaa !11
  %38 = getelementptr inbounds i8, ptr %36, i64 %37
  %39 = load i64, ptr %9, align 8, !tbaa !12
  %40 = sub i64 %39, %37
  %41 = call i32 @vsnprintf(ptr noundef %38, i64 noundef %40, ptr noundef %1, ptr noundef nonnull %4) #5
  call void @llvm.va_end.p0(ptr nonnull %4)
  %.pre = load i64, ptr %0, align 8, !tbaa !11
  br label %42

42:                                               ; preds = %19, %3
  %43 = phi i64 [ %.pre, %19 ], [ %16, %3 ]
  %.0 = phi i32 [ %41, %19 ], [ %12, %3 ]
  %44 = sext i32 %.0 to i64
  %45 = add i64 %43, %44
  store i64 %45, ptr %0, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %4) #5
  ret i32 %.0
}

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_copy.p0(ptr, 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: 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 #4

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #5 = { nounwind }
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, !10, i64 16}
!6 = !{!"__kstring_t", !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, !7, i64 8}


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