; LLVM IR from number571/c-crypto-lib :: crypto/cipher (substitution)

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

@__alpha = external hidden global [2 x [127 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 signext i8 @substitution(ptr noundef %0, i8 noundef signext %1, ptr noundef %2) #1 {
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  %10 = load i8, ptr %6, align 1, !tbaa !9
  %11 = sext i8 %10 to i32
  %12 = icmp ne i32 %11, 1
  br i1 %12, label %13, label %18

13:                                               ; preds = %3
  %14 = load i8, ptr %6, align 1, !tbaa !9
  %15 = sext i8 %14 to i32
  %16 = icmp ne i32 %15, -1
  br i1 %16, label %17, label %18

17:                                               ; preds = %13
  store i8 1, ptr %4, align 1
  br label %47

18:                                               ; preds = %13, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %19 = load i8, ptr %6, align 1, !tbaa !9
  %20 = sext i8 %19 to i32
  %21 = icmp eq i32 %20, 1
  %22 = zext i1 %21 to i64
  %23 = select i1 %21, ptr getelementptr inbounds ([2 x [127 x i8]], ptr @__alpha, i64 0, i64 1), ptr @__alpha
  store ptr %23, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %24 = load i8, ptr %6, align 1, !tbaa !9
  %25 = sext i8 %24 to i32
  %26 = icmp eq i32 %25, -1
  %27 = zext i1 %26 to i64
  %28 = select i1 %26, ptr getelementptr inbounds ([2 x [127 x i8]], ptr @__alpha, i64 0, i64 1), ptr @__alpha
  store ptr %28, ptr %9, align 8, !tbaa !5
  br label %29

29:                                               ; preds = %42, %18
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = zext i8 %31 to i32
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %34, label %45

34:                                               ; preds = %29
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = load i8, ptr %36, align 1, !tbaa !9
  %38 = load ptr, ptr %9, align 8, !tbaa !5
  %39 = call zeroext i8 @_char_encrypt(ptr noundef %35, i8 noundef zeroext %37, ptr noundef %38)
  %40 = load ptr, ptr %5, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i32 1
  store ptr %41, ptr %5, align 8, !tbaa !5
  store i8 %39, ptr %40, align 1, !tbaa !9
  br label %42

42:                                               ; preds = %34
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i32 1
  store ptr %44, ptr %7, align 8, !tbaa !5
  br label %29, !llvm.loop !10

45:                                               ; preds = %29
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  store i8 0, ptr %46, align 1, !tbaa !9
  store i8 0, ptr %4, align 1
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %47

47:                                               ; preds = %45, %17
  %48 = load i8, ptr %4, align 1
  ret i8 %48
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(ptr noundef, i8 noundef zeroext, ptr noundef) #1

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 = distinct !{!10, !11}
!11 = !{!"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"

@__alpha = external hidden global [2 x [127 x i8]], align 16

; Function Attrs: nounwind uwtable
define dso_local signext range(i8 0, 2) i8 @substitution(ptr nocapture noundef writeonly %0, i8 noundef signext %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  switch i8 %1, label %15 [
    i8 -1, label %4
    i8 1, label %4
  ]

4:                                                ; preds = %3, %3
  %5 = icmp eq i8 %1, 1
  %6 = select i1 %5, ptr getelementptr inbounds (i8, ptr @__alpha, i64 127), ptr @__alpha
  %7 = icmp eq i8 %1, -1
  %8 = select i1 %7, ptr getelementptr inbounds (i8, ptr @__alpha, i64 127), ptr @__alpha
  %9 = load i8, ptr %2, align 1, !tbaa !5
  %.not15 = icmp eq i8 %9, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %4, %.lr.ph
  %10 = phi i8 [ %14, %.lr.ph ], [ %9, %4 ]
  %.01317 = phi ptr [ %12, %.lr.ph ], [ %0, %4 ]
  %.01416 = phi ptr [ %13, %.lr.ph ], [ %2, %4 ]
  %11 = tail call zeroext i8 @_char_encrypt(ptr noundef nonnull %6, i8 noundef zeroext %10, ptr noundef nonnull %8)
  %12 = getelementptr inbounds i8, ptr %.01317, i64 1
  store i8 %11, ptr %.01317, align 1, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %.01416, i64 1
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %.not = icmp eq i8 %14, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %4
  %.013.lcssa = phi ptr [ %0, %4 ], [ %12, %.lr.ph ]
  store i8 0, ptr %.013.lcssa, align 1, !tbaa !5
  br label %15

15:                                               ; preds = %3, %._crit_edge
  %.0 = phi i8 [ 0, %._crit_edge ], [ 1, %3 ]
  ret i8 %.0
}

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

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


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