; LLVM IR from specinfo-ua/UAPKI :: library/cm-pkcs12/src/storage (jks-buffer)

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

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

declare i64 @ba_get_len(ptr noundef) #1

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

7:                                                ; preds = %2
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = icmp ne ptr %8, null
  br i1 %9, label %12, label %10

10:                                               ; preds = %7
  store i32 2, ptr %5, align 4, !tbaa !9
  %11 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 111, i32 noundef %11, ptr noundef null)
  br label %83

12:                                               ; preds = %7
  br label %13

13:                                               ; preds = %12
  br label %14

14:                                               ; preds = %13
  br label %15

15:                                               ; preds = %14
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.Buffer_st, ptr %16, i32 0, i32 0
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = call i64 @ba_get_len(ptr noundef %18)
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Buffer_st, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !14
  %23 = add i64 %22, 4
  %24 = icmp uge i64 %19, %23
  br i1 %24, label %27, label %25

25:                                               ; preds = %15
  store i32 2, ptr %5, align 4, !tbaa !9
  %26 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 112, i32 noundef %26, ptr noundef null)
  br label %83

27:                                               ; preds = %15
  br label %28

28:                                               ; preds = %27
  br label %29

29:                                               ; preds = %28
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.Buffer_st, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  %33 = call ptr @ba_get_buf(ptr noundef %32)
  store ptr %33, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.Buffer_st, ptr %35, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = add i64 %37, 1
  store i64 %38, ptr %36, align 8, !tbaa !14
  %39 = getelementptr inbounds i8, ptr %34, i64 %37
  %40 = load i8, ptr %39, align 1, !tbaa !15
  %41 = zext i8 %40 to i32
  %42 = shl i32 %41, 24
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %42, ptr %43, align 4, !tbaa !9
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Buffer_st, ptr %45, i32 0, i32 1
  %47 = load i64, ptr %46, align 8, !tbaa !14
  %48 = add i64 %47, 1
  store i64 %48, ptr %46, align 8, !tbaa !14
  %49 = getelementptr inbounds i8, ptr %44, i64 %47
  %50 = load i8, ptr %49, align 1, !tbaa !15
  %51 = zext i8 %50 to i32
  %52 = and i32 %51, 255
  %53 = shl i32 %52, 16
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load i32, ptr %54, align 4, !tbaa !9
  %56 = or i32 %55, %53
  store i32 %56, ptr %54, align 4, !tbaa !9
  %57 = load ptr, ptr %6, align 8, !tbaa !5
  %58 = load ptr, ptr %3, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Buffer_st, ptr %58, i32 0, i32 1
  %60 = load i64, ptr %59, align 8, !tbaa !14
  %61 = add i64 %60, 1
  store i64 %61, ptr %59, align 8, !tbaa !14
  %62 = getelementptr inbounds i8, ptr %57, i64 %60
  %63 = load i8, ptr %62, align 1, !tbaa !15
  %64 = zext i8 %63 to i32
  %65 = and i32 %64, 255
  %66 = shl i32 %65, 8
  %67 = load ptr, ptr %4, align 8, !tbaa !5
  %68 = load i32, ptr %67, align 4, !tbaa !9
  %69 = or i32 %68, %66
  store i32 %69, ptr %67, align 4, !tbaa !9
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Buffer_st, ptr %71, i32 0, i32 1
  %73 = load i64, ptr %72, align 8, !tbaa !14
  %74 = add i64 %73, 1
  store i64 %74, ptr %72, align 8, !tbaa !14
  %75 = getelementptr inbounds i8, ptr %70, i64 %73
  %76 = load i8, ptr %75, align 1, !tbaa !15
  %77 = zext i8 %76 to i32
  %78 = and i32 %77, 255
  %79 = shl i32 %78, 0
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  %81 = load i32, ptr %80, align 4, !tbaa !9
  %82 = or i32 %81, %79
  store i32 %82, ptr %80, align 4, !tbaa !9
  br label %83

83:                                               ; preds = %29, %25, %10
  %84 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %84
}

declare ptr @ba_get_buf(ptr noundef) #1

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 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 }

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

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

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

declare i64 @ba_get_len(ptr noundef) local_unnamed_addr #0

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

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

4:                                                ; preds = %2
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %6 = tail call i64 @ba_get_len(ptr noundef %5) #2
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %8 = load i64, ptr %7, align 8, !tbaa !11
  %9 = add i64 %8, 4
  %.not19 = icmp ult i64 %6, %9
  br i1 %.not19, label %10, label %11

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

11:                                               ; preds = %4
  %12 = load ptr, ptr %0, align 8, !tbaa !5
  %13 = tail call ptr @ba_get_buf(ptr noundef %12) #2
  %14 = load i64, ptr %7, align 8, !tbaa !11
  %15 = add i64 %14, 1
  store i64 %15, ptr %7, align 8, !tbaa !11
  %16 = getelementptr inbounds i8, ptr %13, i64 %14
  %17 = load i8, ptr %16, align 1, !tbaa !12
  %18 = zext i8 %17 to i32
  %19 = shl nuw i32 %18, 24
  store i32 %19, ptr %1, align 4, !tbaa !13
  %20 = add i64 %14, 2
  store i64 %20, ptr %7, align 8, !tbaa !11
  %21 = getelementptr inbounds i8, ptr %13, i64 %15
  %22 = load i8, ptr %21, align 1, !tbaa !12
  %23 = zext i8 %22 to i32
  %24 = shl nuw nsw i32 %23, 16
  %25 = or disjoint i32 %24, %19
  store i32 %25, ptr %1, align 4, !tbaa !13
  %26 = add i64 %14, 3
  store i64 %26, ptr %7, align 8, !tbaa !11
  %27 = getelementptr inbounds i8, ptr %13, i64 %20
  %28 = load i8, ptr %27, align 1, !tbaa !12
  %29 = zext i8 %28 to i32
  %30 = shl nuw nsw i32 %29, 8
  %31 = or disjoint i32 %30, %25
  store i32 %31, ptr %1, align 4, !tbaa !13
  %32 = add i64 %14, 4
  store i64 %32, ptr %7, align 8, !tbaa !11
  %33 = getelementptr inbounds i8, ptr %13, i64 %26
  %34 = load i8, ptr %33, align 1, !tbaa !12
  %35 = zext i8 %34 to i32
  %36 = or disjoint i32 %31, %35
  store i32 %36, ptr %1, align 4, !tbaa !13
  br label %37

37:                                               ; preds = %11, %10, %3
  %.0 = phi i32 [ 0, %11 ], [ 2, %10 ], [ 2, %3 ]
  ret i32 %.0
}

declare ptr @ba_get_buf(ptr noundef) local_unnamed_addr #0

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 = { 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 = { 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, !7, i64 0}
!6 = !{!"Buffer_st", !7, i64 0, !10, i64 8, !7, 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 8}
!12 = !{!8, !8, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !8, i64 0}


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