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

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

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

20:                                               ; preds = %16
  store i8 1, ptr %6, align 1
  br label %69

21:                                               ; preds = %16, %5
  %22 = load i8, ptr %9, align 1, !tbaa !9
  %23 = zext i8 %22 to i64
  %24 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %25 = zext i8 %24 to i64
  %26 = call i64 @_gcd(i64 noundef %23, i64 noundef %25)
  %27 = icmp ne i64 %26, 1
  br i1 %27, label %28, label %29

28:                                               ; preds = %21
  store i8 2, ptr %6, align 1
  br label %69

29:                                               ; preds = %21
  %30 = load i8, ptr %8, align 1, !tbaa !9
  %31 = sext i8 %30 to i32
  %32 = icmp eq i32 %31, -1
  br i1 %32, label %33, label %49

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #3
  store i8 1, ptr %12, align 1, !tbaa !9
  br label %34

34:                                               ; preds = %44, %33
  %35 = load i8, ptr %9, align 1, !tbaa !9
  %36 = zext i8 %35 to i32
  %37 = load i8, ptr %12, align 1, !tbaa !9
  %38 = zext i8 %37 to i32
  %39 = mul nsw i32 %36, %38
  %40 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %41 = zext i8 %40 to i32
  %42 = srem i32 %39, %41
  %43 = icmp ne i32 %42, 1
  br i1 %43, label %44, label %47

44:                                               ; preds = %34
  %45 = load i8, ptr %12, align 1, !tbaa !9
  %46 = add i8 %45, 1
  store i8 %46, ptr %12, align 1, !tbaa !9
  br label %34, !llvm.loop !10

47:                                               ; preds = %34
  %48 = load i8, ptr %12, align 1, !tbaa !9
  store i8 %48, ptr %9, align 1, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #3
  br label %49

49:                                               ; preds = %47, %29
  br label %50

50:                                               ; preds = %64, %49
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = load i8, ptr %51, align 1, !tbaa !9
  %53 = zext i8 %52 to i32
  %54 = icmp ne i32 %53, 0
  br i1 %54, label %55, label %67

55:                                               ; preds = %50
  %56 = load i8, ptr %8, align 1, !tbaa !9
  %57 = load ptr, ptr %11, align 8, !tbaa !5
  %58 = load i8, ptr %57, align 1, !tbaa !9
  %59 = load i8, ptr %9, align 1, !tbaa !9
  %60 = load i8, ptr %10, align 1, !tbaa !9
  %61 = call zeroext i8 @_char_encrypt(i8 noundef signext %56, i8 noundef zeroext %58, i8 noundef zeroext %59, i8 noundef zeroext %60)
  %62 = load ptr, ptr %7, align 8, !tbaa !5
  %63 = getelementptr inbounds i8, ptr %62, i32 1
  store ptr %63, ptr %7, align 8, !tbaa !5
  store i8 %61, ptr %62, align 1, !tbaa !9
  br label %64

64:                                               ; preds = %55
  %65 = load ptr, ptr %11, align 8, !tbaa !5
  %66 = getelementptr inbounds i8, ptr %65, i32 1
  store ptr %66, ptr %11, align 8, !tbaa !5
  br label %50, !llvm.loop !12

67:                                               ; preds = %50
  %68 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %68, align 1, !tbaa !9
  store i8 0, ptr %6, align 1
  br label %69

69:                                               ; preds = %67, %28, %20
  %70 = load i8, ptr %6, align 1
  ret i8 %70
}

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @_gcd(i64 noundef, i64 noundef) #2

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


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

6:                                                ; preds = %5, %5
  %7 = zext i8 %2 to i64
  %8 = load i8, ptr @__length_alpha, align 1, !tbaa !5
  %9 = zext i8 %8 to i64
  %10 = tail call i64 @_gcd(i64 noundef %7, i64 noundef %9)
  %.not = icmp eq i64 %10, 1
  br i1 %.not, label %11, label %25

11:                                               ; preds = %6
  %12 = icmp eq i8 %1, -1
  br i1 %12, label %.preheader, label %.loopexit

.preheader:                                       ; preds = %11
  %13 = zext i8 %2 to i16
  %14 = load i8, ptr @__length_alpha, align 1, !tbaa !5
  %.rhs.trunc = zext i8 %14 to i16
  br label %15

15:                                               ; preds = %15, %.preheader
  %.0 = phi i8 [ %18, %15 ], [ 1, %.preheader ]
  %16 = zext i8 %.0 to i16
  %.lhs.trunc = mul nuw i16 %16, %13
  %17 = urem i16 %.lhs.trunc, %.rhs.trunc
  %.not22 = icmp eq i16 %17, 1
  %18 = add i8 %.0, 1
  br i1 %.not22, label %.loopexit, label %15, !llvm.loop !8

.loopexit:                                        ; preds = %15, %11
  %.020 = phi i8 [ %2, %11 ], [ %.0, %15 ]
  %19 = load i8, ptr %4, align 1, !tbaa !5
  %.not2324 = icmp eq i8 %19, 0
  br i1 %.not2324, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.loopexit, %.lr.ph
  %20 = phi i8 [ %24, %.lr.ph ], [ %19, %.loopexit ]
  %.01826 = phi ptr [ %23, %.lr.ph ], [ %4, %.loopexit ]
  %.02125 = phi ptr [ %22, %.lr.ph ], [ %0, %.loopexit ]
  %21 = tail call zeroext i8 @_char_encrypt(i8 noundef signext %1, i8 noundef zeroext %20, i8 noundef zeroext %.020, i8 noundef zeroext %3)
  %22 = getelementptr inbounds i8, ptr %.02125, i64 1
  store i8 %21, ptr %.02125, align 1, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %.01826, i64 1
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %.not23 = icmp eq i8 %24, 0
  br i1 %.not23, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %.lr.ph, %.loopexit
  %.021.lcssa = phi ptr [ %0, %.loopexit ], [ %22, %.lr.ph ]
  store i8 0, ptr %.021.lcssa, align 1, !tbaa !5
  br label %25

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

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @_gcd(i64 noundef, i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(i8 noundef signext, i8 noundef zeroext, i8 noundef zeroext, 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" }
attributes #1 = { inlinehint 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"}
!10 = distinct !{!10, !9}


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