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

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

; 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 @vigenere(ptr noundef %0, i8 noundef signext %1, ptr noundef %2, ptr noundef %3) #2 {
  %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 ptr, align 8
  %12 = alloca i64, align 8
  %13 = alloca i32, align 4
  %14 = 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
  %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 %23

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

22:                                               ; preds = %18
  store i8 1, ptr %5, align 1
  br label %64

23:                                               ; preds = %18, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %24 = load ptr, ptr %8, align 8, !tbaa !5
  %25 = call i64 @strlen(ptr noundef %24) #5
  store i64 %25, ptr %10, align 8, !tbaa !10
  %26 = load i64, ptr %10, align 8, !tbaa !10
  %27 = call ptr @llvm.stacksave.p0()
  store ptr %27, ptr %11, align 8
  %28 = alloca i8, i64 %26, align 16
  store i64 %26, ptr %12, align 8
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = call signext i8 @_index_key(ptr noundef %28, ptr noundef %29)
  %31 = icmp ne i8 %30, 0
  br i1 %31, label %32, label %33

32:                                               ; preds = %23
  store i8 2, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %62

33:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %34, ptr %14, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %57, %33
  %36 = load ptr, ptr %14, 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 %41, label %40

40:                                               ; preds = %35
  store i32 2, ptr %13, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  br label %60

41:                                               ; preds = %35
  %42 = load i8, ptr %7, align 1, !tbaa !9
  %43 = load ptr, ptr %14, align 8, !tbaa !5
  %44 = load ptr, ptr %9, align 8, !tbaa !5
  %45 = ptrtoint ptr %43 to i64
  %46 = ptrtoint ptr %44 to i64
  %47 = sub i64 %45, %46
  %48 = load i64, ptr %10, align 8, !tbaa !10
  %49 = urem i64 %47, %48
  %50 = getelementptr inbounds i8, ptr %28, i64 %49
  %51 = load i8, ptr %50, align 1, !tbaa !9
  %52 = load ptr, ptr %14, align 8, !tbaa !5
  %53 = load i8, ptr %52, align 1, !tbaa !9
  %54 = call zeroext i8 @_char_encrypt(i8 noundef signext %42, i8 noundef zeroext %51, i8 noundef zeroext %53)
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds i8, ptr %55, i32 1
  store ptr %56, ptr %6, align 8, !tbaa !5
  store i8 %54, ptr %55, align 1, !tbaa !9
  br label %57

57:                                               ; preds = %41
  %58 = load ptr, ptr %14, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %14, align 8, !tbaa !5
  br label %35, !llvm.loop !12

60:                                               ; preds = %40
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %61, align 1, !tbaa !9
  store i8 0, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %62

62:                                               ; preds = %60, %32
  %63 = load ptr, ptr %11, align 8
  call void @llvm.stackrestore.p0(ptr %63)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %64

64:                                               ; preds = %62, %22
  %65 = load i8, ptr %5, align 1
  ret i8 %65
}

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

; Function Attrs: nounwind uwtable
declare hidden signext i8 @_index_key(ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(i8 noundef signext, i8 noundef zeroext, i8 noundef zeroext) #2

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

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 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 = { nocallback nofree nosync nounwind willreturn }
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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !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"

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

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

5:                                                ; preds = %4, %4
  %6 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %2) #3
  %7 = tail call ptr @llvm.stacksave.p0()
  %8 = alloca i8, i64 %6, align 16
  %9 = call signext i8 @_index_key(ptr noundef nonnull %8, ptr noundef %2)
  %.not = icmp eq i8 %9, 0
  br i1 %.not, label %.preheader, label %23

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

.lr.ph:                                           ; preds = %.preheader
  %11 = ptrtoint ptr %3 to i64
  br label %12

._crit_edge:                                      ; preds = %12, %.preheader
  %.019.lcssa = phi ptr [ %0, %.preheader ], [ %20, %12 ]
  store i8 0, ptr %.019.lcssa, align 1, !tbaa !5
  br label %23

12:                                               ; preds = %.lr.ph, %12
  %13 = phi i8 [ %10, %.lr.ph ], [ %22, %12 ]
  %.023 = phi ptr [ %3, %.lr.ph ], [ %21, %12 ]
  %.01922 = phi ptr [ %0, %.lr.ph ], [ %20, %12 ]
  %14 = ptrtoint ptr %.023 to i64
  %15 = sub i64 %14, %11
  %16 = urem i64 %15, %6
  %17 = getelementptr inbounds i8, ptr %8, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !5
  %19 = call zeroext i8 @_char_encrypt(i8 noundef signext %1, i8 noundef zeroext %18, i8 noundef zeroext %13)
  %20 = getelementptr inbounds i8, ptr %.01922, i64 1
  store i8 %19, ptr %.01922, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %.023, i64 1
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %.not20 = icmp eq i8 %22, 0
  br i1 %.not20, label %._crit_edge, label %12, !llvm.loop !8

23:                                               ; preds = %5, %._crit_edge
  %.018 = phi i8 [ 0, %._crit_edge ], [ 2, %5 ]
  call void @llvm.stackrestore.p0(ptr %7)
  br label %24

24:                                               ; preds = %4, %23
  %.1 = phi i8 [ %.018, %23 ], [ 1, %4 ]
  ret i8 %.1
}

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

; Function Attrs: nounwind uwtable
declare hidden signext i8 @_index_key(ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @_char_encrypt(i8 noundef signext, i8 noundef zeroext, i8 noundef zeroext) local_unnamed_addr #1

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

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 = { 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 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #3 = { 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 = !{!"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