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

; 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_wordkey(ptr noundef %0, i8 noundef signext %1, i8 noundef signext %2, ptr noundef %3, ptr noundef %4) #0 {
  %6 = alloca i8, align 1
  %7 = alloca ptr, align 8
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i8 %1, ptr %8, align 1, !tbaa !9
  store i8 %2, ptr %9, align 1, !tbaa !9
  store ptr %3, ptr %10, align 8, !tbaa !5
  store ptr %4, ptr %11, align 8, !tbaa !5
  %14 = load i8, ptr %8, align 1, !tbaa !9
  %15 = sext i8 %14 to i32
  %16 = icmp ne i32 %15, 1
  br i1 %16, label %17, label %22

17:                                               ; preds = %5
  %18 = load i8, ptr %8, align 1, !tbaa !9
  %19 = sext i8 %18 to i32
  %20 = icmp ne i32 %19, -1
  br i1 %20, label %21, label %22

21:                                               ; preds = %17
  store i8 1, ptr %6, align 1
  br label %76

22:                                               ; preds = %17, %5
  %23 = load ptr, ptr %10, align 8, !tbaa !5
  %24 = call signext i8 @_check_wordkey(ptr noundef %23)
  %25 = icmp ne i8 %24, 0
  br i1 %25, label %26, label %27

26:                                               ; preds = %22
  store i8 2, ptr %6, align 1
  br label %76

27:                                               ; preds = %22
  %28 = load i8, ptr %9, align 1, !tbaa !9
  %29 = sext i8 %28 to i32
  %30 = icmp slt i32 %29, 0
  br i1 %30, label %31, label %40

31:                                               ; preds = %27
  %32 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %33 = zext i8 %32 to i32
  %34 = load i8, ptr %9, align 1, !tbaa !9
  %35 = sext i8 %34 to i32
  %36 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %37 = zext i8 %36 to i32
  %38 = srem i32 %35, %37
  %39 = add nsw i32 %33, %38
  br label %46

40:                                               ; preds = %27
  %41 = load i8, ptr %9, align 1, !tbaa !9
  %42 = sext i8 %41 to i32
  %43 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %44 = zext i8 %43 to i32
  %45 = srem i32 %42, %44
  br label %46

46:                                               ; preds = %40, %31
  %47 = phi i32 [ %39, %31 ], [ %45, %40 ]
  %48 = load i8, ptr %8, align 1, !tbaa !9
  %49 = sext i8 %48 to i32
  %50 = mul nsw i32 %47, %49
  %51 = trunc i32 %50 to i8
  store i8 %51, ptr %9, align 1, !tbaa !9
  %52 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %53 = zext i8 %52 to i32
  %54 = add nsw i32 %53, 1
  %55 = zext i32 %54 to i64
  %56 = call ptr @llvm.stacksave.p0()
  store ptr %56, ptr %12, align 8
  %57 = alloca i8, i64 %55, align 16
  store i64 %55, ptr %13, align 8
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  call void @_set_wordkey(ptr noundef %57, ptr noundef %58)
  br label %59

59:                                               ; preds = %64, %46
  %60 = load ptr, ptr %11, align 8, !tbaa !5
  %61 = load i8, ptr %60, align 1, !tbaa !9
  %62 = zext i8 %61 to i32
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %73

64:                                               ; preds = %59
  %65 = load i8, ptr %8, align 1, !tbaa !9
  %66 = load i8, ptr %9, align 1, !tbaa !9
  %67 = load ptr, ptr %11, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i32 1
  store ptr %68, ptr %11, align 8, !tbaa !5
  %69 = load i8, ptr %67, align 1, !tbaa !9
  %70 = call zeroext i8 @_char_encrypt(i8 noundef signext %65, i8 noundef signext %66, ptr noundef %57, i8 noundef zeroext %69)
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds i8, ptr %71, i32 1
  store ptr %72, ptr %7, align 8, !tbaa !5
  store i8 %70, ptr %71, align 1, !tbaa !9
  br label %59, !llvm.loop !10

73:                                               ; preds = %59
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %74, align 1, !tbaa !9
  store i8 0, ptr %6, align 1
  %75 = load ptr, ptr %12, align 8
  call void @llvm.stackrestore.p0(ptr %75)
  br label %76

76:                                               ; preds = %73, %26, %21
  %77 = load i8, ptr %6, align 1
  ret i8 %77
}

; Function Attrs: nounwind uwtable
declare hidden signext i8 @_check_wordkey(ptr noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: nounwind uwtable
declare hidden void @_set_wordkey(ptr noundef, ptr noundef) #0

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn }

!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, 3) i8 @caesar_wordkey(ptr nocapture noundef writeonly %0, i8 noundef signext %1, i8 noundef signext %2, ptr noundef %3, ptr nocapture noundef readonly %4) local_unnamed_addr #0 {
  switch i8 %1, label %29 [
    i8 -1, label %6
    i8 1, label %6
  ]

6:                                                ; preds = %5, %5
  %7 = tail call signext i8 @_check_wordkey(ptr noundef %3)
  %.not = icmp eq i8 %7, 0
  br i1 %.not, label %8, label %29

8:                                                ; preds = %6
  %9 = icmp slt i8 %2, 0
  %10 = load i8, ptr @__length_alpha, align 1, !tbaa !5
  br i1 %9, label %11, label %14

11:                                               ; preds = %8
  %.lhs.trunc.nonneg = sub i8 0, %2
  %12 = urem i8 %.lhs.trunc.nonneg, %10
  %13 = sub i8 %10, %12
  br label %16

14:                                               ; preds = %8
  %15 = urem i8 %2, %10
  br label %16

16:                                               ; preds = %14, %11
  %17 = phi i8 [ %13, %11 ], [ %15, %14 ]
  %18 = mul i8 %17, %1
  %19 = zext i8 %10 to i64
  %20 = add nuw nsw i64 %19, 1
  %21 = tail call ptr @llvm.stacksave.p0()
  %22 = alloca i8, i64 %20, align 16
  call void @_set_wordkey(ptr noundef nonnull %22, ptr noundef %3)
  %23 = load i8, ptr %4, align 1, !tbaa !5
  %.not2021 = icmp eq i8 %23, 0
  br i1 %.not2021, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %16, %.lr.ph
  %24 = phi i8 [ %28, %.lr.ph ], [ %23, %16 ]
  %.01723 = phi ptr [ %27, %.lr.ph ], [ %0, %16 ]
  %.01822 = phi ptr [ %25, %.lr.ph ], [ %4, %16 ]
  %25 = getelementptr inbounds i8, ptr %.01822, i64 1
  %26 = call zeroext i8 @_char_encrypt(i8 noundef signext %1, i8 noundef signext %18, ptr noundef nonnull %22, i8 noundef zeroext %24)
  %27 = getelementptr inbounds i8, ptr %.01723, i64 1
  store i8 %26, ptr %.01723, align 1, !tbaa !5
  %28 = load i8, ptr %25, align 1, !tbaa !5
  %.not20 = icmp eq i8 %28, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %.lr.ph, %16
  %.017.lcssa = phi ptr [ %0, %16 ], [ %27, %.lr.ph ]
  store i8 0, ptr %.017.lcssa, align 1, !tbaa !5
  call void @llvm.stackrestore.p0(ptr %21)
  br label %29

29:                                               ; preds = %6, %5, %._crit_edge
  %.0 = phi i8 [ 0, %._crit_edge ], [ 1, %5 ], [ 2, %6 ]
  ret i8 %.0
}

; Function Attrs: nounwind uwtable
declare hidden signext i8 @_check_wordkey(ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

; Function Attrs: nounwind uwtable
declare hidden void @_set_wordkey(ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn }

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass