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

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

15:                                               ; preds = %4
  %16 = load i8, ptr %7, align 1, !tbaa !9
  %17 = sext i8 %16 to i32
  %18 = icmp ne i32 %17, -1
  br i1 %18, label %19, label %20

19:                                               ; preds = %15
  store i8 1, ptr %5, align 1
  br label %49

20:                                               ; preds = %15, %4
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = call signext i8 @_check_key(ptr noundef %21)
  %23 = icmp ne i8 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %20
  store i8 2, ptr %5, align 1
  br label %49

25:                                               ; preds = %20
  %26 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %27 = zext i8 %26 to i32
  %28 = add nsw i32 %27, 1
  %29 = zext i32 %28 to i64
  %30 = call ptr @llvm.stacksave.p0()
  store ptr %30, ptr %10, align 8
  %31 = alloca i8, i64 %29, align 16
  store i64 %29, ptr %11, align 8
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  call void @_set_key(ptr noundef %31, ptr noundef %32)
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  call void @_initial_setup(ptr noundef %33, ptr noundef %34)
  br label %35

35:                                               ; preds = %43, %25
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = load i8, ptr %36, align 1, !tbaa !9
  %38 = zext i8 %37 to i32
  %39 = icmp ne i32 %38, 0
  br i1 %39, label %40, label %46

40:                                               ; preds = %35
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i8, ptr %7, align 1, !tbaa !9
  call void @_chars_encrypt(ptr noundef %41, i8 noundef signext %42, ptr noundef %31)
  br label %43

43:                                               ; preds = %40
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds i8, ptr %44, i64 2
  store ptr %45, ptr %6, align 8, !tbaa !5
  br label %35, !llvm.loop !10

46:                                               ; preds = %35
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %47, align 1, !tbaa !9
  store i8 0, ptr %5, align 1
  %48 = load ptr, ptr %10, align 8
  call void @llvm.stackrestore.p0(ptr %48)
  br label %49

49:                                               ; preds = %46, %24, %19
  %50 = load i8, ptr %5, align 1
  ret i8 %50
}

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

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

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

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

; Function Attrs: nounwind uwtable
declare hidden void @_chars_encrypt(ptr noundef, i8 noundef signext, ptr noundef) #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 @playfair(ptr noundef %0, i8 noundef signext %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  switch i8 %1, label %16 [
    i8 -1, label %5
    i8 1, label %5
  ]

5:                                                ; preds = %4, %4
  %6 = tail call signext i8 @_check_key(ptr noundef %2)
  %.not = icmp eq i8 %6, 0
  br i1 %.not, label %7, label %16

7:                                                ; preds = %5
  %8 = load i8, ptr @__length_alpha, align 1, !tbaa !5
  %9 = zext i8 %8 to i64
  %10 = add nuw nsw i64 %9, 1
  %11 = tail call ptr @llvm.stacksave.p0()
  %12 = alloca i8, i64 %10, align 16
  call void @_set_key(ptr noundef nonnull %12, ptr noundef %2)
  call void @_initial_setup(ptr noundef %0, ptr noundef %3)
  %13 = load i8, ptr %0, align 1, !tbaa !5
  %.not1516 = icmp eq i8 %13, 0
  br i1 %.not1516, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %7, %.lr.ph
  %.01417 = phi ptr [ %14, %.lr.ph ], [ %0, %7 ]
  call void @_chars_encrypt(ptr noundef nonnull %.01417, i8 noundef signext %1, ptr noundef nonnull %12)
  %14 = getelementptr inbounds i8, ptr %.01417, i64 2
  %15 = load i8, ptr %14, align 1, !tbaa !5
  %.not15 = icmp eq i8 %15, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph, !llvm.loop !8

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

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

; Function Attrs: nounwind uwtable
declare hidden signext i8 @_check_key(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_key(ptr noundef, ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare hidden void @_chars_encrypt(ptr noundef, i8 noundef signext, ptr noundef) 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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass