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

; 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 [29 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: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #2

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

8:                                                ; preds = %1
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %13, label %11

11:                                               ; preds = %8
  store i32 2, ptr %5, align 4, !tbaa !9
  %12 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 191, i32 noundef %12, ptr noundef null)
  br label %71

13:                                               ; preds = %8
  br label %14

14:                                               ; preds = %13
  br label %15

15:                                               ; preds = %14
  br label %16

16:                                               ; preds = %15
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.ByteArray_st, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = icmp ne ptr %19, null
  br i1 %20, label %23, label %21

21:                                               ; preds = %16
  store i32 2, ptr %5, align 4, !tbaa !9
  %22 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 192, i32 noundef %22, ptr noundef null)
  br label %71

23:                                               ; preds = %16
  br label %24

24:                                               ; preds = %23
  br label %25

25:                                               ; preds = %24
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.ByteArray_st, ptr %26, i32 0, i32 1
  %28 = load i64, ptr %27, align 8, !tbaa !14
  %29 = sub i64 %28, 1
  store i64 %29, ptr %4, align 8, !tbaa !15
  br label %30

30:                                               ; preds = %44, %25
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.ByteArray_st, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !11
  %34 = load i64, ptr %4, align 8, !tbaa !15
  %35 = getelementptr inbounds i8, ptr %33, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !16
  %37 = zext i8 %36 to i32
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %42

39:                                               ; preds = %30
  %40 = load i64, ptr %4, align 8, !tbaa !15
  %41 = icmp ne i64 %40, 0
  br label %42

42:                                               ; preds = %39, %30
  %43 = phi i1 [ false, %30 ], [ %41, %39 ]
  br i1 %43, label %44, label %47

44:                                               ; preds = %42
  %45 = load i64, ptr %4, align 8, !tbaa !15
  %46 = add i64 %45, -1
  store i64 %46, ptr %4, align 8, !tbaa !15
  br label %30, !llvm.loop !17

47:                                               ; preds = %42
  %48 = load i64, ptr %4, align 8, !tbaa !15
  %49 = add i64 %48, 1
  store i64 %49, ptr %4, align 8, !tbaa !15
  br label %50

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

57:                                               ; preds = %50
  store i32 1, ptr %5, align 4, !tbaa !9
  %58 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 200, i32 noundef %58, ptr noundef null)
  store i32 4, ptr %7, align 4
  br label %63

59:                                               ; preds = %50
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.ByteArray_st, ptr %61, i32 0, i32 0
  store ptr %60, ptr %62, align 8, !tbaa !11
  store i32 0, ptr %7, align 4
  br label %63

63:                                               ; preds = %59, %57
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %64 = load i32, ptr %7, align 4
  switch i32 %64, label %73 [
    i32 0, label %65
    i32 4, label %71
  ]

65:                                               ; preds = %63
  br label %66

66:                                               ; preds = %65
  br label %67

67:                                               ; preds = %66
  %68 = load i64, ptr %4, align 8, !tbaa !15
  %69 = load ptr, ptr %3, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.ByteArray_st, ptr %69, i32 0, i32 1
  store i64 %68, ptr %70, align 8, !tbaa !14
  br label %71

71:                                               ; preds = %67, %63, %21, %11
  %72 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %72, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %73

73:                                               ; preds = %71, %63
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %74 = load i32, ptr %2, align 4
  ret i32 %74
}

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 = { 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 #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 }
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, !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, !6, i64 0}
!12 = !{!"ByteArray_st", !6, i64 0, !13, i64 8}
!13 = !{!"long", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!13, !13, i64 0}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; 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 [29 x i8], align 1

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

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

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

2:                                                ; preds = %1
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 191, i32 noundef 2, ptr noundef null) #3
  br label %20

3:                                                ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !5
  %.not24 = icmp eq ptr %4, null
  br i1 %.not24, label %5, label %6

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

6:                                                ; preds = %3
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !11
  br label %9

9:                                                ; preds = %9, %6
  %.020.in = phi i64 [ %8, %6 ], [ %.020, %9 ]
  %.020 = add i64 %.020.in, -1
  %10 = getelementptr inbounds i8, ptr %4, i64 %.020
  %11 = load i8, ptr %10, align 1, !tbaa !12
  %12 = icmp eq i8 %11, 0
  %13 = icmp ne i64 %.020, 0
  %14 = and i1 %12, %13
  br i1 %14, label %9, label %15, !llvm.loop !13

15:                                               ; preds = %9
  %16 = tail call ptr @realloc(ptr noundef nonnull %4, i64 noundef %.020.in) #4
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %15
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 200, i32 noundef 1, ptr noundef null) #3
  br label %20

19:                                               ; preds = %15
  store ptr %16, ptr %0, align 8, !tbaa !5
  store i64 %.020.in, ptr %7, align 8, !tbaa !11
  br label %20

20:                                               ; preds = %18, %2, %5, %19
  %.1 = phi i32 [ 1, %18 ], [ 0, %19 ], [ 2, %5 ], [ 2, %2 ]
  ret i32 %.1
}

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 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 #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 }
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 = !{!"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, !10, i64 8}
!12 = !{!8, !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass