#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 strlen(char *);
char *urlify(char *str, int len_true) {
  int write_pos = strlen(str) - 1;
  for (int i = len_true - 1; i >= 0; i--) {
    if (str[i] == ' ') {
      str[write_pos] = '0';
      str[write_pos - 1] = '2';
      str[write_pos - 2] = '%';
      write_pos -= 3;
    } else {
      str[write_pos] = str[i];
      write_pos--;
    }
  }
  return str;
}


x86 assembly (clang, -O0):
<urlify>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11c4 <urlify+0xb4>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jne    1192 <urlify+0x82>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x30,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x32,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x25,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x10(%rbp)
jmp    11b1 <urlify+0xa1>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11b6 <urlify+0xa6>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1137 <urlify+0x27>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<urlify>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%rbp
call   1030 <strlen@plt>
test   %ebx,%ebx
jle    1168 <urlify+0x58>
add    $0xffffffff,%eax
mov    %ebx,%ecx
add    $0x1,%rcx
jmp    1145 <urlify+0x35>
nopl   0x0(%rax)
mov    %bl,0x0(%rbp,%rdx,1)
mov    $0xffffffff,%edx
add    %edx,%eax
add    $0xffffffffffffffff,%rcx
cmp    $0x1,%rcx
jbe    1168 <urlify+0x58>
lea    -0x2(%rcx),%edx
movzbl 0x0(%rbp,%rdx,1),%ebx
movslq %eax,%rdx
cmp    $0x20,%bl
jne    1130 <urlify+0x20>
movb   $0x30,0x0(%rbp,%rdx,1)
movw   $0x3225,-0x2(%rbp,%rdx,1)
mov    $0xfffffffd,%edx
jmp    1139 <urlify+0x29>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @urlify(i8* returned %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = add nsw i32 %3, -1
%7 = zext i32 %1 to i64
br label %9
8: ; preds = %27, %2
ret i8* %0
9: ; preds = %5, %27
%10 = phi i64 [ %7, %5 ], [ %12, %27 ]
%11 = phi i32 [ %6, %5 ], [ %29, %27 ]
%12 = add nsw i64 %10, -1
%13 = and i64 %12, 4294967295
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 32
%17 = sext i32 %11 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
br i1 %16, label %19, label %26
19: ; preds = %9
store i8 48, i8* %18
%20 = add nsw i32 %11, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 50, i8* %22
%23 = add nsw i32 %11, -2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 37, i8* %25
br label %27
26: ; preds = %9
store i8 %15, i8* %18
br label %27
27: ; preds = %19, %26
%28 = phi i32 [ -3, %19 ], [ -1, %26 ]
%29 = add nsw i32 %11, %28
%30 = icmp ugt i64 %10, 1
br i1 %30, label %9, label %8
}