; LLVM IR from specinfo-ua/UAPKI :: library/common/pkix (ba-utils)

; 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"

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

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

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

declare ptr @ba_get_buf_const(ptr noundef) #1

declare i64 @ba_get_len(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @fopen_utf8(ptr noundef, i32 noundef) #2

declare i32 @fclose(ptr noundef) #1

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

8:                                                ; preds = %2
  %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 176, i32 noundef %12, ptr noundef null)
  br label %50

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 %4, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %21, label %19

19:                                               ; preds = %16
  store i32 2, ptr %5, align 4, !tbaa !9
  %20 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 177, i32 noundef %20, ptr noundef null)
  br label %50

21:                                               ; preds = %16
  br label %22

22:                                               ; preds = %21
  br label %23

23:                                               ; preds = %22
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = call ptr @fopen_utf8(ptr noundef %24, i32 noundef 1)
  store ptr %25, ptr %6, align 8, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = icmp ne ptr %26, null
  br i1 %27, label %33, label %28

28:                                               ; preds = %23
  br label %29

29:                                               ; preds = %28
  store i32 4128, ptr %5, align 4, !tbaa !9
  %30 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 182, i32 noundef %30, ptr noundef null)
  br label %50

31:                                               ; No predecessors!
  br label %32

32:                                               ; preds = %31
  br label %33

33:                                               ; preds = %32, %23
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = call ptr @ba_get_buf_const(ptr noundef %34)
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = call i64 @ba_get_len(ptr noundef %36)
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = call i64 @fwrite(ptr noundef %35, i64 noundef 1, i64 noundef %37, ptr noundef %38)
  store i64 %39, ptr %7, align 8, !tbaa !11
  %40 = load i64, ptr %7, align 8, !tbaa !11
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = call i64 @ba_get_len(ptr noundef %41)
  %43 = icmp ne i64 %40, %42
  br i1 %43, label %44, label %49

44:                                               ; preds = %33
  br label %45

45:                                               ; preds = %44
  store i32 4130, ptr %5, align 4, !tbaa !9
  %46 = load i32, ptr %5, align 4, !tbaa !9
  call void @stacktrace_create(ptr noundef @.str, i64 noundef 187, i32 noundef %46, ptr noundef null)
  br label %50

47:                                               ; No predecessors!
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48, %33
  br label %50

50:                                               ; preds = %49, %45, %29, %19, %11
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %56

53:                                               ; preds = %50
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = call i32 @fclose(ptr noundef %54)
  br label %56

56:                                               ; preds = %53, %50
  %57 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret i32 %57
}

declare i64 @fwrite(ptr noundef, i64 noundef, i64 noundef, 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, !12, i64 0}
!12 = !{!"long", !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 [23 x i8], align 1

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

declare ptr @ba_get_buf_const(ptr noundef) local_unnamed_addr #0

declare i64 @ba_get_len(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @fopen_utf8(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 4131) i32 @ba_to_file(ptr noundef %0, ptr noundef %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 176, i32 noundef 2, ptr noundef null) #3
  br label %.thread

4:                                                ; preds = %2
  %.not19 = icmp eq ptr %1, null
  br i1 %.not19, label %5, label %6

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

6:                                                ; preds = %4
  %7 = tail call ptr @fopen_utf8(ptr noundef nonnull %1, i32 noundef 1)
  %.not20 = icmp eq ptr %7, null
  br i1 %.not20, label %8, label %9

8:                                                ; preds = %6
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 182, i32 noundef 4128, ptr noundef null) #3
  br label %.thread

9:                                                ; preds = %6
  %10 = tail call ptr @ba_get_buf_const(ptr noundef nonnull %0) #3
  %11 = tail call i64 @ba_get_len(ptr noundef nonnull %0) #3
  %12 = tail call i64 @fwrite(ptr noundef %10, i64 noundef 1, i64 noundef %11, ptr noundef nonnull %7)
  %13 = tail call i64 @ba_get_len(ptr noundef nonnull %0) #3
  %.not21 = icmp eq i64 %12, %13
  br i1 %.not21, label %15, label %14

14:                                               ; preds = %9
  tail call void @stacktrace_create(ptr noundef nonnull @.str, i64 noundef 187, i32 noundef 4130, ptr noundef null) #3
  br label %15

15:                                               ; preds = %14, %9
  %.0 = phi i32 [ 4130, %14 ], [ 0, %9 ]
  %16 = tail call i32 @fclose(ptr noundef nonnull %7)
  br label %.thread

.thread:                                          ; preds = %3, %5, %8, %15
  %.026 = phi i32 [ %.0, %15 ], [ 2, %3 ], [ 2, %5 ], [ 4128, %8 ]
  ret i32 %.026
}

; Function Attrs: nofree nounwind
declare noundef i64 @fwrite(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

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 = { 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 #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)"}


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