#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 */
char *am_malloc(int);
int strlen(char const *);
void *to_hex(char const);
char *url_encode_whitespace(const char *str) {
  const char *pstr = str;
  char *buf = am_malloc(strlen(str) * 3 + 1);
  char *pbuf = buf;
  while (*pstr) {
    if (*pstr == ' ')
      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
    else
      *pbuf++ = *pstr;
    pstr++;
  }
  *pbuf = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<url_encode_whitespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
imul   $0x3,%eax,%edi
add    $0x1,%edi
call   1050 <am_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     120a <url_encode_whitespace+0xda>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    11e2 <url_encode_whitespace+0xb2>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x25,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
sar    $0x4,%eax
movsbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0xf,%eax
movsbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
jmp    11f9 <url_encode_whitespace+0xc9>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1164 <url_encode_whitespace+0x34>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_encode_whitespace>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    (%rax,%rax,2),%edi
add    $0x1,%edi
call   1050 <am_malloc@plt>
mov    %rax,%r14
mov    %rax,%rbx
jmp    117c <url_encode_whitespace+0x4c>
movb   $0x25,(%rbx)
movzbl (%r15),%eax
sar    $0x4,%al
movsbl %al,%edi
call   1030 <to_hex@plt>
mov    %al,0x1(%rbx)
movzbl (%r15),%edi
and    $0xf,%edi
call   1030 <to_hex@plt>
mov    %al,0x2(%rbx)
add    $0x3,%rbx
add    $0x1,%r15
movzbl (%r15),%eax
cmp    $0x20,%al
je     1150 <url_encode_whitespace+0x20>
test   %al,%al
je     1194 <url_encode_whitespace+0x64>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0x1,%r15
jmp    117c <url_encode_whitespace+0x4c>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @url_encode_whitespace(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
store i8* %6, i8** %3
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = mul nsw i32 %8, 3
%10 = add nsw i32 %9, 1
%11 = call i8* @am_malloc(i32 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
store i8* %12, i8** %5
br label %13
13: ; preds = %48, %1
%14 = load i8*, i8** %3
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %51
17: ; preds = %13
%18 = load i8*, i8** %3
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 32
br i1 %21, label %22, label %43
22: ; preds = %17
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
store i8 37, i8* %23
%25 = load i8*, i8** %3
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = ashr i32 %27, 4
%29 = trunc i32 %28 to i8
%30 = call i8* @to_hex(i8 signext %29)
%31 = ptrtoint i8* %30 to i8
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %5
store i8 %31, i8* %32
%34 = load i8*, i8** %3
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = and i32 %36, 15
%38 = trunc i32 %37 to i8
%39 = call i8* @to_hex(i8 signext %38)
%40 = ptrtoint i8* %39 to i8
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
store i8 %40, i8* %41
br label %48
43: ; preds = %17
%44 = load i8*, i8** %3
%45 = load i8, i8* %44
%46 = load i8*, i8** %5
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %5
store i8 %45, i8* %46
br label %48
48: ; preds = %43, %22
%49 = load i8*, i8** %3
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %3
br label %13
51: ; preds = %13
%52 = load i8*, i8** %5
store i8 0, i8* %52
%53 = load i8*, i8** %4
ret i8* %53
}

LLVM IR (-O2):
define i8* @url_encode_whitespace(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = mul nsw i32 %2, 3
%4 = add nsw i32 %3, 1
%5 = tail call i8* @am_malloc(i32 %4)
br label %6
6: ; preds = %26, %1
%7 = phi i8* [ %0, %1 ], [ %28, %26 ]
%8 = phi i8* [ %5, %1 ], [ %27, %26 ]
%9 = load i8, i8* %7
switch i8 %9, label %24 [
i8 0, label %29
i8 32, label %10
]
10: ; preds = %6
%11 = getelementptr inbounds i8, i8* %8, i64 1
store i8 37, i8* %8
%12 = load i8, i8* %7
%13 = ashr i8 %12, 4
%14 = tail call i8* @to_hex(i8 signext %13)
%15 = ptrtoint i8* %14 to i64
%16 = trunc i64 %15 to i8
%17 = getelementptr inbounds i8, i8* %8, i64 2
store i8 %16, i8* %11
%18 = load i8, i8* %7
%19 = and i8 %18, 15
%20 = tail call i8* @to_hex(i8 signext %19)
%21 = ptrtoint i8* %20 to i64
%22 = trunc i64 %21 to i8
%23 = getelementptr inbounds i8, i8* %8, i64 3
store i8 %22, i8* %17
br label %26
24: ; preds = %6
%25 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %9, i8* %8
br label %26
26: ; preds = %24, %10
%27 = phi i8* [ %23, %10 ], [ %25, %24 ]
%28 = getelementptr inbounds i8, i8* %7, i64 1
br label %6
29: ; preds = %6
store i8 0, i8* %8
ret i8* %5
}