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

; 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 @vernam(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 %69

23:                                               ; preds = %18, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = call i64 @strlen(ptr noundef %24) #5
  store i64 %25, ptr %10, align 8, !tbaa !10
  %26 = load i8, ptr %7, align 1, !tbaa !9
  %27 = sext i8 %26 to i32
  %28 = icmp eq i32 %27, 1
  br i1 %28, label %29, label %32

29:                                               ; preds = %23
  %30 = load ptr, ptr %8, align 8, !tbaa !5
  %31 = load i64, ptr %10, align 8, !tbaa !10
  call void @_generate_random_key(ptr noundef %30, i64 noundef %31)
  br label %32

32:                                               ; preds = %29, %23
  %33 = load i64, ptr %10, align 8, !tbaa !10
  %34 = call ptr @llvm.stacksave.p0()
  store ptr %34, ptr %11, align 8
  %35 = alloca i8, i64 %33, align 16
  store i64 %33, ptr %12, align 8
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = call signext i8 @_index_key(ptr noundef %35, ptr noundef %36)
  %38 = icmp ne i8 %37, 0
  br i1 %38, label %39, label %40

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

40:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %41, ptr %14, align 8, !tbaa !5
  br label %42

42:                                               ; preds = %62, %40
  %43 = load ptr, ptr %14, align 8, !tbaa !5
  %44 = load i8, ptr %43, align 1, !tbaa !9
  %45 = zext i8 %44 to i32
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %48, label %47

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

48:                                               ; preds = %42
  %49 = load i8, ptr %7, align 1, !tbaa !9
  %50 = load ptr, ptr %14, align 8, !tbaa !5
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = ptrtoint ptr %50 to i64
  %53 = ptrtoint ptr %51 to i64
  %54 = sub i64 %52, %53
  %55 = getelementptr inbounds i8, ptr %35, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !9
  %57 = load ptr, ptr %14, align 8, !tbaa !5
  %58 = load i8, ptr %57, align 1, !tbaa !9
  %59 = call zeroext i8 @_char_encrypt(i8 noundef signext %49, i8 noundef zeroext %56, i8 noundef zeroext %58)
  %60 = load ptr, ptr %6, align 8, !tbaa !5
  %61 = getelementptr inbounds i8, ptr %60, i32 1
  store ptr %61, ptr %6, align 8, !tbaa !5
  store i8 %59, ptr %60, align 1, !tbaa !9
  br label %62

62:                                               ; preds = %48
  %63 = load ptr, ptr %14, align 8, !tbaa !5
  %64 = getelementptr inbounds i8, ptr %63, i32 1
  store ptr %64, ptr %14, align 8, !tbaa !5
  br label %42, !llvm.loop !12

65:                                               ; preds = %47
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  store i8 0, ptr %66, align 1, !tbaa !9
  store i8 0, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %67

67:                                               ; preds = %65, %39
  %68 = load ptr, ptr %11, align 8
  call void @llvm.stackrestore.p0(ptr %68)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %69

69:                                               ; preds = %67, %22
  %70 = load i8, ptr %5, align 1
  ret i8 %70
}

; Function Attrs: nounwind uwtable
declare hidden void @_generate_random_key(ptr noundef, i64 noundef) #2

; 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 @vernam(ptr nocapture noundef writeonly %0, i8 noundef signext %1, ptr noundef %2, ptr noundef %3) local_unnamed_addr #1 {
  switch i8 %1, label %26 [
    i8 -1, label %5
    i8 1, label %5
  ]

5:                                                ; preds = %4, %4
  %6 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %3) #3
  %7 = icmp eq i8 %1, 1
  br i1 %7, label %8, label %9

8:                                                ; preds = %5
  tail call void @_generate_random_key(ptr noundef %2, i64 noundef %6)
  br label %9

9:                                                ; preds = %8, %5
  %10 = tail call ptr @llvm.stacksave.p0()
  %11 = alloca i8, i64 %6, align 16
  %12 = call signext i8 @_index_key(ptr noundef nonnull %11, ptr noundef %2)
  %.not = icmp eq i8 %12, 0
  br i1 %.not, label %.preheader, label %25

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

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

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

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

25:                                               ; preds = %9, %._crit_edge
  %.020 = phi i8 [ 0, %._crit_edge ], [ 2, %9 ]
  call void @llvm.stackrestore.p0(ptr %10)
  br label %26

26:                                               ; preds = %4, %25
  %.1 = phi i8 [ %.020, %25 ], [ 1, %4 ]
  ret i8 %.1
}

; Function Attrs: nounwind uwtable
declare hidden void @_generate_random_key(ptr noundef, i64 noundef) local_unnamed_addr #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