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

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

@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @to_string_indexed(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca ptr, align 8
  %9 = alloca [4 x i8], align 1
  %10 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #5
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = call i64 @_length(i8 noundef zeroext 99, ptr noundef %11, i8 noundef signext -128)
  store i64 %12, ptr %5, align 8, !tbaa !9
  %13 = load i64, ptr %5, align 8, !tbaa !9
  %14 = mul i64 %13, 3
  %15 = add i64 %14, 1
  %16 = call ptr @llvm.stacksave.p0()
  store ptr %16, ptr %6, align 8
  %17 = alloca i8, i64 %15, align 16
  store i64 %15, ptr %7, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  store ptr %17, ptr %8, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %45, %2
  %19 = load ptr, ptr %4, align 8, !tbaa !5
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i32
  %22 = icmp ne i32 %21, -128
  br i1 %22, label %23, label %48

23:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %24 = getelementptr inbounds [4 x i8], ptr %9, i64 0, i64 0
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i8, ptr %25, align 1, !tbaa !11
  %27 = sext i8 %26 to i32
  %28 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef %24, ptr noundef @.str, i32 noundef %27) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %29 = getelementptr inbounds [4 x i8], ptr %9, i64 0, i64 0
  store ptr %29, ptr %10, align 8, !tbaa !5
  br label %30

30:                                               ; preds = %41, %23
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = load i8, ptr %31, align 1, !tbaa !11
  %33 = zext i8 %32 to i32
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %36, label %35

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

36:                                               ; preds = %30
  %37 = load ptr, ptr %10, align 8, !tbaa !5
  %38 = load i8, ptr %37, align 1, !tbaa !11
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %39, i32 1
  store ptr %40, ptr %8, align 8, !tbaa !5
  store i8 %38, ptr %39, align 1, !tbaa !11
  br label %41

41:                                               ; preds = %36
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = getelementptr inbounds i8, ptr %42, i32 1
  store ptr %43, ptr %10, align 8, !tbaa !5
  br label %30, !llvm.loop !12

44:                                               ; preds = %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %45

45:                                               ; preds = %44
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds i8, ptr %46, i32 1
  store ptr %47, ptr %4, align 8, !tbaa !5
  br label %18, !llvm.loop !14

48:                                               ; preds = %18
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = getelementptr inbounds i8, ptr %49, i32 -1
  store ptr %50, ptr %8, align 8, !tbaa !5
  store i8 0, ptr %50, align 1, !tbaa !11
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = call ptr @strcpy(ptr noundef %51, ptr noundef %17) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %53 = load ptr, ptr %6, align 8
  call void @llvm.stackrestore.p0(ptr %53)
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #5
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @_length(i8 noundef zeroext, ptr noundef, i8 noundef signext) #2

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

; Function Attrs: nounwind
declare i32 @sprintf(ptr noundef, ptr noundef, ...) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { 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 #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 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


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

@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local void @to_string_indexed(ptr noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca [4 x i8], align 1
  %4 = tail call i64 @_length(i8 noundef zeroext 99, ptr noundef %1, i8 noundef signext -128)
  %5 = mul i64 %4, 3
  %6 = add i64 %5, 1
  %7 = alloca i8, i64 %6, align 16
  %8 = load i8, ptr %1, align 1, !tbaa !5
  %.not17 = icmp eq i8 %8, -128
  br i1 %.not17, label %._crit_edge22, label %.lr.ph21

.lr.ph21:                                         ; preds = %2, %._crit_edge
  %9 = phi i8 [ %14, %._crit_edge ], [ %8, %2 ]
  %.01119 = phi ptr [ %.1.lcssa, %._crit_edge ], [ %7, %2 ]
  %.01218 = phi ptr [ %13, %._crit_edge ], [ %1, %2 ]
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #5
  %10 = sext i8 %9 to i32
  %11 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef nonnull dereferenceable(1) %3, ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %10) #5
  %12 = load i8, ptr %3, align 1, !tbaa !5
  %.not1314 = icmp eq i8 %12, 0
  br i1 %.not1314, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph21
  %.1.lcssa = phi ptr [ %.01119, %.lr.ph21 ], [ %16, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #5
  %13 = getelementptr inbounds i8, ptr %.01218, i64 1
  %14 = load i8, ptr %13, align 1, !tbaa !5
  %.not = icmp eq i8 %14, -128
  br i1 %.not, label %._crit_edge22, label %.lr.ph21, !llvm.loop !8

.lr.ph:                                           ; preds = %.lr.ph21, %.lr.ph
  %15 = phi i8 [ %18, %.lr.ph ], [ %12, %.lr.ph21 ]
  %.016 = phi ptr [ %17, %.lr.ph ], [ %3, %.lr.ph21 ]
  %.115 = phi ptr [ %16, %.lr.ph ], [ %.01119, %.lr.ph21 ]
  %16 = getelementptr inbounds i8, ptr %.115, i64 1
  store i8 %15, ptr %.115, align 1, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %.016, i64 1
  %18 = load i8, ptr %17, align 1, !tbaa !5
  %.not13 = icmp eq i8 %18, 0
  br i1 %.not13, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge22:                                    ; preds = %._crit_edge, %2
  %.011.lcssa = phi ptr [ %7, %2 ], [ %.1.lcssa, %._crit_edge ]
  %19 = getelementptr inbounds i8, ptr %.011.lcssa, i64 -1
  store i8 0, ptr %19, align 1, !tbaa !5
  %20 = call ptr @strcpy(ptr noundef nonnull dereferenceable(1) %0, ptr noundef nonnull dereferenceable(1) %7) #5
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @_length(i8 noundef zeroext, ptr noundef, i8 noundef signext) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @sprintf(ptr noalias nocapture noundef writeonly, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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

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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nofree 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 #4 = { 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 #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 = !{!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: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass