; LLVM IR from NOAA-PMEL/Ferret :: fmt/src (urlencode)

; 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: nounwind uwtable
define dso_local i32 @url_encode_(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %9, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %10, ptr %8, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %80, %3
  %12 = load ptr, ptr %7, align 8, !tbaa !5
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = icmp ne i8 %13, 0
  br i1 %14, label %15, label %83

15:                                               ; preds = %11
  %16 = call ptr @__ctype_b_loc() #5
  %17 = load ptr, ptr %16, align 8, !tbaa !5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = sext i8 %19 to i32
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i16, ptr %17, i64 %21
  %23 = load i16, ptr %22, align 2, !tbaa !10
  %24 = zext i16 %23 to i32
  %25 = and i32 %24, 8
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %47, label %27

27:                                               ; preds = %15
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = load i8, ptr %28, align 1, !tbaa !9
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %30, 45
  br i1 %31, label %47, label %32

32:                                               ; preds = %27
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load i8, ptr %33, align 1, !tbaa !9
  %35 = sext i8 %34 to i32
  %36 = icmp eq i32 %35, 95
  br i1 %36, label %47, label %37

37:                                               ; preds = %32
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = load i8, ptr %38, align 1, !tbaa !9
  %40 = sext i8 %39 to i32
  %41 = icmp eq i32 %40, 46
  br i1 %41, label %47, label %42

42:                                               ; preds = %37
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = load i8, ptr %43, align 1, !tbaa !9
  %45 = sext i8 %44 to i32
  %46 = icmp eq i32 %45, 126
  br i1 %46, label %47, label %52

47:                                               ; preds = %42, %37, %32, %27, %15
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = load i8, ptr %48, align 1, !tbaa !9
  %50 = load ptr, ptr %8, align 8, !tbaa !5
  %51 = getelementptr inbounds i8, ptr %50, i32 1
  store ptr %51, ptr %8, align 8, !tbaa !5
  store i8 %49, ptr %50, align 1, !tbaa !9
  br label %80

52:                                               ; preds = %42
  %53 = load ptr, ptr %7, align 8, !tbaa !5
  %54 = load i8, ptr %53, align 1, !tbaa !9
  %55 = sext i8 %54 to i32
  %56 = icmp eq i32 %55, 32
  br i1 %56, label %57, label %60

57:                                               ; preds = %52
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %8, align 8, !tbaa !5
  store i8 43, ptr %58, align 1, !tbaa !9
  br label %79

60:                                               ; preds = %52
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %61, i32 1
  store ptr %62, ptr %8, align 8, !tbaa !5
  store i8 37, ptr %61, align 1, !tbaa !9
  %63 = load ptr, ptr %7, align 8, !tbaa !5
  %64 = load i8, ptr %63, align 1, !tbaa !9
  %65 = sext i8 %64 to i32
  %66 = ashr i32 %65, 4
  %67 = trunc i32 %66 to i8
  %68 = call signext i8 @to_hex(i8 noundef signext %67)
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = getelementptr inbounds i8, ptr %69, i32 1
  store ptr %70, ptr %8, align 8, !tbaa !5
  store i8 %68, ptr %69, align 1, !tbaa !9
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = load i8, ptr %71, align 1, !tbaa !9
  %73 = sext i8 %72 to i32
  %74 = and i32 %73, 15
  %75 = trunc i32 %74 to i8
  %76 = call signext i8 @to_hex(i8 noundef signext %75)
  %77 = load ptr, ptr %8, align 8, !tbaa !5
  %78 = getelementptr inbounds i8, ptr %77, i32 1
  store ptr %78, ptr %8, align 8, !tbaa !5
  store i8 %76, ptr %77, align 1, !tbaa !9
  br label %79

79:                                               ; preds = %60, %57
  br label %80

80:                                               ; preds = %79, %47
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds i8, ptr %81, i32 1
  store ptr %82, ptr %7, align 8, !tbaa !5
  br label %11, !llvm.loop !12

83:                                               ; preds = %11
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = call i64 @strlen(ptr noundef %84) #6
  %86 = trunc i64 %85 to i32
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %86, ptr %87, align 4, !tbaa !14
  %88 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 0, ptr %88, align 1, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  ret i32 0
}

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

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() #2

; Function Attrs: nounwind uwtable
declare hidden signext i8 @to_hex(i8 noundef signext) #0

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

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

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 = { nounwind willreturn memory(none) "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) "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(none) }
attributes #6 = { 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 = !{!"short", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}


; 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 noundef i32 @url_encode_(ptr nocapture noundef readonly %0, ptr nocapture noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = load i8, ptr %0, align 1, !tbaa !5
  %.not29 = icmp eq i8 %4, 0
  br i1 %.not29, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %5 = tail call ptr @__ctype_b_loc() #3
  br label %6

6:                                                ; preds = %.lr.ph, %27
  %7 = phi i8 [ %4, %.lr.ph ], [ %29, %27 ]
  %.032 = phi ptr [ %1, %.lr.ph ], [ %.1, %27 ]
  %.02130 = phi ptr [ %0, %.lr.ph ], [ %28, %27 ]
  %8 = load ptr, ptr %5, align 8, !tbaa !8
  %9 = sext i8 %7 to i64
  %10 = getelementptr inbounds i16, ptr %8, i64 %9
  %11 = load i16, ptr %10, align 2, !tbaa !10
  %.fr28 = freeze i16 %11
  %12 = and i16 %.fr28, 8
  %.not24.not = icmp eq i16 %12, 0
  br i1 %.not24.not, label %switch.early.test, label %13

switch.early.test:                                ; preds = %6
  switch i8 %7, label %17 [
    i8 126, label %13
    i8 95, label %13
    i8 46, label %13
    i8 45, label %13
    i8 32, label %15
  ]

13:                                               ; preds = %switch.early.test, %switch.early.test, %switch.early.test, %switch.early.test, %6
  %14 = getelementptr inbounds i8, ptr %.032, i64 1
  store i8 %7, ptr %.032, align 1, !tbaa !5
  br label %27

15:                                               ; preds = %switch.early.test
  %16 = getelementptr inbounds i8, ptr %.032, i64 1
  store i8 43, ptr %.032, align 1, !tbaa !5
  br label %27

17:                                               ; preds = %switch.early.test
  %18 = getelementptr inbounds i8, ptr %.032, i64 1
  store i8 37, ptr %.032, align 1, !tbaa !5
  %19 = load i8, ptr %.02130, align 1, !tbaa !5
  %20 = ashr i8 %19, 4
  %21 = tail call signext i8 @to_hex(i8 noundef signext %20)
  %22 = getelementptr inbounds i8, ptr %.032, i64 2
  store i8 %21, ptr %18, align 1, !tbaa !5
  %23 = load i8, ptr %.02130, align 1, !tbaa !5
  %24 = and i8 %23, 15
  %25 = tail call signext i8 @to_hex(i8 noundef signext %24)
  %26 = getelementptr inbounds i8, ptr %.032, i64 3
  store i8 %25, ptr %22, align 1, !tbaa !5
  br label %27

27:                                               ; preds = %15, %17, %13
  %.1 = phi ptr [ %14, %13 ], [ %16, %15 ], [ %26, %17 ]
  %28 = getelementptr inbounds i8, ptr %.02130, i64 1
  %29 = load i8, ptr %28, align 1, !tbaa !5
  %.not = icmp eq i8 %29, 0
  br i1 %.not, label %._crit_edge, label %6, !llvm.loop !12

._crit_edge:                                      ; preds = %27, %3
  %.0.lcssa = phi ptr [ %1, %3 ], [ %.1, %27 ]
  %30 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %1) #4
  %31 = trunc i64 %30 to i32
  store i32 %31, ptr %2, align 4, !tbaa !14
  store i8 0, ptr %.0.lcssa, align 1, !tbaa !5
  ret i32 0
}

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden signext i8 @to_hex(i8 noundef signext) local_unnamed_addr #0

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

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 nofree nosync nounwind willreturn memory(none) "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 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 #3 = { nounwind willreturn memory(none) }
attributes #4 = { 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 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass