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

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

@__length_alpha = external hidden global i8, align 1

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

18:                                               ; preds = %13, %4
  %19 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %20 = zext i8 %19 to i32
  %21 = load i8, ptr %8, align 1, !tbaa !9
  %22 = zext i8 %21 to i32
  %23 = srem i32 %22, %20
  %24 = trunc i32 %23 to i8
  store i8 %24, ptr %8, align 1, !tbaa !9
  br label %25

25:                                               ; preds = %30, %18
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = load i8, ptr %26, align 1, !tbaa !9
  %28 = zext i8 %27 to i32
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %43

30:                                               ; preds = %25
  %31 = load i8, ptr %7, align 1, !tbaa !9
  %32 = sext i8 %31 to i32
  %33 = load i8, ptr %8, align 1, !tbaa !9
  %34 = zext i8 %33 to i32
  %35 = mul nsw i32 %32, %34
  %36 = trunc i32 %35 to i8
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i32 1
  store ptr %38, ptr %9, align 8, !tbaa !5
  %39 = load i8, ptr %37, align 1, !tbaa !9
  %40 = call zeroext i8 @_char_encrypt(i8 noundef signext %36, i8 noundef zeroext %39)
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = getelementptr inbounds i8, ptr %41, i32 1
  store ptr %42, ptr %6, align 8, !tbaa !5
  store i8 %40, ptr %41, align 1, !tbaa !9
  br label %25, !llvm.loop !10

43:                                               ; preds = %25
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %44, align 1, !tbaa !9
  store i8 0, ptr %5, align 1
  br label %45

45:                                               ; preds = %43, %17
  %46 = load i8, ptr %5, align 1
  ret i8 %46
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(i8 noundef signext, i8 noundef zeroext) #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 = !{!"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"

@__length_alpha = external hidden local_unnamed_addr global i8, align 1

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

5:                                                ; preds = %4, %4
  %6 = load i8, ptr %3, align 1, !tbaa !5
  %.not13 = icmp eq i8 %6, 0
  br i1 %.not13, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %7 = load i8, ptr @__length_alpha, align 1, !tbaa !5
  %8 = urem i8 %2, %7
  %9 = mul i8 %8, %1
  br label %10

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

._crit_edge:                                      ; preds = %10, %5
  %.012.lcssa = phi ptr [ %0, %5 ], [ %14, %10 ]
  store i8 0, ptr %.012.lcssa, align 1, !tbaa !5
  br label %16

16:                                               ; preds = %4, %._crit_edge
  %.011 = phi i8 [ 0, %._crit_edge ], [ 1, %4 ]
  ret i8 %.011
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(i8 noundef signext, i8 noundef zeroext) 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,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass