; LLVM IR from kala13x/libxutils :: src/data (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.XByteBuffer = type { ptr, i64, i64, i16, i16, i32 }

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

; 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 i32 @XByteBuffer_Resize(ptr noundef %0, i64 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %8 = load i64, ptr %5, align 8, !tbaa !9
  %9 = icmp ne i64 %8, 0
  br i1 %9, label %12, label %10

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  call void @XByteBuffer_Clear(ptr noundef %11)
  store i32 0, ptr %3, align 4
  br label %125

12:                                               ; preds = %2
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.XByteBuffer, ptr %13, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %40

17:                                               ; preds = %12
  %18 = load i64, ptr %5, align 8, !tbaa !9
  %19 = call noalias ptr @malloc(i64 noundef %18) #4
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.XByteBuffer, ptr %20, i32 0, i32 0
  store ptr %19, ptr %21, align 8, !tbaa !11
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.XByteBuffer, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = icmp eq ptr %24, null
  br i1 %25, label %26, label %32

26:                                               ; preds = %17
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.XByteBuffer, ptr %27, i32 0, i32 5
  store i32 -1, ptr %28, align 4, !tbaa !15
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.XByteBuffer, ptr %29, i32 0, i32 5
  %31 = load i32, ptr %30, align 4, !tbaa !15
  store i32 %31, ptr %3, align 4
  br label %125

32:                                               ; preds = %17
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.XByteBuffer, ptr %34, i32 0, i32 1
  store i64 %33, ptr %35, align 8, !tbaa !16
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.XByteBuffer, ptr %36, i32 0, i32 2
  store i64 0, ptr %37, align 8, !tbaa !17
  %38 = load i64, ptr %5, align 8, !tbaa !9
  %39 = trunc i64 %38 to i32
  store i32 %39, ptr %3, align 4
  br label %125

40:                                               ; preds = %12
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.XByteBuffer, ptr %41, i32 0, i32 1
  %43 = load i64, ptr %42, align 8, !tbaa !16
  %44 = icmp ne i64 %43, 0
  br i1 %44, label %62, label %45

45:                                               ; preds = %40
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.XByteBuffer, ptr %46, i32 0, i32 2
  %48 = load i64, ptr %47, align 8, !tbaa !17
  %49 = load i64, ptr %5, align 8, !tbaa !9
  %50 = icmp uge i64 %48, %49
  br i1 %50, label %51, label %56

51:                                               ; preds = %45
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.XByteBuffer, ptr %52, i32 0, i32 2
  %54 = load i64, ptr %53, align 8, !tbaa !17
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %3, align 4
  br label %125

56:                                               ; preds = %45
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.XByteBuffer, ptr %57, i32 0, i32 5
  store i32 -1, ptr %58, align 4, !tbaa !15
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.XByteBuffer, ptr %59, i32 0, i32 5
  %61 = load i32, ptr %60, align 4, !tbaa !15
  store i32 %61, ptr %3, align 4
  br label %125

62:                                               ; preds = %40
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.XByteBuffer, ptr %63, i32 0, i32 0
  %65 = load ptr, ptr %64, align 8, !tbaa !11
  store ptr %65, ptr %6, align 8, !tbaa !5
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.XByteBuffer, ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !11
  %69 = load i64, ptr %5, align 8, !tbaa !9
  %70 = call ptr @realloc(ptr noundef %68, i64 noundef %69) #6
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.XByteBuffer, ptr %71, i32 0, i32 0
  store ptr %70, ptr %72, align 8, !tbaa !11
  %73 = load ptr, ptr %4, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.XByteBuffer, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !11
  %76 = icmp eq ptr %75, null
  br i1 %76, label %77, label %86

77:                                               ; preds = %62
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = load ptr, ptr %4, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.XByteBuffer, ptr %79, i32 0, i32 0
  store ptr %78, ptr %80, align 8, !tbaa !11
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.XByteBuffer, ptr %81, i32 0, i32 5
  store i32 -1, ptr %82, align 4, !tbaa !15
  %83 = load ptr, ptr %4, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.XByteBuffer, ptr %83, i32 0, i32 5
  %85 = load i32, ptr %84, align 4, !tbaa !15
  store i32 %85, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %124

86:                                               ; preds = %62
  %87 = load ptr, ptr %4, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.XByteBuffer, ptr %87, i32 0, i32 2
  %89 = load i64, ptr %88, align 8, !tbaa !17
  %90 = load i64, ptr %5, align 8, !tbaa !9
  %91 = icmp uge i64 %89, %90
  br i1 %91, label %92, label %95

92:                                               ; preds = %86
  %93 = load i64, ptr %5, align 8, !tbaa !9
  %94 = sub i64 %93, 1
  br label %99

95:                                               ; preds = %86
  %96 = load ptr, ptr %4, align 8, !tbaa !5
  %97 = getelementptr inbounds %struct.XByteBuffer, ptr %96, i32 0, i32 2
  %98 = load i64, ptr %97, align 8, !tbaa !17
  br label %99

99:                                               ; preds = %95, %92
  %100 = phi i64 [ %94, %92 ], [ %98, %95 ]
  %101 = load ptr, ptr %4, align 8, !tbaa !5
  %102 = getelementptr inbounds %struct.XByteBuffer, ptr %101, i32 0, i32 2
  store i64 %100, ptr %102, align 8, !tbaa !17
  %103 = load ptr, ptr %4, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.XByteBuffer, ptr %103, i32 0, i32 2
  %105 = load i64, ptr %104, align 8, !tbaa !17
  %106 = load i64, ptr %5, align 8, !tbaa !9
  %107 = icmp ult i64 %105, %106
  br i1 %107, label %108, label %116

108:                                              ; preds = %99
  %109 = load ptr, ptr %4, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.XByteBuffer, ptr %109, i32 0, i32 0
  %111 = load ptr, ptr %110, align 8, !tbaa !11
  %112 = load ptr, ptr %4, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.XByteBuffer, ptr %112, i32 0, i32 2
  %114 = load i64, ptr %113, align 8, !tbaa !17
  %115 = getelementptr inbounds i8, ptr %111, i64 %114
  store i8 0, ptr %115, align 1, !tbaa !18
  br label %116

116:                                              ; preds = %108, %99
  %117 = load i64, ptr %5, align 8, !tbaa !9
  %118 = load ptr, ptr %4, align 8, !tbaa !5
  %119 = getelementptr inbounds %struct.XByteBuffer, ptr %118, i32 0, i32 1
  store i64 %117, ptr %119, align 8, !tbaa !16
  %120 = load ptr, ptr %4, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct.XByteBuffer, ptr %120, i32 0, i32 1
  %122 = load i64, ptr %121, align 8, !tbaa !16
  %123 = trunc i64 %122 to i32
  store i32 %123, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %124

124:                                              ; preds = %116, %77
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %125

125:                                              ; preds = %124, %56, %51, %32, %26, %10
  %126 = load i32, ptr %3, align 4
  ret i32 %126
}

; Function Attrs: nounwind uwtable
declare dso_local void @XByteBuffer_Clear(ptr noundef) #2

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 = { 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 = { 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 #4 = { nounwind allocsize(0) }
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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"XByteBuffer", !6, i64 0, !10, i64 8, !10, i64 16, !13, i64 24, !13, i64 26, !14, i64 28}
!13 = !{!"short", !7, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = !{!12, !14, i64 28}
!16 = !{!12, !10, i64 8}
!17 = !{!12, !10, i64 16}
!18 = !{!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: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @XByteBuffer_Resize(ptr noundef %0, i64 noundef %1) local_unnamed_addr #1 {
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %3, label %4

3:                                                ; preds = %2
  tail call void @XByteBuffer_Clear(ptr noundef %0)
  br label %41

4:                                                ; preds = %2
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %16

7:                                                ; preds = %4
  %8 = tail call noalias ptr @malloc(i64 noundef %1) #3
  store ptr %8, ptr %0, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %12

10:                                               ; preds = %7
  %11 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 -1, ptr %11, align 4, !tbaa !13
  br label %41

12:                                               ; preds = %7
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  store i64 %1, ptr %13, align 8, !tbaa !14
  %14 = getelementptr inbounds i8, ptr %0, i64 16
  store i64 0, ptr %14, align 8, !tbaa !15
  %15 = trunc i64 %1 to i32
  br label %41

16:                                               ; preds = %4
  %17 = getelementptr inbounds i8, ptr %0, i64 8
  %18 = load i64, ptr %17, align 8, !tbaa !14
  %.not44 = icmp eq i64 %18, 0
  br i1 %.not44, label %19, label %26

19:                                               ; preds = %16
  %20 = getelementptr inbounds i8, ptr %0, i64 16
  %21 = load i64, ptr %20, align 8, !tbaa !15
  %.not45 = icmp ult i64 %21, %1
  br i1 %.not45, label %24, label %22

22:                                               ; preds = %19
  %23 = trunc i64 %21 to i32
  br label %41

24:                                               ; preds = %19
  %25 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 -1, ptr %25, align 4, !tbaa !13
  br label %41

26:                                               ; preds = %16
  %27 = tail call ptr @realloc(ptr noundef nonnull %5, i64 noundef %1) #4
  store ptr %27, ptr %0, align 8, !tbaa !5
  %28 = icmp eq ptr %27, null
  br i1 %28, label %29, label %31

29:                                               ; preds = %26
  store ptr %5, ptr %0, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %0, i64 28
  store i32 -1, ptr %30, align 4, !tbaa !13
  br label %41

31:                                               ; preds = %26
  %32 = getelementptr inbounds i8, ptr %0, i64 16
  %33 = load i64, ptr %32, align 8, !tbaa !15
  %.not46 = icmp ult i64 %33, %1
  %34 = add i64 %1, -1
  %35 = select i1 %.not46, i64 %33, i64 %34
  store i64 %35, ptr %32, align 8, !tbaa !15
  %36 = icmp ult i64 %35, %1
  br i1 %36, label %37, label %39

37:                                               ; preds = %31
  %38 = getelementptr inbounds i8, ptr %27, i64 %35
  store i8 0, ptr %38, align 1, !tbaa !16
  br label %39

39:                                               ; preds = %37, %31
  store i64 %1, ptr %17, align 8, !tbaa !14
  %40 = trunc i64 %1 to i32
  br label %41

41:                                               ; preds = %29, %39, %24, %22, %12, %10, %3
  %.1 = phi i32 [ -1, %10 ], [ %15, %12 ], [ %23, %22 ], [ -1, %24 ], [ 0, %3 ], [ -1, %29 ], [ %40, %39 ]
  ret i32 %.1
}

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

; 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 #2

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 #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 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 #3 = { nounwind allocsize(0) }
attributes #4 = { 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, !7, i64 0}
!6 = !{!"XByteBuffer", !7, i64 0, !10, i64 8, !10, i64 16, !11, i64 24, !11, i64 26, !12, i64 28}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"short", !8, i64 0}
!12 = !{!"int", !8, i64 0}
!13 = !{!6, !12, i64 28}
!14 = !{!6, !10, i64 8}
!15 = !{!6, !10, i64 16}
!16 = !{!8, !8, i64 0}


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