; LLVM IR from abobija/esp-discord :: src/helpers (estr)

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

@estr_url_encode.hex = external hidden global [17 x i8], align 16
@.str = external hidden unnamed_addr constant [5 x 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @estr_url_encode(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %12, label %11

11:                                               ; preds = %1
  store ptr null, ptr %2, align 8
  br label %95

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = call i64 @strlen(ptr noundef %13) #5
  store i64 %14, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %15 = load i64, ptr %4, align 8, !tbaa !9
  %16 = mul i64 %15, 3
  %17 = add i64 %16, 1
  %18 = call noalias ptr @malloc(i64 noundef %17) #6
  store ptr %18, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = icmp ne ptr %19, null
  br i1 %20, label %22, label %21

21:                                               ; preds = %12
  store ptr null, ptr %2, align 8
  store i32 1, ptr %6, align 4
  br label %94

22:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %23, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 0, ptr %8, align 8, !tbaa !9
  br label %24

24:                                               ; preds = %88, %22
  %25 = load i64, ptr %8, align 8, !tbaa !9
  %26 = load i64, ptr %4, align 8, !tbaa !9
  %27 = icmp ult i64 %25, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %24
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %91

29:                                               ; preds = %24
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = load i64, ptr %8, align 8, !tbaa !9
  %32 = getelementptr inbounds i8, ptr %30, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = call zeroext i1 @estr_is_alnum(i8 noundef signext %33)
  br i1 %34, label %43, label %35

35:                                               ; preds = %29
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = load i64, ptr %8, align 8, !tbaa !9
  %38 = getelementptr inbounds i8, ptr %36, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !11
  %40 = sext i8 %39 to i32
  %41 = call ptr @strchr(ptr noundef @.str, i32 noundef %40) #5
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %50

43:                                               ; preds = %35, %29
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = load i64, ptr %8, align 8, !tbaa !9
  %46 = getelementptr inbounds i8, ptr %44, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i32 1
  store ptr %49, ptr %7, align 8, !tbaa !5
  store i8 %47, ptr %48, align 1, !tbaa !11
  br label %87

50:                                               ; preds = %35
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = load i64, ptr %8, align 8, !tbaa !9
  %53 = getelementptr inbounds i8, ptr %51, i64 %52
  %54 = load i8, ptr %53, align 1, !tbaa !11
  %55 = sext i8 %54 to i32
  %56 = icmp eq i32 %55, 32
  br i1 %56, label %57, label %60

57:                                               ; preds = %50
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %7, align 8, !tbaa !5
  store i8 43, ptr %58, align 1, !tbaa !11
  br label %86

60:                                               ; preds = %50
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %61, i32 1
  store ptr %62, ptr %7, align 8, !tbaa !5
  store i8 37, ptr %61, align 1, !tbaa !11
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = load i64, ptr %8, align 8, !tbaa !9
  %65 = getelementptr inbounds i8, ptr %63, i64 %64
  %66 = load i8, ptr %65, align 1, !tbaa !11
  %67 = sext i8 %66 to i32
  %68 = ashr i32 %67, 4
  %69 = and i32 %68, 15
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [17 x i8], ptr @estr_url_encode.hex, i64 0, i64 %70
  %72 = load i8, ptr %71, align 1, !tbaa !11
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = getelementptr inbounds i8, ptr %73, i32 1
  store ptr %74, ptr %7, align 8, !tbaa !5
  store i8 %72, ptr %73, align 1, !tbaa !11
  %75 = load ptr, ptr %3, align 8, !tbaa !5
  %76 = load i64, ptr %8, align 8, !tbaa !9
  %77 = getelementptr inbounds i8, ptr %75, i64 %76
  %78 = load i8, ptr %77, align 1, !tbaa !11
  %79 = sext i8 %78 to i32
  %80 = and i32 %79, 15
  %81 = sext i32 %80 to i64
  %82 = getelementptr inbounds [17 x i8], ptr @estr_url_encode.hex, i64 0, i64 %81
  %83 = load i8, ptr %82, align 1, !tbaa !11
  %84 = load ptr, ptr %7, align 8, !tbaa !5
  %85 = getelementptr inbounds i8, ptr %84, i32 1
  store ptr %85, ptr %7, align 8, !tbaa !5
  store i8 %83, ptr %84, align 1, !tbaa !11
  br label %86

86:                                               ; preds = %60, %57
  br label %87

87:                                               ; preds = %86, %43
  br label %88

88:                                               ; preds = %87
  %89 = load i64, ptr %8, align 8, !tbaa !9
  %90 = add i64 %89, 1
  store i64 %90, ptr %8, align 8, !tbaa !9
  br label %24, !llvm.loop !12

91:                                               ; preds = %28
  %92 = load ptr, ptr %7, align 8, !tbaa !5
  store i8 0, ptr %92, align 1, !tbaa !11
  %93 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %93, ptr %2, align 8
  store i32 1, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  br label %94

94:                                               ; preds = %91, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %95

95:                                               ; preds = %94, %11
  %96 = load ptr, ptr %2, align 8
  ret ptr %96
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @estr_is_alnum(i8 noundef signext) #3

; Function Attrs: nounwind willreturn memory(read)
declare ptr @strchr(ptr noundef, i32 noundef) #1

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 allocsize(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 = { 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 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }
attributes #6 = { nounwind allocsize(0) }

!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"}


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

@estr_url_encode.hex = external hidden local_unnamed_addr global [17 x i8], align 16
@.str = external hidden unnamed_addr constant [5 x 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 allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @estr_url_encode(ptr noundef readonly %0) local_unnamed_addr #2 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %34, label %2

2:                                                ; preds = %1
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #3
  %4 = mul i64 %3, 3
  %5 = add i64 %4, 1
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #4
  %.not32 = icmp eq ptr %6, null
  br i1 %.not32, label %34, label %.preheader

.preheader:                                       ; preds = %2
  %.not36 = icmp eq i64 %3, 0
  br i1 %.not36, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %32, %.preheader
  %.027.lcssa = phi ptr [ %6, %.preheader ], [ %.1, %32 ]
  store i8 0, ptr %.027.lcssa, align 1, !tbaa !5
  br label %34

.lr.ph:                                           ; preds = %.preheader, %32
  %.035 = phi i64 [ %33, %32 ], [ 0, %.preheader ]
  %.02734 = phi ptr [ %.1, %32 ], [ %6, %.preheader ]
  %7 = getelementptr inbounds i8, ptr %0, i64 %.035
  %8 = load i8, ptr %7, align 1, !tbaa !5
  %9 = tail call zeroext i1 @estr_is_alnum(i8 noundef signext %8)
  %.pre = load i8, ptr %7, align 1, !tbaa !5
  br i1 %9, label %13, label %10

10:                                               ; preds = %.lr.ph
  %11 = sext i8 %.pre to i32
  %12 = tail call ptr @strchr(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %11) #3
  %.not33 = icmp eq ptr %12, null
  br i1 %.not33, label %15, label %13

13:                                               ; preds = %10, %.lr.ph
  %14 = getelementptr inbounds i8, ptr %.02734, i64 1
  store i8 %.pre, ptr %.02734, align 1, !tbaa !5
  br label %32

15:                                               ; preds = %10
  %16 = icmp eq i8 %.pre, 32
  %17 = getelementptr inbounds i8, ptr %.02734, i64 1
  br i1 %16, label %18, label %19

18:                                               ; preds = %15
  store i8 43, ptr %.02734, align 1, !tbaa !5
  br label %32

19:                                               ; preds = %15
  store i8 37, ptr %.02734, align 1, !tbaa !5
  %20 = load i8, ptr %7, align 1, !tbaa !5
  %21 = lshr i8 %20, 4
  %22 = zext nneg i8 %21 to i64
  %23 = getelementptr inbounds [17 x i8], ptr @estr_url_encode.hex, i64 0, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %.02734, i64 2
  store i8 %24, ptr %17, align 1, !tbaa !5
  %26 = load i8, ptr %7, align 1, !tbaa !5
  %27 = and i8 %26, 15
  %28 = zext nneg i8 %27 to i64
  %29 = getelementptr inbounds [17 x i8], ptr @estr_url_encode.hex, i64 0, i64 %28
  %30 = load i8, ptr %29, align 1, !tbaa !5
  %31 = getelementptr inbounds i8, ptr %.02734, i64 3
  store i8 %30, ptr %25, align 1, !tbaa !5
  br label %32

32:                                               ; preds = %13, %19, %18
  %.1 = phi ptr [ %14, %13 ], [ %17, %18 ], [ %31, %19 ]
  %33 = add nuw i64 %.035, 1
  %exitcond.not = icmp eq i64 %33, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

34:                                               ; preds = %._crit_edge, %2, %1
  %.129 = phi ptr [ null, %1 ], [ %6, %._crit_edge ], [ null, %2 ]
  ret ptr %.129
}

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @estr_is_alnum(i8 noundef signext) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare ptr @strchr(ptr noundef, i32 noundef) local_unnamed_addr #0

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 allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nounwind willreturn memory(read) }
attributes #4 = { nounwind allocsize(0) }

!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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass