; LLVM IR from CactiLab/Sherloc-Cortex-M-CFVD :: Sherloc_runtime/evaluation/src/nettle_arcfour (arcfour)

; 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.arcfour_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: nounwind uwtable
define dso_local void @arcfour_crypt(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i8, align 1
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.arcfour_ctx, ptr %13, i32 0, i32 1
  %15 = load i8, ptr %14, align 1, !tbaa !11
  store i8 %15, ptr %9, align 1, !tbaa !13
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.arcfour_ctx, ptr %16, i32 0, i32 2
  %18 = load i8, ptr %17, align 1, !tbaa !14
  store i8 %18, ptr %10, align 1, !tbaa !13
  br label %19

19:                                               ; preds = %23, %4
  %20 = load i32, ptr %6, align 4, !tbaa !9
  %21 = add nsw i32 %20, -1
  store i32 %21, ptr %6, align 4, !tbaa !9
  %22 = icmp ne i32 %20, 0
  br i1 %22, label %23, label %83

23:                                               ; preds = %19
  %24 = load i8, ptr %9, align 1, !tbaa !13
  %25 = add i8 %24, 1
  store i8 %25, ptr %9, align 1, !tbaa !13
  %26 = load i8, ptr %9, align 1, !tbaa !13
  %27 = zext i8 %26 to i32
  %28 = and i32 %27, 255
  %29 = trunc i32 %28 to i8
  store i8 %29, ptr %9, align 1, !tbaa !13
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.arcfour_ctx, ptr %30, i32 0, i32 0
  %32 = load i8, ptr %9, align 1, !tbaa !13
  %33 = zext i8 %32 to i64
  %34 = getelementptr inbounds [256 x i8], ptr %31, i64 0, i64 %33
  %35 = load i8, ptr %34, align 1, !tbaa !13
  %36 = zext i8 %35 to i32
  store i32 %36, ptr %11, align 4, !tbaa !9
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = load i8, ptr %10, align 1, !tbaa !13
  %39 = zext i8 %38 to i32
  %40 = add nsw i32 %39, %37
  %41 = trunc i32 %40 to i8
  store i8 %41, ptr %10, align 1, !tbaa !13
  %42 = load i8, ptr %10, align 1, !tbaa !13
  %43 = zext i8 %42 to i32
  %44 = and i32 %43, 255
  %45 = trunc i32 %44 to i8
  store i8 %45, ptr %10, align 1, !tbaa !13
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.arcfour_ctx, ptr %46, i32 0, i32 0
  %48 = load i8, ptr %10, align 1, !tbaa !13
  %49 = zext i8 %48 to i64
  %50 = getelementptr inbounds [256 x i8], ptr %47, i64 0, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !13
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.arcfour_ctx, ptr %52, i32 0, i32 0
  %54 = load i8, ptr %9, align 1, !tbaa !13
  %55 = zext i8 %54 to i64
  %56 = getelementptr inbounds [256 x i8], ptr %53, i64 0, i64 %55
  store i8 %51, ptr %56, align 1, !tbaa !13
  %57 = zext i8 %51 to i32
  store i32 %57, ptr %12, align 4, !tbaa !9
  %58 = load i32, ptr %11, align 4, !tbaa !9
  %59 = trunc i32 %58 to i8
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.arcfour_ctx, ptr %60, i32 0, i32 0
  %62 = load i8, ptr %10, align 1, !tbaa !13
  %63 = zext i8 %62 to i64
  %64 = getelementptr inbounds [256 x i8], ptr %61, i64 0, i64 %63
  store i8 %59, ptr %64, align 1, !tbaa !13
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = getelementptr inbounds i8, ptr %65, i32 1
  store ptr %66, ptr %8, align 8, !tbaa !5
  %67 = load i8, ptr %65, align 1, !tbaa !13
  %68 = zext i8 %67 to i32
  %69 = load ptr, ptr %5, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.arcfour_ctx, ptr %69, i32 0, i32 0
  %71 = load i32, ptr %11, align 4, !tbaa !9
  %72 = load i32, ptr %12, align 4, !tbaa !9
  %73 = add nsw i32 %71, %72
  %74 = and i32 %73, 255
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [256 x i8], ptr %70, i64 0, i64 %75
  %77 = load i8, ptr %76, align 1, !tbaa !13
  %78 = zext i8 %77 to i32
  %79 = xor i32 %68, %78
  %80 = trunc i32 %79 to i8
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds i8, ptr %81, i32 1
  store ptr %82, ptr %7, align 8, !tbaa !5
  store i8 %80, ptr %81, align 1, !tbaa !13
  br label %19, !llvm.loop !15

83:                                               ; preds = %19
  %84 = load i8, ptr %9, align 1, !tbaa !13
  %85 = load ptr, ptr %5, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.arcfour_ctx, ptr %85, i32 0, i32 1
  store i8 %84, ptr %86, align 1, !tbaa !11
  %87 = load i8, ptr %10, align 1, !tbaa !13
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.arcfour_ctx, ptr %88, i32 0, i32 2
  store i8 %87, ptr %89, align 1, !tbaa !14
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #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 = !{!12, !7, i64 256}
!12 = !{!"arcfour_ctx", !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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @arcfour_crypt(ptr nocapture noundef %0, i32 noundef %1, ptr nocapture noundef writeonly %2, ptr nocapture noundef readonly %3) local_unnamed_addr #0 {
  %5 = getelementptr inbounds i8, ptr %0, i64 256
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 257
  %8 = load i8, ptr %7, align 1, !tbaa !9
  %.not29 = icmp eq i32 %1, 0
  br i1 %.not29, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %.lr.ph
  %.034 = phi i32 [ %9, %.lr.ph ], [ %1, %4 ]
  %.02533 = phi ptr [ %24, %.lr.ph ], [ %2, %4 ]
  %.02632 = phi i8 [ %14, %.lr.ph ], [ %8, %4 ]
  %.02731 = phi i8 [ %10, %.lr.ph ], [ %6, %4 ]
  %.02830 = phi ptr [ %18, %.lr.ph ], [ %3, %4 ]
  %9 = add nsw i32 %.034, -1
  %10 = add i8 %.02731, 1
  %11 = zext i8 %10 to i64
  %12 = getelementptr inbounds [256 x i8], ptr %0, i64 0, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !10
  %14 = add i8 %13, %.02632
  %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 !10
  store i8 %17, ptr %12, align 1, !tbaa !10
  store i8 %13, ptr %16, align 1, !tbaa !10
  %18 = getelementptr inbounds i8, ptr %.02830, i64 1
  %19 = load i8, ptr %.02830, align 1, !tbaa !10
  %.narrow = add i8 %17, %13
  %20 = zext i8 %.narrow to i64
  %21 = getelementptr inbounds [256 x i8], ptr %0, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !10
  %23 = xor i8 %22, %19
  %24 = getelementptr inbounds i8, ptr %.02533, i64 1
  store i8 %23, ptr %.02533, align 1, !tbaa !10
  %.not = icmp eq i32 %9, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph, %4
  %.027.lcssa = phi i8 [ %6, %4 ], [ %10, %.lr.ph ]
  %.026.lcssa = phi i8 [ %8, %4 ], [ %14, %.lr.ph ]
  store i8 %.027.lcssa, ptr %5, align 1, !tbaa !5
  store i8 %.026.lcssa, ptr %7, align 1, !tbaa !9
  ret void
}

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


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