; LLVM IR from Farama-Foundation/stable-retro :: cores/gba/src/third-party/lzma (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"

@Sbox = external hidden constant [256 x i8], align 16
@D = external hidden global [1024 x i32], 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
declare dso_local void @Aes_SetKey_Enc(ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @Aes_SetKey_Dec(ptr noundef %0, ptr noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = load i32, ptr %6, align 4, !tbaa !9
  call void @Aes_SetKey_Enc(ptr noundef %10, ptr noundef %11, i32 noundef %12)
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = add i32 %13, 20
  store i32 %14, ptr %8, align 4, !tbaa !9
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds i32, ptr %15, i64 8
  store ptr %16, ptr %4, align 8, !tbaa !5
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %75, %3
  %18 = load i32, ptr %7, align 4, !tbaa !9
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = icmp ult i32 %18, %19
  br i1 %20, label %21, label %78

21:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %22, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  store i32 %26, ptr %9, align 4, !tbaa !9
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = and i32 %27, 255
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !11
  %32 = zext i8 %31 to i64
  %33 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = lshr i32 %35, 8
  %37 = and i32 %36, 255
  %38 = zext i32 %37 to i64
  %39 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %38
  %40 = load i8, ptr %39, align 1, !tbaa !11
  %41 = zext i8 %40 to i32
  %42 = add nsw i32 256, %41
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = xor i32 %34, %45
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = lshr i32 %47, 16
  %49 = and i32 %48, 255
  %50 = zext i32 %49 to i64
  %51 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %50
  %52 = load i8, ptr %51, align 1, !tbaa !11
  %53 = zext i8 %52 to i32
  %54 = add nsw i32 512, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %55
  %57 = load i32, ptr %56, align 4, !tbaa !9
  %58 = xor i32 %46, %57
  %59 = load i32, ptr %9, align 4, !tbaa !9
  %60 = lshr i32 %59, 24
  %61 = and i32 %60, 255
  %62 = zext i32 %61 to i64
  %63 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %62
  %64 = load i8, ptr %63, align 1, !tbaa !11
  %65 = zext i8 %64 to i32
  %66 = add nsw i32 768, %65
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %67
  %69 = load i32, ptr %68, align 4, !tbaa !9
  %70 = xor i32 %58, %69
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = load i32, ptr %7, align 4, !tbaa !9
  %73 = zext i32 %72 to i64
  %74 = getelementptr inbounds i32, ptr %71, i64 %73
  store i32 %70, ptr %74, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %75

75:                                               ; preds = %21
  %76 = load i32, ptr %7, align 4, !tbaa !9
  %77 = add i32 %76, 1
  store i32 %77, ptr %7, align 4, !tbaa !9
  br label %17, !llvm.loop !12

78:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@Sbox = external hidden local_unnamed_addr constant [256 x i8], align 16
@D = external hidden local_unnamed_addr global [1024 x i32], align 16

; Function Attrs: nounwind uwtable
declare dso_local void @Aes_SetKey_Enc(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @Aes_SetKey_Dec(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  tail call void @Aes_SetKey_Enc(ptr noundef %0, ptr noundef %1, i32 noundef %2)
  %4 = add i32 %2, 20
  %5 = getelementptr inbounds i8, ptr %0, i64 32
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext i32 %4 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %6 = getelementptr inbounds i32, ptr %5, i64 %indvars.iv
  %7 = load i32, ptr %6, align 4, !tbaa !5
  %8 = and i32 %7, 255
  %9 = zext nneg i32 %8 to i64
  %10 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = zext i8 %11 to i64
  %13 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %12
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = lshr i32 %7, 8
  %16 = and i32 %15, 255
  %17 = zext nneg i32 %16 to i64
  %18 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = zext i8 %19 to i64
  %21 = or disjoint i64 %20, 256
  %22 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %21
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = xor i32 %23, %14
  %25 = lshr i32 %7, 16
  %26 = and i32 %25, 255
  %27 = zext nneg i32 %26 to i64
  %28 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = zext i8 %29 to i64
  %31 = or disjoint i64 %30, 512
  %32 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = xor i32 %24, %33
  %35 = lshr i32 %7, 24
  %36 = zext nneg i32 %35 to i64
  %37 = getelementptr inbounds [256 x i8], ptr @Sbox, i64 0, i64 %36
  %38 = load i8, ptr %37, align 1, !tbaa !9
  %39 = zext i8 %38 to i64
  %40 = or disjoint i64 %39, 768
  %41 = getelementptr inbounds [1024 x i32], ptr @D, i64 0, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = xor i32 %34, %42
  store i32 %43, ptr %6, align 4, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %3
  ret void
}

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 = !{!"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,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass