; LLVM IR from ARMmbed/mbed-crypto :: library (psa_crypto)

; 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.psa_global_data_t = type { ptr, ptr, %struct.mbedtls_entropy_context, %struct.mbedtls_ctr_drbg_context, i8 }
%struct.mbedtls_entropy_context = type { i32, %struct.mbedtls_sha512_context, i32, [20 x %struct.mbedtls_entropy_source_state] }
%struct.mbedtls_sha512_context = type { [2 x i64], [8 x i64], [128 x i8], i32 }
%struct.mbedtls_entropy_source_state = type { ptr, ptr, i64, i64, i32 }
%struct.mbedtls_ctr_drbg_context = type { [16 x i8], i32, i32, i64, i32, %struct.mbedtls_aes_context, ptr, ptr }
%struct.mbedtls_aes_context = type { i32, ptr, [68 x i32] }
%struct.psa_cipher_operation_s = type { i32, i8, i8, i8, %union.anon.2 }
%union.anon.2 = type { %struct.mbedtls_cipher_context_t }
%struct.mbedtls_cipher_context_t = type { ptr, i32, i32, ptr, ptr, [16 x i8], i64, [16 x i8], i64, ptr, ptr }

@global_data = external hidden global %struct.psa_global_data_t, 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
declare hidden i32 @mbedtls_to_psa_error(i32 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_cipher_generate_iv(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i64 %2, ptr %8, align 8, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 -110, ptr %11, align 4, !tbaa !11
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %13, i32 0, i32 1
  %15 = load i8, ptr %14, align 4
  %16 = lshr i8 %15, 2
  %17 = and i8 %16, 1
  %18 = zext i8 %17 to i32
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %28, label %20

20:                                               ; preds = %4
  %21 = load ptr, ptr %6, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %21, i32 0, i32 1
  %23 = load i8, ptr %22, align 4
  %24 = lshr i8 %23, 1
  %25 = and i8 %24, 1
  %26 = zext i8 %25 to i32
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %29, label %28

28:                                               ; preds = %20, %4
  store i32 -137, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %68

29:                                               ; preds = %20
  %30 = load i64, ptr %8, align 8, !tbaa !9
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %31, i32 0, i32 2
  %33 = load i8, ptr %32, align 1, !tbaa !13
  %34 = zext i8 %33 to i64
  %35 = icmp ult i64 %30, %34
  br i1 %35, label %36, label %37

36:                                               ; preds = %29
  store i32 -138, ptr %10, align 4, !tbaa !11
  br label %60

37:                                               ; preds = %29
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %39, i32 0, i32 2
  %41 = load i8, ptr %40, align 1, !tbaa !13
  %42 = zext i8 %41 to i64
  %43 = call i32 @mbedtls_ctr_drbg_random(ptr noundef getelementptr inbounds (%struct.psa_global_data_t, ptr @global_data, i32 0, i32 3), ptr noundef %38, i64 noundef %42)
  store i32 %43, ptr %11, align 4, !tbaa !11
  %44 = load i32, ptr %11, align 4, !tbaa !11
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %46, label %49

46:                                               ; preds = %37
  %47 = load i32, ptr %11, align 4, !tbaa !11
  %48 = call i32 @mbedtls_to_psa_error(i32 noundef %47)
  store i32 %48, ptr %10, align 4, !tbaa !11
  br label %60

49:                                               ; preds = %37
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.psa_cipher_operation_s, ptr %50, i32 0, i32 2
  %52 = load i8, ptr %51, align 1, !tbaa !13
  %53 = zext i8 %52 to i64
  %54 = load ptr, ptr %9, align 8, !tbaa !5
  store i64 %53, ptr %54, align 8, !tbaa !9
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = load i64, ptr %57, align 8, !tbaa !9
  %59 = call i32 @psa_cipher_set_iv(ptr noundef %55, ptr noundef %56, i64 noundef %58)
  store i32 %59, ptr %10, align 4, !tbaa !11
  br label %60

60:                                               ; preds = %49, %46, %36
  %61 = load i32, ptr %10, align 4, !tbaa !11
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %63, label %66

63:                                               ; preds = %60
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = call i32 @psa_cipher_abort(ptr noundef %64)
  br label %66

66:                                               ; preds = %63, %60
  %67 = load i32, ptr %10, align 4, !tbaa !11
  store i32 %67, ptr %5, align 4
  store i32 1, ptr %12, align 4
  br label %68

68:                                               ; preds = %66, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  %69 = load i32, ptr %5, align 4
  ret i32 %69
}

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

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

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


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

%struct.psa_global_data_t = type { ptr, ptr, %struct.mbedtls_entropy_context, %struct.mbedtls_ctr_drbg_context, i8 }
%struct.mbedtls_entropy_context = type { i32, %struct.mbedtls_sha512_context, i32, [20 x %struct.mbedtls_entropy_source_state] }
%struct.mbedtls_sha512_context = type { [2 x i64], [8 x i64], [128 x i8], i32 }
%struct.mbedtls_entropy_source_state = type { ptr, ptr, i64, i64, i32 }
%struct.mbedtls_ctr_drbg_context = type { [16 x i8], i32, i32, i64, i32, %struct.mbedtls_aes_context, ptr, ptr }
%struct.mbedtls_aes_context = type { i32, ptr, [68 x i32] }

@global_data = external hidden global %struct.psa_global_data_t, align 8

; Function Attrs: nounwind uwtable
declare hidden i32 @mbedtls_to_psa_error(i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @psa_cipher_generate_iv(ptr noundef %0, ptr noundef %1, i64 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 4
  %6 = load i8, ptr %5, align 4
  %7 = and i8 %6, 6
  %or.cond.not = icmp eq i8 %7, 2
  br i1 %or.cond.not, label %8, label %23

8:                                                ; preds = %4
  %9 = getelementptr inbounds i8, ptr %0, i64 5
  %10 = load i8, ptr %9, align 1, !tbaa !5
  %11 = zext i8 %10 to i64
  %12 = icmp ugt i64 %11, %2
  br i1 %12, label %.thread, label %13

13:                                               ; preds = %8
  %14 = tail call i32 @mbedtls_ctr_drbg_random(ptr noundef nonnull getelementptr inbounds (i8, ptr @global_data, i64 1048), ptr noundef %1, i64 noundef %11) #2
  %.not20 = icmp eq i32 %14, 0
  br i1 %.not20, label %17, label %15

15:                                               ; preds = %13
  %16 = tail call i32 @mbedtls_to_psa_error(i32 noundef %14)
  br label %21

17:                                               ; preds = %13
  %18 = load i8, ptr %9, align 1, !tbaa !5
  %19 = zext i8 %18 to i64
  store i64 %19, ptr %3, align 8, !tbaa !10
  %20 = tail call i32 @psa_cipher_set_iv(ptr noundef nonnull %0, ptr noundef %1, i64 noundef %19)
  br label %21

21:                                               ; preds = %17, %15
  %.0 = phi i32 [ %16, %15 ], [ %20, %17 ]
  %.not21 = icmp eq i32 %.0, 0
  br i1 %.not21, label %23, label %.thread

.thread:                                          ; preds = %8, %21
  %.024 = phi i32 [ %.0, %21 ], [ -138, %8 ]
  %22 = tail call i32 @psa_cipher_abort(ptr noundef nonnull %0)
  br label %23

23:                                               ; preds = %21, %.thread, %4
  %.016 = phi i32 [ -137, %4 ], [ %.024, %.thread ], [ 0, %21 ]
  ret i32 %.016
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_cipher_set_iv(ptr noundef, ptr noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @psa_cipher_abort(ptr noundef) local_unnamed_addr #0

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, !8, i64 5}
!6 = !{!"psa_cipher_operation_s", !7, i64 0, !7, i64 4, !7, i64 4, !7, i64 4, !8, i64 5, !8, i64 6, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !8, i64 0}


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