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

; 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: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

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

; 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 @hill(ptr noundef %0, i8 noundef signext %1, ptr noundef %2, ptr noundef %3) #3 {
  %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 i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i8, align 1
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i64, align 8
  %16 = alloca i16, align 2
  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
  %17 = load i8, ptr %7, align 1, !tbaa !9
  %18 = sext i8 %17 to i32
  %19 = icmp ne i32 %18, 1
  br i1 %19, label %20, label %25

20:                                               ; preds = %4
  %21 = load i8, ptr %7, align 1, !tbaa !9
  %22 = sext i8 %21 to i32
  %23 = icmp ne i32 %22, -1
  br i1 %23, label %24, label %25

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

25:                                               ; preds = %20, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %26 = load ptr, ptr %8, align 8, !tbaa !5
  %27 = call i64 @strlen(ptr noundef %26) #6
  store i64 %27, ptr %10, align 8, !tbaa !10
  %28 = load i64, ptr %10, align 8, !tbaa !10
  %29 = icmp uge i64 %28, 127
  br i1 %29, label %30, label %31

30:                                               ; preds = %25
  store i8 2, ptr %5, align 1
  store i32 1, ptr %11, align 4
  br label %89

31:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #5
  store i8 1, ptr %12, align 1, !tbaa !9
  br label %32

32:                                               ; preds = %41, %31
  %33 = load i8, ptr %12, align 1, !tbaa !9
  %34 = zext i8 %33 to i32
  %35 = load i8, ptr %12, align 1, !tbaa !9
  %36 = zext i8 %35 to i32
  %37 = mul nsw i32 %34, %36
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %10, align 8, !tbaa !10
  %40 = icmp ult i64 %38, %39
  br i1 %40, label %41, label %44

41:                                               ; preds = %32
  %42 = load i8, ptr %12, align 1, !tbaa !9
  %43 = add i8 %42, 1
  store i8 %43, ptr %12, align 1, !tbaa !9
  br label %32, !llvm.loop !12

44:                                               ; preds = %32
  %45 = load i8, ptr %12, align 1, !tbaa !9
  %46 = zext i8 %45 to i32
  %47 = load i8, ptr %12, align 1, !tbaa !9
  %48 = zext i8 %47 to i32
  %49 = mul nsw i32 %46, %48
  %50 = sext i32 %49 to i64
  %51 = load i64, ptr %10, align 8, !tbaa !10
  %52 = icmp ne i64 %50, %51
  br i1 %52, label %53, label %54

53:                                               ; preds = %44
  store i8 3, ptr %5, align 1
  store i32 1, ptr %11, align 4
  br label %88

54:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = call i64 @strlen(ptr noundef %55) #6
  store i64 %56, ptr %13, align 8, !tbaa !10
  %57 = load i64, ptr %13, align 8, !tbaa !10
  %58 = add i64 %57, 1
  %59 = call ptr @llvm.stacksave.p0()
  store ptr %59, ptr %14, align 8
  %60 = alloca i8, i64 %58, align 16
  store i64 %58, ptr %15, align 8
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = call ptr @strcpy(ptr noundef %60, ptr noundef %61) #5
  call void @_to_index(ptr noundef %60)
  call void @llvm.lifetime.start.p0(i64 2, ptr %16) #5
  %63 = load i8, ptr %12, align 1, !tbaa !9
  %64 = call i32 @_determinant(ptr noundef %60, i8 noundef zeroext %63)
  %65 = trunc i32 %64 to i16
  store i16 %65, ptr %16, align 2, !tbaa !14
  %66 = load i16, ptr %16, align 2, !tbaa !14
  %67 = sext i16 %66 to i32
  %68 = load i8, ptr @__length_alpha, align 1, !tbaa !9
  %69 = zext i8 %68 to i32
  %70 = srem i32 %67, %69
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %73, label %72

72:                                               ; preds = %54
  store i8 4, ptr %5, align 1
  store i32 1, ptr %11, align 4
  br label %86

73:                                               ; preds = %54
  %74 = load i8, ptr %7, align 1, !tbaa !9
  %75 = sext i8 %74 to i32
  switch i32 %75, label %85 [
    i32 1, label %76
    i32 -1, label %80
  ]

76:                                               ; preds = %73
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = load i8, ptr %12, align 1, !tbaa !9
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  call void @_encrypt(ptr noundef %77, i8 noundef zeroext %78, ptr noundef %60, ptr noundef %79)
  br label %85

80:                                               ; preds = %73
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = load i8, ptr %12, align 1, !tbaa !9
  %83 = load i16, ptr %16, align 2, !tbaa !14
  %84 = load ptr, ptr %9, align 8, !tbaa !5
  call void @_decrypt(ptr noundef %81, i8 noundef zeroext %82, i16 noundef signext %83, ptr noundef %60, ptr noundef %84)
  br label %85

85:                                               ; preds = %80, %76, %73
  store i8 0, ptr %5, align 1
  store i32 1, ptr %11, align 4
  br label %86

86:                                               ; preds = %85, %72
  call void @llvm.lifetime.end.p0(i64 2, ptr %16) #5
  %87 = load ptr, ptr %14, align 8
  call void @llvm.stackrestore.p0(ptr %87)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  br label %88

88:                                               ; preds = %86, %53
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #5
  br label %89

89:                                               ; preds = %88, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %90

90:                                               ; preds = %89, %24
  %91 = load i8, ptr %5, align 1
  ret i8 %91
}

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

; Function Attrs: nounwind uwtable
declare hidden void @_to_index(ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden i32 @_determinant(ptr noundef, i8 noundef zeroext) #3

; Function Attrs: nounwind uwtable
declare hidden void @_encrypt(ptr noundef, i8 noundef zeroext, ptr noundef, ptr noundef) #3

; Function Attrs: nounwind uwtable
declare hidden void @_decrypt(ptr noundef, i8 noundef zeroext, i16 noundef signext, ptr noundef, ptr noundef) #3

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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 #4 = { nocallback nofree nosync nounwind willreturn }
attributes #5 = { nounwind }
attributes #6 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"short", !7, i64 0}


; 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: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #0

; 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 #1

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

5:                                                ; preds = %4, %4
  %6 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %2) #4
  %7 = icmp ugt i64 %6, 126
  br i1 %7, label %27, label %.preheader

.preheader:                                       ; preds = %5, %.preheader
  %.027 = phi i8 [ %12, %.preheader ], [ 1, %5 ]
  %8 = zext i8 %.027 to i32
  %9 = mul nuw nsw i32 %8, %8
  %10 = zext nneg i32 %9 to i64
  %11 = icmp ugt i64 %6, %10
  %12 = add i8 %.027, 1
  br i1 %11, label %.preheader, label %13, !llvm.loop !5

13:                                               ; preds = %.preheader
  %.not = icmp eq i64 %6, %10
  br i1 %.not, label %14, label %27

14:                                               ; preds = %13
  %15 = add nuw nsw i64 %6, 1
  %16 = tail call ptr @llvm.stacksave.p0()
  %17 = alloca i8, i64 %15, align 16
  %18 = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %17, ptr noundef nonnull dereferenceable(1) %2) #5
  call void @_to_index(ptr noundef nonnull %17)
  %19 = call i32 @_determinant(ptr noundef nonnull %17, i8 noundef zeroext %.027)
  %20 = trunc i32 %19 to i16
  %21 = load i8, ptr @__length_alpha, align 1, !tbaa !7
  %.rhs.trunc = zext i8 %21 to i16
  %22 = srem i16 %20, %.rhs.trunc
  %.not30 = icmp eq i16 %22, 0
  br i1 %.not30, label %26, label %23

23:                                               ; preds = %14
  switch i8 %1, label %26 [
    i8 1, label %24
    i8 -1, label %25
  ]

24:                                               ; preds = %23
  call void @_encrypt(ptr noundef %0, i8 noundef zeroext %.027, ptr noundef nonnull %17, ptr noundef %3)
  br label %26

25:                                               ; preds = %23
  call void @_decrypt(ptr noundef %0, i8 noundef zeroext %.027, i16 noundef signext %20, ptr noundef nonnull %17, ptr noundef %3)
  br label %26

26:                                               ; preds = %23, %24, %25, %14
  %.0 = phi i8 [ 4, %14 ], [ 0, %25 ], [ 0, %24 ], [ 0, %23 ]
  call void @llvm.stackrestore.p0(ptr %16)
  br label %27

27:                                               ; preds = %5, %13, %26, %4
  %.3 = phi i8 [ 1, %4 ], [ 2, %5 ], [ %.0, %26 ], [ 3, %13 ]
  ret i8 %.3
}

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

; Function Attrs: nounwind uwtable
declare hidden void @_to_index(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden i32 @_determinant(ptr noundef, i8 noundef zeroext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @_encrypt(ptr noundef, i8 noundef zeroext, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @_decrypt(ptr noundef, i8 noundef zeroext, i16 noundef signext, ptr noundef, ptr noundef) local_unnamed_addr #2

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

attributes #0 = { 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 #1 = { 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 #2 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #4 = { nounwind willreturn memory(read) }
attributes #5 = { 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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass