; 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 = external hidden unnamed_addr constant [56 x i8], align 1
@peacemakr_global_malloc = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1
@__FUNCTION__.peacemakr_key_new_symmetric = external hidden unnamed_addr constant [28 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

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

12:                                               ; preds = %2
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_new_symmetric, i32 noundef 240, ptr noundef @.str)
  store ptr null, ptr %3, align 8
  br label %52

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %14 = load ptr, ptr @peacemakr_global_malloc, align 8, !tbaa !9
  %15 = call ptr %14(i64 noundef 32)
  store ptr %15, ptr %6, align 8, !tbaa !9
  %16 = load ptr, ptr %6, align 8, !tbaa !9
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %13
  call void (ptr, i32, ptr, ...) @log_printf(ptr noundef @__FUNCTION__.peacemakr_key_new_symmetric, i32 noundef 243, ptr noundef @.str.1)
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %51

19:                                               ; preds = %13
  %20 = load ptr, ptr %6, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.PeacemakrKey, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds %struct.crypto_config_t, ptr %21, i32 0, i32 0
  store i32 0, ptr %22, align 8, !tbaa !11
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = load ptr, ptr %6, align 8, !tbaa !9
  %25 = getelementptr inbounds %struct.PeacemakrKey, ptr %24, i32 0, i32 0
  %26 = getelementptr inbounds %struct.crypto_config_t, ptr %25, i32 0, i32 1
  store i32 %23, ptr %26, align 4, !tbaa !14
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  %28 = getelementptr inbounds %struct.PeacemakrKey, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.crypto_config_t, ptr %28, i32 0, i32 2
  store i32 0, ptr %29, align 8, !tbaa !15
  %30 = load ptr, ptr %6, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.PeacemakrKey, ptr %30, i32 0, i32 0
  %32 = getelementptr inbounds %struct.crypto_config_t, ptr %31, i32 0, i32 3
  store i32 0, ptr %32, align 4, !tbaa !16
  %33 = load ptr, ptr %6, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.PeacemakrKey, ptr %33, i32 0, i32 1
  store ptr null, ptr %34, align 8, !tbaa !17
  %35 = load ptr, ptr %6, align 8, !tbaa !9
  %36 = getelementptr inbounds %struct.PeacemakrKey, ptr %35, i32 0, i32 2
  store ptr null, ptr %36, align 8, !tbaa !18
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %37 = load i32, ptr %4, align 4, !tbaa !5
  %38 = call ptr @parse_cipher(i32 noundef %37)
  store ptr %38, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %39 = load ptr, ptr %8, align 8, !tbaa !9
  %40 = call i32 @EVP_CIPHER_get_key_length(ptr noundef %39)
  %41 = sext i32 %40 to i64
  store i64 %41, ptr %9, align 8, !tbaa !19
  %42 = load i64, ptr %9, align 8, !tbaa !19
  %43 = call ptr @buffer_new(i64 noundef %42)
  %44 = load ptr, ptr %6, align 8, !tbaa !9
  %45 = getelementptr inbounds %struct.PeacemakrKey, ptr %44, i32 0, i32 1
  store ptr %43, ptr %45, align 8, !tbaa !17
  %46 = load ptr, ptr %6, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.PeacemakrKey, ptr %46, i32 0, i32 1
  %48 = load ptr, ptr %47, align 8, !tbaa !17
  %49 = load ptr, ptr %5, align 8, !tbaa !9
  call void @buffer_init_rand(ptr noundef %48, ptr noundef %49)
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %50, ptr %3, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %51

51:                                               ; preds = %19, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %52

52:                                               ; preds = %51, %12
  %53 = load ptr, ptr %3, align 8
  ret ptr %53
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @parse_cipher(i32 noundef) #3

declare i32 @EVP_CIPHER_get_key_length(ptr noundef) #0

declare ptr @buffer_new(i64 noundef) #0

declare void @buffer_init_rand(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 = { 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 = { inlinehint 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"PeacemakrKey", !13, i64 0, !7, i64 16, !10, i64 24}
!13 = !{!"", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12}
!14 = !{!12, !6, i64 4}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !6, i64 12}
!17 = !{!7, !7, i64 0}
!18 = !{!12, !10, i64 24}
!19 = !{!20, !20, i64 0}
!20 = !{!"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 [56 x i8], align 1
@peacemakr_global_malloc = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [15 x i8], align 1
@__FUNCTION__.peacemakr_key_new_symmetric = external hidden unnamed_addr constant [28 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local ptr @peacemakr_key_new_symmetric(i32 noundef %0, ptr noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq ptr %1, null
  br i1 %3, label %4, label %5

4:                                                ; preds = %2
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_new_symmetric, i32 noundef 240, ptr noundef nonnull @.str) #4
  br label %18

5:                                                ; preds = %2
  %6 = load ptr, ptr @peacemakr_global_malloc, align 8, !tbaa !5
  %7 = tail call ptr %6(i64 noundef 32) #4
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %10

9:                                                ; preds = %5
  tail call void (ptr, i32, ptr, ...) @log_printf(ptr noundef nonnull @__FUNCTION__.peacemakr_key_new_symmetric, i32 noundef 243, ptr noundef nonnull @.str.1) #4
  br label %18

10:                                               ; preds = %5
  store i32 0, ptr %7, align 8, !tbaa !9
  %11 = getelementptr inbounds i8, ptr %7, i64 4
  store i32 %0, ptr %11, align 4, !tbaa !13
  %12 = getelementptr inbounds i8, ptr %7, i64 8
  %13 = getelementptr inbounds i8, ptr %7, i64 16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %12, i8 0, i64 24, i1 false)
  %14 = tail call ptr @parse_cipher(i32 noundef %0)
  %15 = tail call i32 @EVP_CIPHER_get_key_length(ptr noundef %14) #4
  %16 = sext i32 %15 to i64
  %17 = tail call ptr @buffer_new(i64 noundef %16) #4
  store ptr %17, ptr %13, align 8, !tbaa !14
  tail call void @buffer_init_rand(ptr noundef %17, ptr noundef nonnull %1) #4
  br label %18

18:                                               ; preds = %9, %10, %4
  %.1 = phi ptr [ null, %4 ], [ null, %9 ], [ %7, %10 ]
  ret ptr %.1
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden ptr @parse_cipher(i32 noundef) local_unnamed_addr #2

declare i32 @EVP_CIPHER_get_key_length(ptr noundef) local_unnamed_addr #0

declare ptr @buffer_new(i64 noundef) local_unnamed_addr #0

declare void @buffer_init_rand(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { 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 = { inlinehint 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!10, !12, i64 4}
!14 = !{!7, !7, i64 0}


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