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

; 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"

; 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 @trithemius(ptr noundef %0, i8 noundef signext %1, ptr noundef %2, ptr noundef %3) #1 {
  %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
  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
  %11 = load i8, ptr %7, align 1, !tbaa !9
  %12 = sext i8 %11 to i32
  %13 = icmp ne i32 %12, 1
  br i1 %13, label %14, label %19

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

18:                                               ; preds = %14
  store i8 1, ptr %5, align 1
  br label %47

19:                                               ; preds = %14, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %20, ptr %10, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %42, %19
  %22 = load ptr, ptr %10, align 8, !tbaa !5
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %24 = zext i8 %23 to i32
  %25 = icmp ne i32 %24, 0
  br i1 %25, label %27, label %26

26:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %45

27:                                               ; preds = %21
  %28 = load i8, ptr %7, align 1, !tbaa !9
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = load ptr, ptr %10, align 8, !tbaa !5
  %31 = load ptr, ptr %9, align 8, !tbaa !5
  %32 = ptrtoint ptr %30 to i64
  %33 = ptrtoint ptr %31 to i64
  %34 = sub i64 %32, %33
  %35 = trunc i64 %34 to i8
  %36 = call signext i8 %29(i8 noundef signext %35)
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = load i8, ptr %37, align 1, !tbaa !9
  %39 = call zeroext i8 @_char_encrypt(i8 noundef signext %28, i8 noundef signext %36, i8 noundef zeroext %38)
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds i8, ptr %40, i32 1
  store ptr %41, ptr %6, align 8, !tbaa !5
  store i8 %39, ptr %40, align 1, !tbaa !9
  br label %42

42:                                               ; preds = %27
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = getelementptr inbounds i8, ptr %43, i32 1
  store ptr %44, ptr %10, align 8, !tbaa !5
  br label %21, !llvm.loop !10

45:                                               ; preds = %26
  %46 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %46, align 1, !tbaa !9
  store i8 0, ptr %5, align 1
  br label %47

47:                                               ; preds = %45, %18
  %48 = load i8, ptr %5, align 1
  ret i8 %48
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(i8 noundef signext, i8 noundef signext, 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 = { 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"}


; 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"

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

5:                                                ; preds = %4, %4
  %6 = load i8, ptr %3, align 1, !tbaa !5
  %.not16 = icmp eq i8 %6, 0
  br i1 %.not16, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %7 = ptrtoint ptr %3 to i64
  br label %8

._crit_edge:                                      ; preds = %8, %5
  %.015.lcssa = phi ptr [ %0, %5 ], [ %15, %8 ]
  store i8 0, ptr %.015.lcssa, align 1, !tbaa !5
  br label %18

8:                                                ; preds = %.lr.ph, %8
  %.018 = phi ptr [ %3, %.lr.ph ], [ %16, %8 ]
  %.01517 = phi ptr [ %0, %.lr.ph ], [ %15, %8 ]
  %9 = ptrtoint ptr %.018 to i64
  %10 = sub i64 %9, %7
  %11 = trunc i64 %10 to i8
  %12 = tail call signext i8 %2(i8 noundef signext %11) #1
  %13 = load i8, ptr %.018, align 1, !tbaa !5
  %14 = tail call zeroext i8 @_char_encrypt(i8 noundef signext %1, i8 noundef signext %12, i8 noundef zeroext %13)
  %15 = getelementptr inbounds i8, ptr %.01517, i64 1
  store i8 %14, ptr %.01517, align 1, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %.018, i64 1
  %17 = load i8, ptr %16, align 1, !tbaa !5
  %.not = icmp eq i8 %17, 0
  br i1 %.not, label %._crit_edge, label %8, !llvm.loop !8

18:                                               ; preds = %4, %._crit_edge
  %.014 = phi i8 [ 0, %._crit_edge ], [ 1, %4 ]
  ret i8 %.014
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(i8 noundef signext, i8 noundef signext, 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 = { 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 = !{!"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,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass