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

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

@Nb = external dso_local global i32, align 4
@inv_s_box = external hidden global [256 x i8], align 16

; 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

; Function Attrs: nounwind uwtable
define dso_local void @inv_sub_bytes(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca i8, align 1
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #2
  store i8 0, ptr %3, align 1, !tbaa !9
  br label %5

5:                                                ; preds = %44, %1
  %6 = load i8, ptr %3, align 1, !tbaa !9
  %7 = zext i8 %6 to i32
  %8 = icmp slt i32 %7, 4
  br i1 %8, label %9, label %47

9:                                                ; preds = %5
  store i8 0, ptr %4, align 1, !tbaa !9
  br label %10

10:                                               ; preds = %40, %9
  %11 = load i8, ptr %4, align 1, !tbaa !9
  %12 = zext i8 %11 to i32
  %13 = load i32, ptr @Nb, align 4, !tbaa !10
  %14 = icmp slt i32 %12, %13
  br i1 %14, label %15, label %43

15:                                               ; preds = %10
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = load i32, ptr @Nb, align 4, !tbaa !10
  %18 = load i8, ptr %3, align 1, !tbaa !9
  %19 = zext i8 %18 to i32
  %20 = mul nsw i32 %17, %19
  %21 = load i8, ptr %4, align 1, !tbaa !9
  %22 = zext i8 %21 to i32
  %23 = add nsw i32 %20, %22
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %16, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = zext i8 %26 to i64
  %28 = getelementptr inbounds [256 x i8], ptr @inv_s_box, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = load i32, ptr @Nb, align 4, !tbaa !10
  %32 = load i8, ptr %3, align 1, !tbaa !9
  %33 = zext i8 %32 to i32
  %34 = mul nsw i32 %31, %33
  %35 = load i8, ptr %4, align 1, !tbaa !9
  %36 = zext i8 %35 to i32
  %37 = add nsw i32 %34, %36
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %30, i64 %38
  store i8 %29, ptr %39, align 1, !tbaa !9
  br label %40

40:                                               ; preds = %15
  %41 = load i8, ptr %4, align 1, !tbaa !9
  %42 = add i8 %41, 1
  store i8 %42, ptr %4, align 1, !tbaa !9
  br label %10, !llvm.loop !12

43:                                               ; preds = %10
  br label %44

44:                                               ; preds = %43
  %45 = load i8, ptr %3, align 1, !tbaa !9
  %46 = add i8 %45, 1
  store i8 %46, ptr %3, align 1, !tbaa !9
  br label %5, !llvm.loop !14

47:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #2
  ret void
}

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


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

@Nb = external dso_local local_unnamed_addr global i32, align 4
@inv_s_box = external hidden local_unnamed_addr global [256 x i8], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @inv_sub_bytes(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @Nb, align 4, !tbaa !5
  %3 = icmp sgt i32 %2, 0
  br i1 %3, label %.lr.ph, label %.split16.us

.lr.ph:                                           ; preds = %1, %.lr.ph
  %4 = phi i32 [ %12, %.lr.ph ], [ 0, %1 ]
  %.013 = phi i8 [ %11, %.lr.ph ], [ 0, %1 ]
  %5 = zext nneg i32 %4 to i64
  %6 = getelementptr inbounds i8, ptr %0, i64 %5
  %7 = load i8, ptr %6, align 1, !tbaa !9
  %8 = zext i8 %7 to i64
  %9 = getelementptr inbounds [256 x i8], ptr @inv_s_box, i64 0, i64 %8
  %10 = load i8, ptr %9, align 1, !tbaa !9
  store i8 %10, ptr %6, align 1, !tbaa !9
  %11 = add i8 %.013, 1
  %12 = zext i8 %11 to i32
  %13 = load i32, ptr @Nb, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, %12
  br i1 %14, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph
  %15 = icmp sgt i32 %13, 0
  br i1 %15, label %.lr.ph.1, label %.split16.us

.lr.ph.1:                                         ; preds = %._crit_edge, %.lr.ph.1
  %16 = phi i32 [ %27, %.lr.ph.1 ], [ %13, %._crit_edge ]
  %17 = phi i32 [ %26, %.lr.ph.1 ], [ 0, %._crit_edge ]
  %.013.1 = phi i8 [ %25, %.lr.ph.1 ], [ 0, %._crit_edge ]
  %18 = add nuw nsw i32 %16, %17
  %19 = zext nneg i32 %18 to i64
  %20 = getelementptr inbounds i8, ptr %0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !9
  %22 = zext i8 %21 to i64
  %23 = getelementptr inbounds [256 x i8], ptr @inv_s_box, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  store i8 %24, ptr %20, align 1, !tbaa !9
  %25 = add i8 %.013.1, 1
  %26 = zext i8 %25 to i32
  %27 = load i32, ptr @Nb, align 4, !tbaa !5
  %28 = icmp sgt i32 %27, %26
  br i1 %28, label %.lr.ph.1, label %._crit_edge.1, !llvm.loop !10

._crit_edge.1:                                    ; preds = %.lr.ph.1
  %29 = icmp sgt i32 %27, 0
  br i1 %29, label %.lr.ph.2, label %.split16.us

.lr.ph.2:                                         ; preds = %._crit_edge.1, %.lr.ph.2
  %30 = phi i32 [ %42, %.lr.ph.2 ], [ %27, %._crit_edge.1 ]
  %31 = phi i32 [ %41, %.lr.ph.2 ], [ 0, %._crit_edge.1 ]
  %.013.2 = phi i8 [ %40, %.lr.ph.2 ], [ 0, %._crit_edge.1 ]
  %32 = shl nuw nsw i32 %30, 1
  %33 = add nuw nsw i32 %32, %31
  %34 = zext nneg i32 %33 to i64
  %35 = getelementptr inbounds i8, ptr %0, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !9
  %37 = zext i8 %36 to i64
  %38 = getelementptr inbounds [256 x i8], ptr @inv_s_box, i64 0, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !9
  store i8 %39, ptr %35, align 1, !tbaa !9
  %40 = add i8 %.013.2, 1
  %41 = zext i8 %40 to i32
  %42 = load i32, ptr @Nb, align 4, !tbaa !5
  %43 = icmp sgt i32 %42, %41
  br i1 %43, label %.lr.ph.2, label %._crit_edge.2, !llvm.loop !10

._crit_edge.2:                                    ; preds = %.lr.ph.2
  %44 = icmp sgt i32 %42, 0
  br i1 %44, label %.lr.ph.3, label %.split16.us

.lr.ph.3:                                         ; preds = %._crit_edge.2, %.lr.ph.3
  %45 = phi i32 [ %57, %.lr.ph.3 ], [ %42, %._crit_edge.2 ]
  %46 = phi i32 [ %56, %.lr.ph.3 ], [ 0, %._crit_edge.2 ]
  %.013.3 = phi i8 [ %55, %.lr.ph.3 ], [ 0, %._crit_edge.2 ]
  %47 = mul nsw i32 %45, 3
  %48 = add nuw nsw i32 %47, %46
  %49 = zext nneg i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %0, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !9
  %52 = zext i8 %51 to i64
  %53 = getelementptr inbounds [256 x i8], ptr @inv_s_box, i64 0, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !9
  store i8 %54, ptr %50, align 1, !tbaa !9
  %55 = add i8 %.013.3, 1
  %56 = zext i8 %55 to i32
  %57 = load i32, ptr @Nb, align 4, !tbaa !5
  %58 = icmp sgt i32 %57, %56
  br i1 %58, label %.lr.ph.3, label %.split16.us, !llvm.loop !10

.split16.us:                                      ; preds = %.lr.ph.3, %._crit_edge, %._crit_edge.1, %._crit_edge.2, %1
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: readwrite, 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass