; LLVM IR from peacemakr-io/peacemakr-core-crypto :: src/core/src (Key)

; 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.PeacemakrKey = type { %struct.crypto_config_t, %union.anon, ptr }
%struct.crypto_config_t = type { i32, i32, i32, i32 }
%union.anon = type { ptr }

@.str.20 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [32 x i8], align 1
@peacemakr_global_calloc = external global ptr, align 8
@__FUNCTION__.peacemakr_key_priv_to_pem = external hidden unnamed_addr constant [26 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.47 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.48 = external hidden unnamed_addr constant [41 x i8], align 1
@.str.49 = external hidden unnamed_addr constant [32 x i8], align 1

declare void @log_printf(ptr noundef, i32 noundef, ptr noundef, ...) #0

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

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

declare void @openssl_log(ptr noundef, i32 noundef) #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

declare ptr @BIO_new(ptr noundef) #0

declare ptr @BIO_s_secmem() #0

declare i32 @BIO_free(ptr noundef) #0

declare i64 @BIO_ctrl(ptr noundef, i32 noundef, i64 noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @peacemakr_key_priv_to_pem(ptr noundef %0, ptr noundef %1, ptr noundef %2) #3 {
  %4 = alloca i1, align 1
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %3
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 863, ptr noundef @.str.45)
  store i1 false, ptr %4, align 1
  br label %69

14:                                               ; preds = %3
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %18

17:                                               ; preds = %14
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 864, ptr noundef @.str.46)
  store i1 false, ptr %4, align 1
  br label %69

18:                                               ; preds = %14
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %21, label %22

21:                                               ; preds = %18
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 866, ptr noundef @.str.47)
  store i1 false, ptr %4, align 1
  br label %69

22:                                               ; preds = %18
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.PeacemakrKey, ptr %23, i32 0, i32 0
  %25 = getelementptr inbounds %struct.crypto_config_t, ptr %24, i32 0, i32 0
  %26 = load i32, ptr %25, align 8, !tbaa !9
  %27 = icmp ne i32 %26, 1
  br i1 %27, label %28, label %29

28:                                               ; preds = %22
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 869, ptr noundef @.str.48)
  store i1 false, ptr %4, align 1
  br label %69

29:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %30 = call ptr @BIO_s_secmem()
  %31 = call ptr @BIO_new(ptr noundef %30)
  store ptr %31, ptr %8, align 8, !tbaa !5
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.PeacemakrKey, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = call i32 @PEM_write_bio_PrivateKey(ptr noundef %32, ptr noundef %35, ptr noundef null, ptr noundef null, i32 noundef 0, ptr noundef null, ptr noundef null)
  %37 = icmp ne i32 %36, 0
  br i1 %37, label %39, label %38

38:                                               ; preds = %29
  call void @openssl_log(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 876)
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 877, ptr noundef @.str.49)
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %68

39:                                               ; preds = %29
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = call i64 @BIO_ctrl(ptr noundef %40, i32 noundef 2, i64 noundef 0, ptr noundef null)
  %42 = trunc i64 %41 to i32
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %45

44:                                               ; preds = %39
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 882, ptr noundef @.str.20)
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %68

45:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store ptr null, ptr %10, align 8, !tbaa !5
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = call i64 @BIO_ctrl(ptr noundef %46, i32 noundef 3, i64 noundef 0, ptr noundef %10)
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %47, ptr %48, align 8, !tbaa !14
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = icmp eq ptr %49, null
  br i1 %50, label %51, label %54

51:                                               ; preds = %45
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  %53 = call i32 @BIO_free(ptr noundef %52)
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 889, ptr noundef @.str.21)
  store i1 false, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %67

54:                                               ; preds = %45
  %55 = load ptr, ptr @peacemakr_global_calloc, align 8, !tbaa !5
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load i64, ptr %56, align 8, !tbaa !14
  %58 = call ptr %55(i64 noundef %57, i64 noundef 1)
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %58, ptr %59, align 8, !tbaa !5
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = load i64, ptr %63, align 8, !tbaa !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %61, ptr align 1 %62, i64 %64, i1 false)
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = call i32 @BIO_free(ptr noundef %65)
  store i1 true, ptr %4, align 1
  store i32 1, ptr %9, align 4
  br label %67

67:                                               ; preds = %54, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %68

68:                                               ; preds = %67, %44, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %69

69:                                               ; preds = %68, %28, %21, %17, %13
  %70 = load i1, ptr %4, align 1
  ret i1 %70
}

declare i32 @PEM_write_bio_PrivateKey(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 }

!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, !12, i64 0}
!10 = !{!"PeacemakrKey", !11, i64 0, !7, i64 16, !6, i64 24}
!11 = !{!"", !12, i64 0, !12, i64 4, !12, i64 8, !12, i64 12}
!12 = !{!"int", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"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.20 = external hidden unnamed_addr constant [23 x i8], align 1
@.str.21 = external hidden unnamed_addr constant [32 x i8], align 1
@peacemakr_global_calloc = external local_unnamed_addr global ptr, align 8
@__FUNCTION__.peacemakr_key_priv_to_pem = external hidden unnamed_addr constant [26 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [37 x i8], align 1
@.str.47 = external hidden unnamed_addr constant [38 x i8], align 1
@.str.48 = external hidden unnamed_addr constant [41 x i8], align 1
@.str.49 = external hidden unnamed_addr constant [32 x i8], align 1

declare void @log_printf(ptr noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #0

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

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

declare void @openssl_log(ptr noundef, i32 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) #2

declare ptr @BIO_new(ptr noundef) local_unnamed_addr #0

declare ptr @BIO_s_secmem() local_unnamed_addr #0

declare i32 @BIO_free(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @peacemakr_key_priv_to_pem(ptr noundef readonly %0, ptr noundef writeonly %1, ptr noundef %2) local_unnamed_addr #3 {
  %4 = alloca ptr, align 8
  %5 = icmp eq ptr %0, null
  br i1 %5, label %6, label %7

6:                                                ; preds = %3
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 863, ptr noundef nonnull @.str.45) #4
  br label %40

7:                                                ; preds = %3
  %8 = icmp eq ptr %1, null
  br i1 %8, label %9, label %10

9:                                                ; preds = %7
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 864, ptr noundef nonnull @.str.46) #4
  br label %40

10:                                               ; preds = %7
  %11 = icmp eq ptr %2, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %10
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 866, ptr noundef nonnull @.str.47) #4
  br label %40

13:                                               ; preds = %10
  %14 = load i32, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i32 %14, 1
  br i1 %.not, label %16, label %15

15:                                               ; preds = %13
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 869, ptr noundef nonnull @.str.48) #4
  br label %40

16:                                               ; preds = %13
  %17 = tail call ptr @BIO_s_secmem() #4
  %18 = tail call ptr @BIO_new(ptr noundef %17) #4
  %19 = getelementptr inbounds i8, ptr %0, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !12
  %21 = tail call i32 @PEM_write_bio_PrivateKey(ptr noundef %18, ptr noundef %20, ptr noundef null, ptr noundef null, i32 noundef 0, ptr noundef null, ptr noundef null) #4
  %.not19 = icmp eq i32 %21, 0
  br i1 %.not19, label %22, label %23

22:                                               ; preds = %16
  tail call void @openssl_log(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 876) #4
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 877, ptr noundef nonnull @.str.49) #4
  br label %40

23:                                               ; preds = %16
  %24 = tail call i64 @BIO_ctrl(ptr noundef %18, i32 noundef 2, i64 noundef 0, ptr noundef null) #4
  %25 = and i64 %24, 4294967295
  %.not20 = icmp eq i64 %25, 0
  br i1 %.not20, label %27, label %26

26:                                               ; preds = %23
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 882, ptr noundef nonnull @.str.20) #4
  br label %40

27:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #4
  store ptr null, ptr %4, align 8, !tbaa !13
  %28 = call i64 @BIO_ctrl(ptr noundef %18, i32 noundef 3, i64 noundef 0, ptr noundef nonnull %4) #4
  store i64 %28, ptr %2, align 8, !tbaa !14
  %29 = load ptr, ptr %4, align 8, !tbaa !13
  %30 = icmp ne ptr %29, null
  br i1 %30, label %33, label %31

31:                                               ; preds = %27
  %32 = call i32 @BIO_free(ptr noundef %18) #4
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_priv_to_pem, i32 noundef 889, ptr noundef nonnull @.str.21) #4
  br label %39

33:                                               ; preds = %27
  %34 = load ptr, ptr @peacemakr_global_calloc, align 8, !tbaa !13
  %35 = call ptr %34(i64 noundef %28, i64 noundef 1) #4
  store ptr %35, ptr %1, align 8, !tbaa !13
  %36 = load ptr, ptr %4, align 8, !tbaa !13
  %37 = load i64, ptr %2, align 8, !tbaa !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %35, ptr align 1 %36, i64 %37, i1 false)
  %38 = call i32 @BIO_free(ptr noundef %18) #4
  br label %39

39:                                               ; preds = %33, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #4
  br label %40

40:                                               ; preds = %22, %26, %39, %15, %12, %9, %6
  %.2 = phi i1 [ false, %6 ], [ false, %9 ], [ false, %12 ], [ false, %15 ], [ false, %26 ], [ %30, %39 ], [ false, %22 ]
  ret i1 %.2
}

declare i32 @PEM_write_bio_PrivateKey(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, ptr noundef, 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress 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 }

!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, !8, i64 0}
!6 = !{!"PeacemakrKey", !7, i64 0, !9, i64 16, !11, i64 24}
!7 = !{!"", !8, i64 0, !8, i64 4, !8, i64 8, !8, i64 12}
!8 = !{!"int", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"any pointer", !9, i64 0}
!12 = !{!9, !9, i64 0}
!13 = !{!11, !11, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"long", !9, i64 0}


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