; LLVM IR from dhuertas/AES :: aes (Rcon)

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

@gmult_aes = external global [0 x i8], align 1
@R = external dso_local global [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @Rcon(i8 noundef zeroext %0) #0 {
  %2 = alloca i8, align 1
  store i8 %0, ptr %2, align 1, !tbaa !5
  %3 = load i8, ptr %2, align 1, !tbaa !5
  %4 = zext i8 %3 to i32
  %5 = icmp eq i32 %4, 1
  br i1 %5, label %6, label %7

6:                                                ; preds = %1
  store i8 1, ptr @R, align 1, !tbaa !5
  br label %30

7:                                                ; preds = %1
  %8 = load i8, ptr %2, align 1, !tbaa !5
  %9 = zext i8 %8 to i32
  %10 = icmp sgt i32 %9, 1
  br i1 %10, label %11, label %29

11:                                               ; preds = %7
  store i8 2, ptr @R, align 1, !tbaa !5
  %12 = load i8, ptr %2, align 1, !tbaa !5
  %13 = add i8 %12, -1
  store i8 %13, ptr %2, align 1, !tbaa !5
  br label %14

14:                                               ; preds = %18, %11
  %15 = load i8, ptr %2, align 1, !tbaa !5
  %16 = zext i8 %15 to i32
  %17 = icmp sgt i32 %16, 1
  br i1 %17, label %18, label %28

18:                                               ; preds = %14
  %19 = load i8, ptr @R, align 1, !tbaa !5
  %20 = zext i8 %19 to i32
  %21 = mul nsw i32 256, %20
  %22 = add nsw i32 %21, 2
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [0 x i8], ptr @gmult_aes, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !5
  store i8 %25, ptr @R, align 1, !tbaa !5
  %26 = load i8, ptr %2, align 1, !tbaa !5
  %27 = add i8 %26, -1
  store i8 %27, ptr %2, align 1, !tbaa !5
  br label %14, !llvm.loop !8

28:                                               ; preds = %14
  br label %29

29:                                               ; preds = %28, %7
  br label %30

30:                                               ; preds = %29, %6
  ret ptr @R
}

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"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"

@gmult_aes = external local_unnamed_addr global [0 x i8], align 1
@R = external dso_local global [4 x i8], align 1

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef nonnull ptr @Rcon(i8 noundef zeroext %0) local_unnamed_addr #0 {
  switch i8 %0, label %.preheader [
    i8 1, label %.sink.split
    i8 0, label %32
  ]

.preheader:                                       ; preds = %1
  %2 = add i8 %0, -3
  %3 = icmp ult i8 %2, -2
  br i1 %3, label %.lr.ph.preheader, label %.sink.split

.lr.ph.preheader:                                 ; preds = %.preheader
  %4 = add i8 %0, -2
  %5 = add i8 %0, -3
  %xtraiter = and i8 %4, 3
  %6 = icmp ult i8 %5, 3
  br i1 %6, label %.sink.split.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i8 %4, -4
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %storemerge7 = phi i8 [ 2, %.lr.ph.preheader.new ], [ %26, %.lr.ph ]
  %niter = phi i8 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %7 = zext i8 %storemerge7 to i64
  %8 = shl nuw nsw i64 %7, 8
  %9 = or disjoint i64 %8, 2
  %10 = getelementptr inbounds [0 x i8], ptr @gmult_aes, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = zext i8 %11 to i64
  %13 = shl nuw nsw i64 %12, 8
  %14 = or disjoint i64 %13, 2
  %15 = getelementptr inbounds [0 x i8], ptr @gmult_aes, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = zext i8 %16 to i64
  %18 = shl nuw nsw i64 %17, 8
  %19 = or disjoint i64 %18, 2
  %20 = getelementptr inbounds [0 x i8], ptr @gmult_aes, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !5
  %22 = zext i8 %21 to i64
  %23 = shl nuw nsw i64 %22, 8
  %24 = or disjoint i64 %23, 2
  %25 = getelementptr inbounds [0 x i8], ptr @gmult_aes, i64 0, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !5
  %niter.next.3 = add i8 %niter, 4
  %niter.ncmp.3.not = icmp eq i8 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3.not, label %.sink.split.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !8

.sink.split.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i8 [ poison, %.lr.ph.preheader ], [ %26, %.lr.ph ]
  %storemerge7.unr = phi i8 [ 2, %.lr.ph.preheader ], [ %26, %.lr.ph ]
  %lcmp.mod.not = icmp eq i8 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.sink.split, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.sink.split.loopexit.unr-lcssa, %.lr.ph.epil
  %storemerge7.epil = phi i8 [ %31, %.lr.ph.epil ], [ %storemerge7.unr, %.sink.split.loopexit.unr-lcssa ]
  %epil.iter = phi i8 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.sink.split.loopexit.unr-lcssa ]
  %27 = zext i8 %storemerge7.epil to i64
  %28 = shl nuw nsw i64 %27, 8
  %29 = or disjoint i64 %28, 2
  %30 = getelementptr inbounds [0 x i8], ptr @gmult_aes, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !5
  %epil.iter.next = add i8 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i8 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.sink.split, label %.lr.ph.epil, !llvm.loop !10

.sink.split:                                      ; preds = %.sink.split.loopexit.unr-lcssa, %.lr.ph.epil, %.preheader, %1
  %storemerge.lcssa.sink = phi i8 [ %0, %1 ], [ 2, %.preheader ], [ %.lcssa.ph, %.sink.split.loopexit.unr-lcssa ], [ %31, %.lr.ph.epil ]
  store i8 %storemerge.lcssa.sink, ptr @R, align 1, !tbaa !5
  br label %32

32:                                               ; preds = %.sink.split, %1
  ret ptr @R
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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" }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass