; LLVM IR from specinfo-ua/UAPKI :: library/uapkic/src (byte-array)

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

@.str = external hidden unnamed_addr constant [20 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

declare void @stacktrace_create(ptr noundef, i64 noundef, i32 noundef, ptr 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local i32 @ba_append(ptr noundef %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) #3 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i64 %1, ptr %7, align 8, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  store i32 0, ptr %10, align 4, !tbaa !11
  br label %13

13:                                               ; preds = %4
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = icmp ne ptr %14, null
  br i1 %15, label %18, label %16

16:                                               ; preds = %13
  store i32 2, ptr %10, align 4, !tbaa !11
  %17 = load i32, ptr %10, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 319, i32 noundef %17, ptr noundef null)
  br label %91

18:                                               ; preds = %13
  br label %19

19:                                               ; preds = %18
  br label %20

20:                                               ; preds = %19
  br label %21

21:                                               ; preds = %20
  %22 = load ptr, ptr %9, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %26, label %24

24:                                               ; preds = %21
  store i32 2, ptr %10, align 4, !tbaa !11
  %25 = load i32, ptr %10, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 320, i32 noundef %25, ptr noundef null)
  br label %91

26:                                               ; preds = %21
  br label %27

27:                                               ; preds = %26
  br label %28

28:                                               ; preds = %27
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = icmp eq i64 %29, 0
  br i1 %30, label %31, label %37

31:                                               ; preds = %28
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.ByteArray_st, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !13
  %35 = load i64, ptr %7, align 8, !tbaa !9
  %36 = sub i64 %34, %35
  store i64 %36, ptr %8, align 8, !tbaa !9
  br label %37

37:                                               ; preds = %31, %28
  br label %38

38:                                               ; preds = %37
  %39 = load i64, ptr %7, align 8, !tbaa !9
  %40 = load i64, ptr %8, align 8, !tbaa !9
  %41 = add i64 %39, %40
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.ByteArray_st, ptr %42, i32 0, i32 1
  %44 = load i64, ptr %43, align 8, !tbaa !13
  %45 = icmp ule i64 %41, %44
  br i1 %45, label %48, label %46

46:                                               ; preds = %38
  store i32 2, ptr %10, align 4, !tbaa !11
  %47 = load i32, ptr %10, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 325, i32 noundef %47, ptr noundef null)
  br label %91

48:                                               ; preds = %38
  br label %49

49:                                               ; preds = %48
  br label %50

50:                                               ; preds = %49
  br label %51

51:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  store ptr null, ptr %11, align 8, !tbaa !5
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.ByteArray_st, ptr %52, i32 0, i32 0
  %54 = load ptr, ptr %53, align 8, !tbaa !15
  %55 = load ptr, ptr %9, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.ByteArray_st, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !13
  %58 = load i64, ptr %8, align 8, !tbaa !9
  %59 = add i64 %57, %58
  %60 = call ptr @realloc(ptr noundef %54, i64 noundef %59) #6
  store ptr %60, ptr %11, align 8, !tbaa !5
  %61 = icmp eq ptr null, %60
  br i1 %61, label %62, label %64

62:                                               ; preds = %51
  store i32 1, ptr %10, align 4, !tbaa !11
  %63 = load i32, ptr %10, align 4, !tbaa !11
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 326, i32 noundef %63, ptr noundef null)
  store i32 4, ptr %12, align 4
  br label %68

64:                                               ; preds = %51
  %65 = load ptr, ptr %11, align 8, !tbaa !5
  %66 = load ptr, ptr %9, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.ByteArray_st, ptr %66, i32 0, i32 0
  store ptr %65, ptr %67, align 8, !tbaa !15
  store i32 0, ptr %12, align 4
  br label %68

68:                                               ; preds = %64, %62
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  %69 = load i32, ptr %12, align 4
  switch i32 %69, label %93 [
    i32 0, label %70
    i32 4, label %91
  ]

70:                                               ; preds = %68
  br label %71

71:                                               ; preds = %70
  br label %72

72:                                               ; preds = %71
  %73 = load ptr, ptr %9, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.ByteArray_st, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !15
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.ByteArray_st, ptr %76, i32 0, i32 1
  %78 = load i64, ptr %77, align 8, !tbaa !13
  %79 = getelementptr inbounds i8, ptr %75, i64 %78
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.ByteArray_st, ptr %80, i32 0, i32 0
  %82 = load ptr, ptr %81, align 8, !tbaa !15
  %83 = load i64, ptr %7, align 8, !tbaa !9
  %84 = getelementptr inbounds i8, ptr %82, i64 %83
  %85 = load i64, ptr %8, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %79, ptr align 1 %84, i64 %85, i1 false)
  %86 = load i64, ptr %8, align 8, !tbaa !9
  %87 = load ptr, ptr %9, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.ByteArray_st, ptr %87, i32 0, i32 1
  %89 = load i64, ptr %88, align 8, !tbaa !13
  %90 = add i64 %89, %86
  store i64 %90, ptr %88, align 8, !tbaa !13
  br label %91

91:                                               ; preds = %72, %68, %46, %24, %16
  %92 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %92, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %93

93:                                               ; preds = %91, %68
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %94 = load i32, ptr %5, align 4
  ret i32 %94
}

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

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

@.str = external hidden unnamed_addr constant [20 x i8], align 1

declare void @stacktrace_create(ptr noundef, i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @ba_append(ptr noundef readonly %0, i64 noundef %1, i64 noundef %2, ptr noundef %3) local_unnamed_addr #2 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %5, label %6

5:                                                ; preds = %4
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 319, i32 noundef 2, ptr noundef null) #4
  br label %28

6:                                                ; preds = %4
  %.not33 = icmp eq ptr %3, null
  br i1 %.not33, label %7, label %._crit_edge

7:                                                ; preds = %6
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 320, i32 noundef 2, ptr noundef null) #4
  br label %28

._crit_edge:                                      ; preds = %6
  %8 = icmp eq i64 %2, 0
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = sub i64 %10, %1
  %.028 = select i1 %8, i64 %11, i64 %2
  %12 = add i64 %.028, %1
  %.not34 = icmp ugt i64 %12, %10
  br i1 %.not34, label %13, label %14

13:                                               ; preds = %._crit_edge
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 325, i32 noundef 2, ptr noundef null) #4
  br label %28

14:                                               ; preds = %._crit_edge
  %15 = load ptr, ptr %3, align 8, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %3, i64 8
  %17 = load i64, ptr %16, align 8, !tbaa !5
  %18 = add i64 %17, %.028
  %19 = tail call ptr @realloc(ptr noundef %15, i64 noundef %18) #5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %14
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 326, i32 noundef 1, ptr noundef null) #4
  br label %28

22:                                               ; preds = %14
  store ptr %19, ptr %3, align 8, !tbaa !11
  %23 = load i64, ptr %16, align 8, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %19, i64 %23
  %25 = load ptr, ptr %0, align 8, !tbaa !11
  %26 = getelementptr inbounds i8, ptr %25, i64 %1
  tail call void @llvm.memcpy.p0.p0.i64(ptr nonnull align 1 %24, ptr align 1 %26, i64 %.028, i1 false)
  %27 = add i64 %23, %.028
  store i64 %27, ptr %16, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %21, %5, %7, %13, %22
  %.1 = phi i32 [ 1, %21 ], [ 0, %22 ], [ 2, %13 ], [ 2, %7 ], [ 2, %5 ]
  ret i32 %.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 #3

attributes #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 nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #4 = { nounwind }
attributes #5 = { 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 8}
!6 = !{!"ByteArray_st", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !7, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass