; LLVM IR from trustwallet/wallet-core :: trezor-crypto/crypto (rc4)

; 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.RC4_CTX = type { [256 x i8], i8, i8 }

; 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: inlinehint nounwind uwtable
declare hidden void @rc4_swap(ptr noundef, i8 noundef zeroext, i8 noundef zeroext) #1

; Function Attrs: nounwind uwtable
define dso_local void @rc4_encrypt(ptr noundef %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i8, align 1
  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 8, ptr %7) #3
  store i64 0, ptr %7, align 8, !tbaa !9
  br label %9

9:                                                ; preds = %75, %3
  %10 = load i64, ptr %7, align 8, !tbaa !9
  %11 = load i64, ptr %6, align 8, !tbaa !9
  %12 = icmp ult i64 %10, %11
  br i1 %12, label %14, label %13

13:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  br label %78

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

75:                                               ; preds = %14
  %76 = load i64, ptr %7, align 8, !tbaa !9
  %77 = add i64 %76, 1
  store i64 %77, ptr %7, align 8, !tbaa !9
  br label %9, !llvm.loop !15

78:                                               ; preds = %13
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 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 = { 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, !7, i64 256}
!12 = !{!"", !7, i64 0, !7, i64 256, !7, i64 257}
!13 = !{!7, !7, i64 0}
!14 = !{!12, !7, i64 257}
!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"

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @rc4_swap(ptr noundef, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @rc4_encrypt(ptr noundef %0, ptr nocapture noundef %1, i64 noundef %2) local_unnamed_addr #1 {
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %4 = getelementptr inbounds i8, ptr %0, i64 256
  %5 = getelementptr inbounds i8, ptr %0, i64 257
  br label %6

._crit_edge:                                      ; preds = %6, %3
  ret void

6:                                                ; preds = %.lr.ph, %6
  %.017 = phi i64 [ 0, %.lr.ph ], [ %28, %6 ]
  %7 = load i8, ptr %4, align 1, !tbaa !5
  %8 = add i8 %7, 1
  store i8 %8, ptr %4, align 1, !tbaa !5
  %9 = zext i8 %8 to i64
  %10 = getelementptr inbounds [256 x i8], ptr %0, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = load i8, ptr %5, align 1, !tbaa !10
  %13 = add i8 %12, %11
  store i8 %13, ptr %5, align 1, !tbaa !10
  tail call void @rc4_swap(ptr noundef %0, i8 noundef zeroext %8, i8 noundef zeroext %13)
  %14 = load i8, ptr %4, align 1, !tbaa !5
  %15 = zext i8 %14 to i64
  %16 = getelementptr inbounds [256 x i8], ptr %0, i64 0, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = load i8, ptr %5, align 1, !tbaa !10
  %19 = zext i8 %18 to i64
  %20 = getelementptr inbounds [256 x i8], ptr %0, i64 0, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !9
  %.narrow = add i8 %21, %17
  %22 = zext i8 %.narrow to i64
  %23 = getelementptr inbounds [256 x i8], ptr %0, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %25 = getelementptr inbounds i8, ptr %1, i64 %.017
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = xor i8 %26, %24
  store i8 %27, ptr %25, align 1, !tbaa !9
  %28 = add nuw i64 %.017, 1
  %exitcond.not = icmp eq i64 %28, %2
  br i1 %exitcond.not, label %._crit_edge, label %6, !llvm.loop !11
}

attributes #0 = { inlinehint 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 = { 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, !7, i64 256}
!6 = !{!"", !7, i64 0, !7, i64 256, !7, i64 257}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!6, !7, i64 257}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass