#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 *index(char *, char);
int strlen(char *);
char *rewrite_url(char *url) {
  int length = strlen(url);
  char *pos = "";
  if (length < 1 || url[0] != '/')
    return NULL;
  if (length == 1)
    return "index.html";
  url = &url[1];
  if ((pos = index(url, '?')) != NULL)
    *pos = '\0';
  length = strlen(url);
  if (url[length] == '/')
    url[length] = '\0';
  return url;
}


x86 assembly (clang, -O0):
<rewrite_url>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
lea    0xecb(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     115d <rewrite_url+0x3d>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     116a <rewrite_url+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    11e8 <rewrite_url+0xc8>
cmpl   $0x1,-0x14(%rbp)
jne    1184 <rewrite_url+0x64>
lea    0xe85(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11e8 <rewrite_url+0xc8>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3f,%esi
call   1040 <index@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b3 <rewrite_url+0x93>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11e0 <rewrite_url+0xc0>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite_url>:
push   %rbx
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
jle    1140 <rewrite_url+0x20>
cmpb   $0x2f,(%rbx)
jne    1140 <rewrite_url+0x20>
cmp    $0x1,%eax
jne    1147 <rewrite_url+0x27>
lea    0xec2(%rip),%rbx
jmp    1142 <rewrite_url+0x22>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x3f,%esi
call   1040 <index@plt>
test   %rax,%rax
je     1160 <rewrite_url+0x40>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0x2f,(%rbx,%rax,1)
jne    1142 <rewrite_url+0x22>
movb   $0x0,(%rbx,%rax,1)
jmp    1142 <rewrite_url+0x22>

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

LLVM IR (-O2):
@.str.1 = [11 x i8] c"index.html\00"
define i8* @rewrite_url(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp slt i32 %2, 1
br i1 %3, label %21, label %4
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 47
br i1 %6, label %7, label %21
7: ; preds = %4
%8 = icmp eq i32 %2, 1
br i1 %8, label %21, label %9
9: ; preds = %7
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = tail call i8* @index(i8* %10, i8 signext 63)
%12 = icmp eq i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %9
store i8 0, i8* %11
br label %14
14: ; preds = %13, %9
%15 = tail call i32 @strlen(i8* %10)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %10, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 47
br i1 %19, label %20, label %21
20: ; preds = %14
store i8 0, i8* %17
br label %21
21: ; preds = %14, %20, %7, %1, %4
%22 = phi i8* [ null, %4 ], [ null, %1 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %7 ], [ %10, %20 ], [ %10, %14 ]
ret i8* %22
}