#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 *malloc(int);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
char *rewrite_url(char *url) {
  if (strcmp(url, "/") == 0 || strcmp(url, "") == 0)
    return "/index.html";
  int length = strlen(url);
  char *res = malloc(length);
  int i = 0;
  while (url[i] != '?' && i < length) {
    res[i] = url[i];
    i++;
  }
  return res;
}


x86 assembly (clang, -O0):
<rewrite_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb9(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     1170 <rewrite_url+0x40>
mov    -0x10(%rbp),%rdi
lea    0xea0(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1180 <rewrite_url+0x50>
lea    0xe8b(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1201 <rewrite_url+0xd1>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x3f,%ecx
mov    %al,-0x25(%rbp)
je     11c5 <rewrite_url+0x95>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11d5 <rewrite_url+0xa5>
jmp    11f9 <rewrite_url+0xc9>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119f <rewrite_url+0x6f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite_url>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xec2(%rip),%rsi
call   1040 <strcmp@plt>
lea    0xeb8(%rip),%r14
test   %rax,%rax
je     11a9 <rewrite_url+0x79>
lea    0xeab(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11a9 <rewrite_url+0x79>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    (%rbx),%al
cmp    $0x3f,%al
je     11a9 <rewrite_url+0x79>
test   %ebp,%ebp
jle    11a9 <rewrite_url+0x79>
mov    %ebp,%ecx
mov    $0x1,%edx
nopl   0x0(%rax,%rax,1)
mov    %al,-0x1(%r14,%rdx,1)
movzbl (%rbx,%rdx,1),%eax
cmp    $0x3f,%al
je     11a9 <rewrite_url+0x79>
lea    0x1(%rdx),%rsi
cmp    %rcx,%rdx
mov    %rsi,%rdx
jb     1190 <rewrite_url+0x60>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [12 x i8] c"/index.html\00"
define i8* @rewrite_url(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10, %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %48
15: ; preds = %10
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = call i8* @malloc(i32 %18)
store i8* %19, i8** %5
store i32 0, i32* %6
br label %20
20: ; preds = %34, %15
%21 = load i8*, i8** %3
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 63
br i1 %27, label %28, label %32
28: ; preds = %20
%29 = load i32, i32* %6
%30 = load i32, i32* %4
%31 = icmp slt i32 %29, %30
br label %32
32: ; preds = %28, %20
%33 = phi i1 [ false, %20 ], [ %31, %28 ]
br i1 %33, label %34, label %46
34: ; preds = %32
%35 = load i8*, i8** %3
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %5
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 %39, i8* %43
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %20
46: ; preds = %32
%47 = load i8*, i8** %5
store i8* %47, i8** %2
br label %48
48: ; preds = %46, %14
%49 = load i8*, i8** %2
ret i8* %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [12 x i8] c"/index.html\00"
define i8* @rewrite_url(i8* %0) {
%2 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i64 %2, 0
br i1 %3, label %26, label %4
4: ; preds = %1
%5 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %26, label %7
7: ; preds = %4
%8 = tail call i32 @strlen(i8* %0)
%9 = tail call i8* @malloc(i32 %8)
%10 = load i8, i8* %0
%11 = icmp ne i8 %10, 63
%12 = icmp sgt i32 %8, 0
%13 = and i1 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %7
%15 = zext i32 %8 to i64
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %20, %16 ]
%18 = phi i8 [ %10, %14 ], [ %22, %16 ]
%19 = getelementptr inbounds i8, i8* %9, i64 %17
store i8 %18, i8* %19
%20 = add nuw nsw i64 %17, 1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 63
%24 = icmp ult i64 %20, %15
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %16, label %26
26: ; preds = %16, %7, %1, %4
%27 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %4 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %1 ], [ %9, %7 ], [ %9, %16 ]
ret i8* %27
}