; 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
@__sqrt_length_alpha = external hidden global i8, align 1
@__alpha = external hidden global [127 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local signext i8 @set_alpha_playfair(ptr noundef %0) #0 {
  %2 = alloca i8, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = call i64 @strlen(ptr noundef %8) #5
  store i64 %9, ptr %4, align 8, !tbaa !9
  %10 = load i64, ptr %4, align 8, !tbaa !9
  %11 = icmp uge i64 %10, 127
  br i1 %11, label %12, label %13

12:                                               ; preds = %1
  store i8 1, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %43

13:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  store i8 1, ptr %7, align 1, !tbaa !11
  br label %14

14:                                               ; preds = %29, %13
  %15 = load i8, ptr %7, align 1, !tbaa !11
  %16 = zext i8 %15 to i32
  %17 = load i8, ptr %7, align 1, !tbaa !11
  %18 = zext i8 %17 to i32
  %19 = mul nsw i32 %16, %18
  %20 = trunc i32 %19 to i8
  store i8 %20, ptr %6, align 1, !tbaa !11
  %21 = zext i8 %20 to i32
  %22 = icmp slt i32 %21, 127
  br i1 %22, label %23, label %32

23:                                               ; preds = %14
  %24 = load i8, ptr %6, align 1, !tbaa !11
  %25 = zext i8 %24 to i64
  %26 = load i64, ptr %4, align 8, !tbaa !9
  %27 = icmp eq i64 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %23
  br label %32

29:                                               ; preds = %23
  %30 = load i8, ptr %7, align 1, !tbaa !11
  %31 = add i8 %30, 1
  store i8 %31, ptr %7, align 1, !tbaa !11
  br label %14, !llvm.loop !12

32:                                               ; preds = %28, %14
  %33 = load i8, ptr %6, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  %35 = icmp sge i32 %34, 127
  br i1 %35, label %36, label %37

36:                                               ; preds = %32
  store i8 2, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %42

37:                                               ; preds = %32
  %38 = load i8, ptr %6, align 1, !tbaa !11
  store i8 %38, ptr @__length_alpha, align 1, !tbaa !11
  %39 = load i8, ptr %7, align 1, !tbaa !11
  store i8 %39, ptr @__sqrt_length_alpha, align 1, !tbaa !11
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = call ptr @strcpy(ptr noundef @__alpha, ptr noundef %40) #4
  store i8 0, ptr %2, align 1
  store i32 1, ptr %5, align 4
  br label %42

42:                                               ; preds = %37, %36
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #4
  br label %43

43:                                               ; preds = %42, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  %44 = load i8, ptr %2, align 1
  ret i8 %44
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

; Function Attrs: nounwind
declare ptr @strcpy(ptr noundef, ptr noundef) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 memory(argmem: readwrite) }
attributes #2 = { nounwind willreturn memory(read) "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 #3 = { nounwind "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 #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

!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 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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
@__sqrt_length_alpha = external hidden local_unnamed_addr global i8, align 1
@__alpha = external hidden global [127 x i8], align 16

; Function Attrs: nofree nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local signext range(i8 0, 3) i8 @set_alpha_playfair(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #3
  %3 = icmp ugt i64 %2, 126
  br i1 %3, label %12, label %.preheader

.preheader:                                       ; preds = %1, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader ], [ 1, %1 ]
  %4 = trunc nuw nsw i64 %indvars.iv to i8
  %5 = mul i8 %4, %4
  %6 = icmp ugt i8 %5, 126
  %7 = zext nneg i8 %5 to i64
  %8 = icmp eq i64 %2, %7
  %or.cond = select i1 %6, i1 true, i1 %8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br i1 %or.cond, label %9, label %.preheader, !llvm.loop !5

9:                                                ; preds = %.preheader
  br i1 %6, label %12, label %10

10:                                               ; preds = %9
  store i8 %5, ptr @__length_alpha, align 1, !tbaa !7
  store i8 %4, ptr @__sqrt_length_alpha, align 1, !tbaa !7
  %11 = tail call ptr @strcpy(ptr noundef nonnull dereferenceable(1) @__alpha, ptr noundef nonnull dereferenceable(1) %0) #4
  br label %12

12:                                               ; preds = %10, %9, %1
  %.1 = phi i8 [ 1, %1 ], [ 0, %10 ], [ 2, %9 ]
  ret i8 %.1
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcpy(ptr noalias noundef returned writeonly, ptr noalias nocapture noundef readonly) local_unnamed_addr #2

attributes #0 = { nofree 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" }
attributes #1 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 #3 = { nounwind willreturn memory(read) }
attributes #4 = { 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}


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