#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ sprintf(char *, char *, char);
scalar_t__ strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
void urlEncode(char *ps) {
  char *pd, *p, str[256];
  pd = str;
  p = ps;
  while (*p) {
    if (strchr(" %&=#+/\\", *p)) {
      sprintf(pd, "%%%02X", *p);
      pd += 3;
      p++;
    } else {
      *pd++ = *p++;
    }
  }
  *pd = '\0';
  strcpy(ps, str);
}


x86 assembly (clang, -O0):
<urlEncode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d7 <urlEncode+0xa7>
mov    -0x18(%rbp),%rax
lea    0xe96(%rip),%rdi
movsbl (%rax),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     11b0 <urlEncode+0x80>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
lea    0xe7e(%rip),%rsi
movsbl (%rax),%edx
call   1050 <sprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11d2 <urlEncode+0xa2>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    1152 <urlEncode+0x22>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1030 <strcpy@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urlEncode>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r14
mov    (%rdi),%al
mov    %rsp,%r13
test   %al,%al
je     119b <urlEncode+0x6b>
lea    0x1(%r14),%rbx
lea    0xea9(%rip),%r15
lea    0xeab(%rip),%r12
jmp    1173 <urlEncode+0x43>
mov    %cl,0x0(%r13)
add    $0x1,%r13
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     119b <urlEncode+0x6b>
movsbl %al,%esi
mov    %r15,%rdi
call   1040 <strchr@plt>
movzbl -0x1(%rbx),%ecx
test   %rax,%rax
je     1160 <urlEncode+0x30>
movsbl %cl,%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <sprintf@plt>
add    $0x3,%r13
jmp    1168 <urlEncode+0x38>
movb   $0x0,0x0(%r13)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [9 x i8] c" %&=#+/\\\00"
@.str.1 = [7 x i8] c"%%%02X\00"
define void @urlEncode(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [256 x i8]
store i8* %0, i8** %2
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
store i8* %6, i8** %3
%7 = load i8*, i8** %2
store i8* %7, i8** %4
br label %8
8: ; preds = %32, %1
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = icmp ne i8 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = call i64 @strchr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8 signext %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 signext %20)
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 3
store i8* %23, i8** %3
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
br label %32
26: ; preds = %12
%27 = load i8*, i8** %4
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %4
%29 = load i8, i8* %27
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %3
store i8 %29, i8* %30
br label %32
32: ; preds = %26, %17
br label %8
33: ; preds = %8
%34 = load i8*, i8** %3
store i8 0, i8* %34
%35 = load i8*, i8** %2
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%37 = call i32 @strcpy(i8* %35, i8* %36)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c" %&=#+/\\\00"
@.str.1 = [7 x i8] c"%%%02X\00"
define void @urlEncode(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %23, label %6
6: ; preds = %1, %18
%7 = phi i8 [ %21, %18 ], [ %4, %1 ]
%8 = phi i8* [ %20, %18 ], [ %0, %1 ]
%9 = phi i8* [ %19, %18 ], [ %3, %1 ]
%10 = tail call i64 @strchr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8 signext %7)
%11 = icmp eq i64 %10, 0
%12 = load i8, i8* %8
br i1 %11, label %16, label %13
13: ; preds = %6
%14 = call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8 signext %12)
%15 = getelementptr inbounds i8, i8* %9, i64 3
br label %18
16: ; preds = %6
%17 = getelementptr inbounds i8, i8* %9, i64 1
store i8 %12, i8* %9
br label %18
18: ; preds = %16, %13
%19 = phi i8* [ %15, %13 ], [ %17, %16 ]
%20 = getelementptr inbounds i8, i8* %8, i64 1
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 0
br i1 %22, label %23, label %6
23: ; preds = %18, %1
%24 = phi i8* [ %3, %1 ], [ %19, %18 ]
store i8 0, i8* %24
%25 = call i32 @strcpy(i8* %0, i8* %3)
ret void
}