; 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_ecb_encrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #1

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

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

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

; Function Attrs: nounwind uwtable
define dso_local void @do_mct_encrypt(i32 noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i32 noundef %5) #2 {
  %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 ptr, ptr %8, align 8, !tbaa !9
  %17 = load i32, ptr %12, align 4, !tbaa !5
  %18 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %19 = call i32 @aes_encrypt_key(ptr noundef %16, i32 noundef %17, ptr noundef %18)
  %20 = load i32, ptr %7, align 4, !tbaa !5
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %41

22:                                               ; preds = %6
  store i32 0, ptr %15, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %35, %22
  %24 = load i32, ptr %15, align 4, !tbaa !5
  %25 = icmp slt i32 %24, 500
  br i1 %25, label %26, label %38

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

35:                                               ; preds = %26
  %36 = load i32, ptr %15, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %15, align 4, !tbaa !5
  br label %23, !llvm.loop !11

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

41:                                               ; preds = %6
  %42 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %43 = load ptr, ptr %9, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %42, ptr align 1 %43, i64 16, i1 false)
  store i32 0, ptr %15, align 4, !tbaa !5
  br label %44

44:                                               ; preds = %72, %41
  %45 = load i32, ptr %15, align 4, !tbaa !5
  %46 = icmp slt i32 %45, 1000
  br i1 %46, label %47, label %75

47:                                               ; preds = %44
  %48 = load i32, ptr %7, align 4, !tbaa !5
  switch i32 %48, label %67 [
    i32 1, label %49
    i32 2, label %55
    i32 3, label %61
  ]

49:                                               ; preds = %47
  %50 = load ptr, ptr %10, align 8, !tbaa !9
  %51 = load ptr, ptr %11, align 8, !tbaa !9
  %52 = load ptr, ptr %9, align 8, !tbaa !9
  %53 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %54 = call i32 @aes_cbc_encrypt(ptr noundef %50, ptr noundef %51, i32 noundef 16, ptr noundef %52, ptr noundef %53)
  br label %67

55:                                               ; preds = %47
  %56 = load ptr, ptr %10, align 8, !tbaa !9
  %57 = load ptr, ptr %11, align 8, !tbaa !9
  %58 = load ptr, ptr %9, align 8, !tbaa !9
  %59 = getelementptr inbounds [1 x %struct.aes_crypt_ctx], ptr %13, i64 0, i64 0
  %60 = call i32 @aes_cfb_encrypt(ptr noundef %56, ptr noundef %57, i32 noundef 16, ptr noundef %58, ptr noundef %59)
  br label %67

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

67:                                               ; preds = %61, %55, %49, %47
  %68 = load ptr, ptr %10, align 8, !tbaa !9
  %69 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %68, ptr align 16 %69, i64 16, i1 false)
  %70 = getelementptr inbounds [16 x i8], ptr %14, i64 0, i64 0
  %71 = load ptr, ptr %11, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %70, ptr align 1 %71, i64 16, i1 false)
  br label %72

72:                                               ; preds = %67
  %73 = load i32, ptr %15, align 4, !tbaa !5
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %15, align 4, !tbaa !5
  br label %44, !llvm.loop !13

75:                                               ; preds = %44
  br label %76

76:                                               ; preds = %75, %38
  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
}

; 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) #3

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 = { 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: readwrite) }
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_ecb_encrypt(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

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

declare i32 @aes_cfb_encrypt(ptr noundef, ptr noundef, i32 noundef, ptr 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

; Function Attrs: nounwind uwtable
define dso_local void @do_mct_encrypt(i32 noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4, i32 noundef %5) local_unnamed_addr #2 {
  %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)
  %9 = call i32 @aes_encrypt_key(ptr noundef %1, i32 noundef %5, ptr noundef nonnull %7) #4
  %10 = icmp eq i32 %0, 0
  br i1 %10, label %.preheader, label %15

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

14:                                               ; preds = %.preheader
  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

15:                                               ; preds = %6
  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 1, label %.split.us
    i32 2, label %.split.us27
    i32 3, label %.split.us29
  ]

.split.us:                                        ; preds = %15, %.split.us
  %.126.us = phi i32 [ %17, %.split.us ], [ 0, %15 ]
  %16 = call i32 @aes_cbc_encrypt(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)
  %17 = add nuw nsw i32 %.126.us, 1
  %exitcond36.not = icmp eq i32 %17, 1000
  br i1 %exitcond36.not, label %.loopexit, label %.split.us, !llvm.loop !7

.split.us27:                                      ; preds = %15, %.split.us27
  %.126.us28 = phi i32 [ %19, %.split.us27 ], [ 0, %15 ]
  %18 = call i32 @aes_cfb_encrypt(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)
  %19 = add nuw nsw i32 %.126.us28, 1
  %exitcond35.not = icmp eq i32 %19, 1000
  br i1 %exitcond35.not, label %.loopexit, label %.split.us27, !llvm.loop !7

.split.us29:                                      ; preds = %15, %.split.us29
  %.126.us30 = phi i32 [ %21, %.split.us29 ], [ 0, %15 ]
  %20 = 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)
  %21 = add nuw nsw i32 %.126.us30, 1
  %exitcond.not = icmp eq i32 %21, 1000
  br i1 %exitcond.not, label %.loopexit, label %.split.us29, !llvm.loop !7

.split:                                           ; preds = %15, %.split
  %.126 = phi i32 [ %22, %.split ], [ 0, %15 ]
  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)
  %22 = add nuw nsw i32 %.126, 2
  %exitcond37.not.1 = icmp eq i32 %22, 1000
  br i1 %exitcond37.not.1, label %.loopexit, label %.split, !llvm.loop !7

.loopexit:                                        ; preds = %.split.us29, %.split.us27, %.split.us, %.split, %14
  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
}

; 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) #3

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 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass