; LLVM IR from shanet/Crypto-Example :: Crypto.ll (ZN6Crypto10rsaEncryptEPKhmPPhS3_PmS3_S4_)

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

%class.Crypto = type { ptr, ptr, ptr, ptr, ptr, ptr, ptr, i64, i64 }

declare ptr @EVP_aes_256_cbc() #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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6Crypto10rsaEncryptEPKhmPPhS3_PmS3_S4_(ptr noundef nonnull align 8 dereferenceable(72) %0, ptr noundef %1, i64 noundef %2, ptr noundef %3, ptr noundef %4, ptr noundef %5, ptr noundef %6, ptr noundef %7) #3 align 2 {
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  %17 = alloca ptr, align 8
  %18 = alloca i64, align 8
  %19 = alloca i64, align 8
  %20 = alloca i32, align 4
  store ptr %0, ptr %10, align 8, !tbaa !5
  store ptr %1, ptr %11, align 8, !tbaa !5
  store i64 %2, ptr %12, align 8, !tbaa !9
  store ptr %3, ptr %13, align 8, !tbaa !5
  store ptr %4, ptr %14, align 8, !tbaa !5
  store ptr %5, ptr %15, align 8, !tbaa !5
  store ptr %6, ptr %16, align 8, !tbaa !5
  store ptr %7, ptr %17, align 8, !tbaa !5
  %21 = load ptr, ptr %10, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %18) #4
  store i64 0, ptr %18, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #4
  store i64 0, ptr %19, align 8, !tbaa !9
  %22 = getelementptr inbounds %class.Crypto, ptr %21, i32 0, i32 0
  %23 = load ptr, ptr %22, align 8, !tbaa !11
  %24 = call i32 @EVP_PKEY_get_size(ptr noundef %23)
  %25 = sext i32 %24 to i64
  %26 = call noalias ptr @malloc(i64 noundef %25) #5
  %27 = load ptr, ptr %14, align 8, !tbaa !5
  store ptr %26, ptr %27, align 8, !tbaa !5
  %28 = call noalias ptr @malloc(i64 noundef 16) #5
  %29 = load ptr, ptr %16, align 8, !tbaa !5
  store ptr %28, ptr %29, align 8, !tbaa !5
  %30 = load ptr, ptr %17, align 8, !tbaa !5
  store i64 16, ptr %30, align 8, !tbaa !9
  %31 = load ptr, ptr %14, align 8, !tbaa !5
  %32 = load ptr, ptr %31, align 8, !tbaa !5
  %33 = icmp eq ptr %32, null
  br i1 %33, label %38, label %34

34:                                               ; preds = %8
  %35 = load ptr, ptr %16, align 8, !tbaa !5
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = icmp eq ptr %36, null
  br i1 %37, label %38, label %39

38:                                               ; preds = %34, %8
  store i32 -1, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %91

39:                                               ; preds = %34
  %40 = load i64, ptr %12, align 8, !tbaa !9
  %41 = add i64 %40, 16
  %42 = call noalias ptr @malloc(i64 noundef %41) #5
  %43 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %42, ptr %43, align 8, !tbaa !5
  %44 = load ptr, ptr %13, align 8, !tbaa !5
  %45 = icmp eq ptr %44, null
  br i1 %45, label %46, label %47

46:                                               ; preds = %39
  store i32 -1, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %91

47:                                               ; preds = %39
  %48 = getelementptr inbounds %class.Crypto, ptr %21, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  %50 = call ptr @EVP_aes_256_cbc()
  %51 = load ptr, ptr %14, align 8, !tbaa !5
  %52 = load ptr, ptr %15, align 8, !tbaa !5
  %53 = load ptr, ptr %16, align 8, !tbaa !5
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  %55 = getelementptr inbounds %class.Crypto, ptr %21, i32 0, i32 0
  %56 = call i32 @EVP_SealInit(ptr noundef %49, ptr noundef %50, ptr noundef %51, ptr noundef %52, ptr noundef %54, ptr noundef %55, i32 noundef 1)
  %57 = icmp ne i32 %56, 0
  br i1 %57, label %59, label %58

58:                                               ; preds = %47
  store i32 -1, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %91

59:                                               ; preds = %47
  %60 = getelementptr inbounds %class.Crypto, ptr %21, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  %64 = load i64, ptr %18, align 8, !tbaa !9
  %65 = getelementptr inbounds i8, ptr %63, i64 %64
  %66 = load ptr, ptr %11, align 8, !tbaa !5
  %67 = load i64, ptr %12, align 8, !tbaa !9
  %68 = trunc i64 %67 to i32
  %69 = call i32 @EVP_EncryptUpdate(ptr noundef %61, ptr noundef %65, ptr noundef %19, ptr noundef %66, i32 noundef %68)
  %70 = icmp ne i32 %69, 0
  br i1 %70, label %72, label %71

71:                                               ; preds = %59
  store i32 -1, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %91

72:                                               ; preds = %59
  %73 = load i64, ptr %19, align 8, !tbaa !9
  %74 = load i64, ptr %18, align 8, !tbaa !9
  %75 = add i64 %74, %73
  store i64 %75, ptr %18, align 8, !tbaa !9
  %76 = getelementptr inbounds %class.Crypto, ptr %21, i32 0, i32 1
  %77 = load ptr, ptr %76, align 8, !tbaa !13
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  %79 = load ptr, ptr %78, align 8, !tbaa !5
  %80 = load i64, ptr %18, align 8, !tbaa !9
  %81 = getelementptr inbounds i8, ptr %79, i64 %80
  %82 = call i32 @EVP_SealFinal(ptr noundef %77, ptr noundef %81, ptr noundef %19)
  %83 = icmp ne i32 %82, 0
  br i1 %83, label %85, label %84

84:                                               ; preds = %72
  store i32 -1, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %91

85:                                               ; preds = %72
  %86 = load i64, ptr %19, align 8, !tbaa !9
  %87 = load i64, ptr %18, align 8, !tbaa !9
  %88 = add i64 %87, %86
  store i64 %88, ptr %18, align 8, !tbaa !9
  %89 = load i64, ptr %18, align 8, !tbaa !9
  %90 = trunc i64 %89 to i32
  store i32 %90, ptr %9, align 4
  store i32 1, ptr %20, align 4
  br label %91

91:                                               ; preds = %85, %84, %71, %58, %46, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %18) #4
  %92 = load i32, ptr %9, align 4
  ret i32 %92
}

declare i32 @EVP_PKEY_get_size(ptr noundef) #0

declare i32 @EVP_SealInit(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef) #0

declare i32 @EVP_EncryptUpdate(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef) #0

declare i32 @EVP_SealFinal(ptr 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 = { nounwind allocsize(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 = { mustprogress 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 }
attributes #5 = { nounwind allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS6Crypto", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !10, i64 56, !10, i64 64}
!13 = !{!12, !6, 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"

declare ptr @EVP_aes_256_cbc() 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

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN6Crypto10rsaEncryptEPKhmPPhS3_PmS3_S4_(ptr noundef nonnull align 8 dereferenceable(72) %0, ptr noundef %1, i64 noundef %2, ptr nocapture noundef %3, ptr noundef %4, ptr noundef %5, ptr nocapture noundef %6, ptr nocapture noundef writeonly %7) local_unnamed_addr #3 align 2 {
  %9 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %9) #4
  store i64 0, ptr %9, align 8, !tbaa !5
  %10 = load ptr, ptr %0, align 8, !tbaa !9
  %11 = tail call i32 @EVP_PKEY_get_size(ptr noundef %10)
  %12 = sext i32 %11 to i64
  %13 = tail call noalias ptr @malloc(i64 noundef %12) #5
  store ptr %13, ptr %4, align 8, !tbaa !12
  %14 = tail call noalias dereferenceable_or_null(16) ptr @malloc(i64 noundef 16) #5
  store ptr %14, ptr %6, align 8, !tbaa !12
  store i64 16, ptr %7, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !12
  %16 = icmp eq ptr %15, null
  %17 = icmp eq ptr %14, null
  %or.cond = or i1 %17, %16
  br i1 %or.cond, label %41, label %18

18:                                               ; preds = %8
  %19 = add i64 %2, 16
  %20 = tail call noalias ptr @malloc(i64 noundef %19) #5
  store ptr %20, ptr %3, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %0, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  %23 = tail call ptr @EVP_aes_256_cbc()
  %24 = load ptr, ptr %6, align 8, !tbaa !12
  %25 = tail call i32 @EVP_SealInit(ptr noundef %22, ptr noundef %23, ptr noundef nonnull %4, ptr noundef %5, ptr noundef %24, ptr noundef nonnull %0, i32 noundef 1)
  %.not = icmp eq i32 %25, 0
  br i1 %.not, label %41, label %26

26:                                               ; preds = %18
  %27 = load ptr, ptr %21, align 8, !tbaa !13
  %28 = load ptr, ptr %3, align 8, !tbaa !12
  %29 = trunc i64 %2 to i32
  %30 = call i32 @EVP_EncryptUpdate(ptr noundef %27, ptr noundef %28, ptr noundef nonnull %9, ptr noundef %1, i32 noundef %29)
  %.not21 = icmp eq i32 %30, 0
  br i1 %.not21, label %41, label %31

31:                                               ; preds = %26
  %32 = load i64, ptr %9, align 8, !tbaa !5
  %33 = load ptr, ptr %21, align 8, !tbaa !13
  %34 = load ptr, ptr %3, align 8, !tbaa !12
  %35 = getelementptr inbounds i8, ptr %34, i64 %32
  %36 = call i32 @EVP_SealFinal(ptr noundef %33, ptr noundef %35, ptr noundef nonnull %9)
  %.not22 = icmp eq i32 %36, 0
  br i1 %.not22, label %41, label %37

37:                                               ; preds = %31
  %38 = load i64, ptr %9, align 8, !tbaa !5
  %39 = add i64 %38, %32
  %40 = trunc i64 %39 to i32
  br label %41

41:                                               ; preds = %31, %26, %18, %8, %37
  %.0 = phi i32 [ %40, %37 ], [ -1, %8 ], [ -1, %18 ], [ -1, %26 ], [ -1, %31 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %9) #4
  ret i32 %.0
}

declare i32 @EVP_PKEY_get_size(ptr noundef) local_unnamed_addr #0

declare i32 @EVP_SealInit(ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @EVP_EncryptUpdate(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

declare i32 @EVP_SealFinal(ptr 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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress 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 }
attributes #5 = { nounwind allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTS6Crypto", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !6, i64 56, !6, i64 64}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 8}


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