; LLVM IR from square/subzero :: core/external-crypto/aes (aes_avs)

; 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.aes_crypt_ctx = type { [60 x i32], %union.aes_inf, [12 x i8] }
%union.aes_inf = type { i32 }

; 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

declare i32 @aes_encrypt_key(ptr noundef, i32 noundef, ptr noundef) #1

declare i32 @aes_ofb_crypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

declare i32 @aes_decrypt_key(ptr noundef, i32 noundef, ptr noundef) #1

declare i32 @aes_ecb_decrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

declare i32 @aes_cbc_decrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

declare i32 @aes_cfb_decrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) #1

; 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

; Function Attrs: nounwind uwtable
define dso_local void @do_mct_decrypt(i32 noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i32 noundef %5) #3 {
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca [1 x %struct.aes_crypt_ctx], align 16
  %14 = alloca [16 x i8], align 16
  %15 = alloca i32, align 4
  store i32 %0, ptr %7, align 4, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !9
  store ptr %2, ptr %9, align 8, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !9
  store ptr %4, ptr %11, align 8, !tbaa !9
  store i32 %5, ptr %12, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 256, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %43

18:                                               ; preds = %6
  %19 = load ptr, ptr %8, align 8, !tbaa !9
  %20 = load i32, ptr %12, align 4, !tbaa !5
  %21 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %22 = call i32 @aes_decrypt_key(ptr noundef %19, i32 noundef %20, ptr noundef %21)
  %23 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %24 = load ptr, ptr %10, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %23, ptr align 1 %24, i64 16, i1 false)
  store i32 0, ptr %15, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %37, %18
  %26 = load i32, ptr %15, align 4, !tbaa !5
  %27 = icmp slt i32 %26, 500
  br i1 %27, label %28, label %40

28:                                               ; preds = %25
  %29 = load ptr, ptr %10, align 8, !tbaa !9
  %30 = load ptr, ptr %11, align 8, !tbaa !9
  %31 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %32 = call i32 @aes_ecb_decrypt(ptr noundef %29, ptr noundef %30, i32 noundef 16, ptr noundef %31)
  %33 = load ptr, ptr %11, align 8, !tbaa !9
  %34 = load ptr, ptr %10, align 8, !tbaa !9
  %35 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %36 = call i32 @aes_ecb_decrypt(ptr noundef %33, ptr noundef %34, i32 noundef 16, ptr noundef %35)
  br label %37

37:                                               ; preds = %28
  %38 = load i32, ptr %15, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %15, align 4, !tbaa !5
  br label %25, !llvm.loop !11

40:                                               ; preds = %25
  %41 = load ptr, ptr %11, align 8, !tbaa !9
  %42 = load ptr, ptr %10, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %41, ptr align 1 %42, i64 16, i1 false)
  br label %91

43:                                               ; preds = %6
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = icmp eq i32 %44, 1
  br i1 %45, label %46, label %51

46:                                               ; preds = %43
  %47 = load ptr, ptr %8, align 8, !tbaa !9
  %48 = load i32, ptr %12, align 4, !tbaa !5
  %49 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %50 = call i32 @aes_decrypt_key(ptr noundef %47, i32 noundef %48, ptr noundef %49)
  br label %56

51:                                               ; preds = %43
  %52 = load ptr, ptr %8, align 8, !tbaa !9
  %53 = load i32, ptr %12, align 4, !tbaa !5
  %54 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %55 = call i32 @aes_encrypt_key(ptr noundef %52, i32 noundef %53, ptr noundef %54)
  br label %56

56:                                               ; preds = %51, %46
  %57 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %58 = load ptr, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %57, ptr align 1 %58, i64 16, i1 false)
  store i32 0, ptr %15, align 4, !tbaa !5
  br label %59

59:                                               ; preds = %87, %56
  %60 = load i32, ptr %15, align 4, !tbaa !5
  %61 = icmp slt i32 %60, 1000
  br i1 %61, label %62, label %90

62:                                               ; preds = %59
  %63 = load i32, ptr %7, align 4, !tbaa !5
  switch i32 %63, label %82 [
    i32 1, label %64
    i32 2, label %70
    i32 3, label %76
  ]

64:                                               ; preds = %62
  %65 = load ptr, ptr %10, align 8, !tbaa !9
  %66 = load ptr, ptr %11, align 8, !tbaa !9
  %67 = load ptr, ptr %9, align 8, !tbaa !9
  %68 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %69 = call i32 @aes_cbc_decrypt(ptr noundef %65, ptr noundef %66, i32 noundef 16, ptr noundef %67, ptr noundef %68)
  br label %82

70:                                               ; preds = %62
  %71 = load ptr, ptr %10, align 8, !tbaa !9
  %72 = load ptr, ptr %11, align 8, !tbaa !9
  %73 = load ptr, ptr %9, align 8, !tbaa !9
  %74 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %75 = call i32 @aes_cfb_decrypt(ptr noundef %71, ptr noundef %72, i32 noundef 16, ptr noundef %73, ptr noundef %74)
  br label %82

76:                                               ; preds = %62
  %77 = load ptr, ptr %10, align 8, !tbaa !9
  %78 = load ptr, ptr %11, align 8, !tbaa !9
  %79 = load ptr, ptr %9, align 8, !tbaa !9
  %80 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %81 = call i32 @aes_ofb_crypt(ptr noundef %77, ptr noundef %78, i32 noundef 16, ptr noundef %79, ptr noundef %80)
  br label %82

82:                                               ; preds = %76, %70, %64, %62
  %83 = load ptr, ptr %10, align 8, !tbaa !9
  %84 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %83, ptr align 16 %84, i64 16, i1 false)
  %85 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %86 = load ptr, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %85, ptr align 1 %86, i64 16, i1 false)
  br label %87

87:                                               ; preds = %82
  %88 = load i32, ptr %15, align 4, !tbaa !5
  %89 = add nsw i32 %88, 1
  store i32 %89, ptr %15, align 4, !tbaa !5
  br label %59, !llvm.loop !13

90:                                               ; preds = %59
  br label %91

91:                                               ; preds = %90, %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 256, ptr %13) #4
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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.aes_crypt_ctx = type { [60 x i32], %union.aes_inf, [12 x i8] }
%union.aes_inf = type { i32 }

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

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

declare i32 @aes_encrypt_key(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i32 @aes_ofb_crypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare i32 @aes_decrypt_key(ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i32 @aes_ecb_decrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare i32 @aes_cbc_decrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

declare i32 @aes_cfb_decrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; 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

; Function Attrs: nounwind uwtable
define dso_local void @do_mct_decrypt(i32 noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i32 noundef %5) local_unnamed_addr #3 {
  %7 = alloca [1 x %struct.aes_crypt_ctx], align 16
  %8 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 256, ptr nonnull %7) #4
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %8)
  switch i32 %0, label %17 [
    i32 0, label %9
    i32 1, label %.thread
  ]

9:                                                ; preds = %6
  %10 = call i32 @aes_decrypt_key(ptr noundef %1, i32 noundef %5, ptr noundef nonnull %7) #4
  br label %11

11:                                               ; preds = %9, %11
  %.032 = phi i32 [ 0, %9 ], [ %14, %11 ]
  %12 = call i32 @aes_ecb_decrypt(ptr noundef %3, ptr noundef %4, i32 noundef 16, ptr noundef nonnull %7) #4
  %13 = call i32 @aes_ecb_decrypt(ptr noundef %4, ptr noundef %3, i32 noundef 16, ptr noundef nonnull %7) #4
  %14 = add nuw nsw i32 %.032, 1
  %exitcond.not = icmp eq i32 %14, 500
  br i1 %exitcond.not, label %15, label %11, !llvm.loop !5

15:                                               ; preds = %11
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %4, ptr noundef nonnull align 1 dereferenceable(16) %3, i64 16, i1 false)
  br label %.loopexit

.thread:                                          ; preds = %6
  %16 = call i32 @aes_decrypt_key(ptr noundef %1, i32 noundef %5, ptr noundef nonnull %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, ptr noundef nonnull align 1 dereferenceable(16) %2, i64 16, i1 false)
  br label %.split.us

17:                                               ; preds = %6
  %18 = call i32 @aes_encrypt_key(ptr noundef %1, i32 noundef %5, ptr noundef nonnull %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, ptr noundef nonnull align 1 dereferenceable(16) %2, i64 16, i1 false)
  switch i32 %0, label %.split [
    i32 3, label %.split.us36
    i32 2, label %.split.us34
  ]

.split.us:                                        ; preds = %.thread, %.split.us
  %.133.us = phi i32 [ %20, %.split.us ], [ 0, %.thread ]
  %19 = call i32 @aes_cbc_decrypt(ptr noundef %3, ptr noundef %4, i32 noundef 16, ptr noundef %2, ptr noundef nonnull %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %3, ptr noundef nonnull align 16 dereferenceable(16) %8, i64 16, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, ptr noundef nonnull align 1 dereferenceable(16) %4, i64 16, i1 false)
  %20 = add nuw nsw i32 %.133.us, 1
  %exitcond43.not = icmp eq i32 %20, 1000
  br i1 %exitcond43.not, label %.loopexit, label %.split.us, !llvm.loop !7

.split.us34:                                      ; preds = %17, %.split.us34
  %.133.us35 = phi i32 [ %22, %.split.us34 ], [ 0, %17 ]
  %21 = call i32 @aes_cfb_decrypt(ptr noundef %3, ptr noundef %4, i32 noundef 16, ptr noundef %2, ptr noundef nonnull %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %3, ptr noundef nonnull align 16 dereferenceable(16) %8, i64 16, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, ptr noundef nonnull align 1 dereferenceable(16) %4, i64 16, i1 false)
  %22 = add nuw nsw i32 %.133.us35, 1
  %exitcond42.not = icmp eq i32 %22, 1000
  br i1 %exitcond42.not, label %.loopexit, label %.split.us34, !llvm.loop !7

.split.us36:                                      ; preds = %17, %.split.us36
  %.133.us37 = phi i32 [ %24, %.split.us36 ], [ 0, %17 ]
  %23 = call i32 @aes_ofb_crypt(ptr noundef %3, ptr noundef %4, i32 noundef 16, ptr noundef %2, ptr noundef nonnull %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %3, ptr noundef nonnull align 16 dereferenceable(16) %8, i64 16, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, ptr noundef nonnull align 1 dereferenceable(16) %4, i64 16, i1 false)
  %24 = add nuw nsw i32 %.133.us37, 1
  %exitcond41.not = icmp eq i32 %24, 1000
  br i1 %exitcond41.not, label %.loopexit, label %.split.us36, !llvm.loop !7

.split:                                           ; preds = %17, %.split
  %.133 = phi i32 [ %25, %.split ], [ 0, %17 ]
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %3, ptr noundef nonnull align 16 dereferenceable(16) %8, i64 16, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, ptr noundef nonnull align 1 dereferenceable(16) %4, i64 16, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(16) %3, ptr noundef nonnull align 16 dereferenceable(16) %8, i64 16, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 16 dereferenceable(16) %8, ptr noundef nonnull align 1 dereferenceable(16) %4, i64 16, i1 false)
  %25 = add nuw nsw i32 %.133, 2
  %exitcond44.not.1 = icmp eq i32 %25, 1000
  br i1 %exitcond44.not.1, label %.loopexit, label %.split, !llvm.loop !7

.loopexit:                                        ; preds = %.split.us, %.split.us34, %.split.us36, %.split, %15
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %8)
  call void @llvm.lifetime.end.p0(i64 256, ptr nonnull %7) #4
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = distinct !{!7, !6}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass