; LLVM IR from guanzhi/GmSSL :: src (aes)

; 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_KEY = type { [60 x i32], i64 }

; Function Attrs: nounwind uwtable
declare dso_local i32 @aes_set_encrypt_key(ptr noundef, ptr noundef, i64 noundef) #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 uwtable
define dso_local i32 @aes_set_decrypt_key(ptr noundef %0, ptr noundef %1, i64 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca %struct.AES_KEY, align 8
  %9 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i64 %2, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 248, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = call i32 @aes_set_encrypt_key(ptr noundef %8, ptr noundef %10, i64 noundef %11)
  %13 = icmp ne i32 %12, 0
  br i1 %13, label %15, label %14

14:                                               ; preds = %3
  br label %88

15:                                               ; preds = %3
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %80, %15
  %17 = load i64, ptr %9, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = icmp ule i64 %17, %19
  br i1 %20, label %21, label %83

21:                                               ; preds = %16
  %22 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 0
  %23 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 1
  %24 = load i64, ptr %23, align 8, !tbaa !13
  %25 = load i64, ptr %9, align 8, !tbaa !9
  %26 = sub i64 %24, %25
  %27 = mul i64 4, %26
  %28 = getelementptr inbounds [60 x i32], ptr %22, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !11
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.AES_KEY, ptr %30, i32 0, i32 0
  %32 = load i64, ptr %9, align 8, !tbaa !9
  %33 = mul i64 4, %32
  %34 = getelementptr inbounds [60 x i32], ptr %31, i64 0, i64 %33
  store i32 %29, ptr %34, align 4, !tbaa !11
  %35 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 0
  %36 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 1
  %37 = load i64, ptr %36, align 8, !tbaa !13
  %38 = load i64, ptr %9, align 8, !tbaa !9
  %39 = sub i64 %37, %38
  %40 = mul i64 4, %39
  %41 = add i64 %40, 1
  %42 = getelementptr inbounds [60 x i32], ptr %35, i64 0, i64 %41
  %43 = load i32, ptr %42, align 4, !tbaa !11
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.AES_KEY, ptr %44, i32 0, i32 0
  %46 = load i64, ptr %9, align 8, !tbaa !9
  %47 = mul i64 4, %46
  %48 = add i64 %47, 1
  %49 = getelementptr inbounds [60 x i32], ptr %45, i64 0, i64 %48
  store i32 %43, ptr %49, align 4, !tbaa !11
  %50 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 0
  %51 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !13
  %53 = load i64, ptr %9, align 8, !tbaa !9
  %54 = sub i64 %52, %53
  %55 = mul i64 4, %54
  %56 = add i64 %55, 2
  %57 = getelementptr inbounds [60 x i32], ptr %50, i64 0, i64 %56
  %58 = load i32, ptr %57, align 4, !tbaa !11
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.AES_KEY, ptr %59, i32 0, i32 0
  %61 = load i64, ptr %9, align 8, !tbaa !9
  %62 = mul i64 4, %61
  %63 = add i64 %62, 2
  %64 = getelementptr inbounds [60 x i32], ptr %60, i64 0, i64 %63
  store i32 %58, ptr %64, align 4, !tbaa !11
  %65 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 0
  %66 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 1
  %67 = load i64, ptr %66, align 8, !tbaa !13
  %68 = load i64, ptr %9, align 8, !tbaa !9
  %69 = sub i64 %67, %68
  %70 = mul i64 4, %69
  %71 = add i64 %70, 3
  %72 = getelementptr inbounds [60 x i32], ptr %65, i64 0, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !11
  %74 = load ptr, ptr %4, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.AES_KEY, ptr %74, i32 0, i32 0
  %76 = load i64, ptr %9, align 8, !tbaa !9
  %77 = mul i64 4, %76
  %78 = add i64 %77, 3
  %79 = getelementptr inbounds [60 x i32], ptr %75, i64 0, i64 %78
  store i32 %73, ptr %79, align 4, !tbaa !11
  br label %80

80:                                               ; preds = %21
  %81 = load i64, ptr %9, align 8, !tbaa !9
  %82 = add i64 %81, 1
  store i64 %82, ptr %9, align 8, !tbaa !9
  br label %16, !llvm.loop !15

83:                                               ; preds = %16
  %84 = getelementptr inbounds %struct.AES_KEY, ptr %8, i32 0, i32 1
  %85 = load i64, ptr %84, align 8, !tbaa !13
  %86 = load ptr, ptr %4, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.AES_KEY, ptr %86, i32 0, i32 1
  store i64 %85, ptr %87, align 8, !tbaa !13
  store i32 1, ptr %7, align 4, !tbaa !11
  br label %88

88:                                               ; preds = %83, %14
  call void @llvm.memset.p0.i64(ptr align 8 %8, i8 0, i64 248, i1 false)
  %89 = load i32, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 248, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret i32 %89
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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, !10, i64 240}
!14 = !{!"", !7, i64 0, !10, i64 240}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; 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_KEY = type { [60 x i32], i64 }

; Function Attrs: nounwind uwtable
declare dso_local i32 @aes_set_encrypt_key(ptr noundef, ptr noundef, i64 noundef) 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: nounwind uwtable
define dso_local range(i32 0, 2) i32 @aes_set_decrypt_key(ptr nocapture noundef writeonly %0, ptr noundef %1, i64 noundef %2) local_unnamed_addr #0 {
  %4 = alloca %struct.AES_KEY, align 8
  call void @llvm.lifetime.start.p0(i64 248, ptr nonnull %4) #3
  %5 = call i32 @aes_set_encrypt_key(ptr noundef nonnull %4, ptr noundef %1, i64 noundef %2)
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %33, label %.preheader

.preheader:                                       ; preds = %3
  %6 = getelementptr inbounds i8, ptr %4, i64 240
  %7 = load i64, ptr %6, align 8, !tbaa !5
  %8 = add i64 %7, 1
  %umax = call i64 @llvm.umax.i64(i64 %8, i64 1)
  %xtraiter = and i64 %umax, 1
  %9 = icmp ult i64 %8, 2
  br i1 %9, label %.unr-lcssa, label %.preheader.new

.preheader.new:                                   ; preds = %.preheader
  %unroll_iter = and i64 %umax, -2
  br label %10

10:                                               ; preds = %10, %.preheader.new
  %.020 = phi i64 [ 0, %.preheader.new ], [ %24, %10 ]
  %niter = phi i64 [ 0, %.preheader.new ], [ %niter.next.1, %10 ]
  %11 = sub i64 %7, %.020
  %12 = shl i64 %11, 2
  %13 = getelementptr inbounds [60 x i32], ptr %4, i64 0, i64 %12
  %14 = shl i64 %.020, 2
  %15 = getelementptr inbounds [60 x i32], ptr %0, i64 0, i64 %14
  %16 = load <4 x i32>, ptr %13, align 8, !tbaa !10
  store <4 x i32> %16, ptr %15, align 4, !tbaa !10
  %17 = or disjoint i64 %.020, 1
  %18 = sub i64 %7, %17
  %19 = shl i64 %18, 2
  %20 = getelementptr inbounds [60 x i32], ptr %4, i64 0, i64 %19
  %21 = shl i64 %17, 2
  %22 = getelementptr inbounds [60 x i32], ptr %0, i64 0, i64 %21
  %23 = load <4 x i32>, ptr %20, align 8, !tbaa !10
  store <4 x i32> %23, ptr %22, align 4, !tbaa !10
  %24 = add nuw i64 %.020, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.unr-lcssa, label %10, !llvm.loop !12

.unr-lcssa:                                       ; preds = %10, %.preheader
  %.020.unr = phi i64 [ 0, %.preheader ], [ %24, %10 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %31, label %.epilog-lcssa

.epilog-lcssa:                                    ; preds = %.unr-lcssa
  %25 = sub i64 %7, %.020.unr
  %26 = shl i64 %25, 2
  %27 = getelementptr inbounds [60 x i32], ptr %4, i64 0, i64 %26
  %28 = shl i64 %.020.unr, 2
  %29 = getelementptr inbounds [60 x i32], ptr %0, i64 0, i64 %28
  %30 = load <4 x i32>, ptr %27, align 8, !tbaa !10
  store <4 x i32> %30, ptr %29, align 4, !tbaa !10
  br label %31

31:                                               ; preds = %.unr-lcssa, %.epilog-lcssa
  %32 = getelementptr inbounds i8, ptr %0, i64 240
  store i64 %7, ptr %32, align 8, !tbaa !5
  br label %33

33:                                               ; preds = %3, %31
  %.017 = phi i32 [ 1, %31 ], [ 0, %3 ]
  call void @llvm.lifetime.end.p0(i64 248, ptr nonnull %4) #3
  ret i32 %.017
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umax.i64(i64, i64) #2

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !9, i64 240}
!6 = !{!"", !7, i64 0, !9, i64 240}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"long", !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: DSEPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass