; LLVM IR from openssl/openssl :: crypto (mem_sec)

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

@secure_mem_initialized = external hidden global i32, align 4
@sec_malloc_lock = external hidden global ptr, align 8
@secure_mem_used = external hidden global i64, align 8

; 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

; Function Attrs: nounwind uwtable
define dso_local ptr @CRYPTO_secure_malloc(i64 noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr null, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 111, ptr %10, align 4, !tbaa !11
  %12 = load i32, ptr @secure_mem_initialized, align 4, !tbaa !11
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %19, label %14

14:                                               ; preds = %3
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = load ptr, ptr %6, align 8, !tbaa !9
  %17 = load i32, ptr %7, align 4, !tbaa !11
  %18 = call ptr @CRYPTO_malloc(i64 noundef %15, ptr noundef %16, i32 noundef %17)
  store ptr %18, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %55

19:                                               ; preds = %3
  %20 = load ptr, ptr @sec_malloc_lock, align 8, !tbaa !9
  %21 = call i32 @CRYPTO_THREAD_write_lock(ptr noundef %20)
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %24, label %23

23:                                               ; preds = %19
  store i32 524303, ptr %10, align 4, !tbaa !11
  br label %40

24:                                               ; preds = %19
  %25 = load i64, ptr %5, align 8, !tbaa !5
  %26 = call ptr @sh_malloc(i64 noundef %25)
  store ptr %26, ptr %8, align 8, !tbaa !9
  %27 = load ptr, ptr %8, align 8, !tbaa !9
  %28 = icmp ne ptr %27, null
  br i1 %28, label %29, label %32

29:                                               ; preds = %24
  %30 = load ptr, ptr %8, align 8, !tbaa !9
  %31 = call i64 @sh_actual_size(ptr noundef %30)
  br label %33

32:                                               ; preds = %24
  br label %33

33:                                               ; preds = %32, %29
  %34 = phi i64 [ %31, %29 ], [ 0, %32 ]
  store i64 %34, ptr %9, align 8, !tbaa !5
  %35 = load i64, ptr %9, align 8, !tbaa !5
  %36 = load i64, ptr @secure_mem_used, align 8, !tbaa !5
  %37 = add i64 %36, %35
  store i64 %37, ptr @secure_mem_used, align 8, !tbaa !5
  %38 = load ptr, ptr @sec_malloc_lock, align 8, !tbaa !9
  %39 = call i32 @CRYPTO_THREAD_unlock(ptr noundef %38)
  br label %40

40:                                               ; preds = %33, %23
  %41 = load ptr, ptr %8, align 8, !tbaa !9
  %42 = icmp eq ptr %41, null
  br i1 %42, label %43, label %53

43:                                               ; preds = %40
  %44 = load ptr, ptr %6, align 8, !tbaa !9
  %45 = icmp ne ptr %44, null
  br i1 %45, label %49, label %46

46:                                               ; preds = %43
  %47 = load i32, ptr %7, align 4, !tbaa !11
  %48 = icmp ne i32 %47, 0
  br i1 %48, label %49, label %53

49:                                               ; preds = %46, %43
  call void @ERR_new()
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  %51 = load i32, ptr %7, align 4, !tbaa !11
  call void @ERR_set_debug(ptr noundef %50, i32 noundef %51, ptr noundef null)
  %52 = load i32, ptr %10, align 4, !tbaa !11
  call void (i32, i32, ptr, ...) @ERR_set_error(i32 noundef 15, i32 noundef %52, ptr noundef null)
  br label %53

53:                                               ; preds = %49, %46, %40
  %54 = load ptr, ptr %8, align 8, !tbaa !9
  store ptr %54, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %55

55:                                               ; preds = %53, %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %56 = load ptr, ptr %4, align 8
  ret ptr %56
}

declare ptr @CRYPTO_malloc(i64 noundef, ptr noundef, i32 noundef) #2

declare i32 @CRYPTO_THREAD_write_lock(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden ptr @sh_malloc(i64 noundef) #1

; Function Attrs: nounwind uwtable
declare hidden i64 @sh_actual_size(ptr noundef) #1

declare i32 @CRYPTO_THREAD_unlock(ptr noundef) #2

declare void @ERR_new() #2

declare void @ERR_set_debug(ptr noundef, i32 noundef, ptr noundef) #2

declare void @ERR_set_error(i32 noundef, i32 noundef, ptr noundef, ...) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = !{!"long", !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, !12, i64 0}
!12 = !{!"int", !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"

@secure_mem_initialized = external hidden local_unnamed_addr global i32, align 4
@sec_malloc_lock = external hidden local_unnamed_addr global ptr, align 8
@secure_mem_used = external hidden local_unnamed_addr global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @CRYPTO_secure_malloc(i64 noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @secure_mem_initialized, align 4, !tbaa !5
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %5, label %7

5:                                                ; preds = %3
  %6 = tail call ptr @CRYPTO_malloc(i64 noundef %0, ptr noundef %1, i32 noundef %2) #2
  br label %23

7:                                                ; preds = %3
  %8 = load ptr, ptr @sec_malloc_lock, align 8, !tbaa !9
  %9 = tail call i32 @CRYPTO_THREAD_write_lock(ptr noundef %8) #2
  %.not18 = icmp eq i32 %9, 0
  br i1 %.not18, label %.thread, label %10

10:                                               ; preds = %7
  %11 = tail call ptr @sh_malloc(i64 noundef %0)
  %.not19 = icmp eq ptr %11, null
  br i1 %.not19, label %.thread.critedge, label %12

12:                                               ; preds = %10
  %13 = tail call i64 @sh_actual_size(ptr noundef nonnull %11)
  %14 = load i64, ptr @secure_mem_used, align 8, !tbaa !11
  %15 = add i64 %14, %13
  store i64 %15, ptr @secure_mem_used, align 8, !tbaa !11
  %16 = load ptr, ptr @sec_malloc_lock, align 8, !tbaa !9
  %17 = tail call i32 @CRYPTO_THREAD_unlock(ptr noundef %16) #2
  br label %23

.thread.critedge:                                 ; preds = %10
  %18 = load ptr, ptr @sec_malloc_lock, align 8, !tbaa !9
  %19 = tail call i32 @CRYPTO_THREAD_unlock(ptr noundef %18) #2
  br label %.thread

.thread:                                          ; preds = %.thread.critedge, %7
  %.023 = phi i32 [ 524303, %7 ], [ 111, %.thread.critedge ]
  %20 = icmp ne ptr %1, null
  %21 = icmp ne i32 %2, 0
  %or.cond = or i1 %20, %21
  br i1 %or.cond, label %22, label %23

22:                                               ; preds = %.thread
  tail call void @ERR_new() #2
  tail call void @ERR_set_debug(ptr noundef %1, i32 noundef %2, ptr noundef null) #2
  tail call void (i32, i32, ptr, ...) @ERR_set_error(i32 noundef 15, i32 noundef %.023, ptr noundef null) #2
  br label %23

23:                                               ; preds = %12, %22, %.thread, %5
  %.015 = phi ptr [ %6, %5 ], [ null, %.thread ], [ null, %22 ], [ %11, %12 ]
  ret ptr %.015
}

declare ptr @CRYPTO_malloc(i64 noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

declare i32 @CRYPTO_THREAD_write_lock(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @sh_malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden i64 @sh_actual_size(ptr noundef) local_unnamed_addr #0

declare i32 @CRYPTO_THREAD_unlock(ptr noundef) local_unnamed_addr #1

declare void @ERR_new() local_unnamed_addr #1

declare void @ERR_set_debug(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare void @ERR_set_error(i32 noundef, i32 noundef, ptr noundef, ...) local_unnamed_addr #1

attributes #0 = { 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 #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 }

!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, !12, i64 0}
!12 = !{!"long", !7, i64 0}


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