#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);
char *rindex(char *, char);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
void parse_uri(char *root, char *uri, int uri_length, char *filename) {
  char *question_mark = index(uri, '?');
  int file_length;
  if (question_mark) {
    file_length = (int)(question_mark - uri);
  } else {
    file_length = uri_length;
  }
  strcpy(filename, root);
  strncat(filename, uri, file_length);
  char *last_dot = rindex(filename, '.');
  if (last_dot == 0 && filename[strlen(filename) - 1] != '/') {
    strcat(filename, "/");
  }
  if (filename[strlen(filename) - 1] == '/') {
    strcat(filename, "index.html");
  }
}


x86 assembly (clang, -O0):
<parse_uri>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3f,%esi
call   1050 <index@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a7 <parse_uri+0x47>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
jmp    11ad <parse_uri+0x4d>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1060 <strncat@plt>
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <rindex@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1221 <parse_uri+0xc1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1221 <parse_uri+0xc1>
mov    -0x20(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    125b <parse_uri+0xfb>
mov    -0x20(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1080 <strcat@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_uri>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%r15
mov    %rsi,%rdi
mov    $0x3f,%esi
call   1050 <index@plt>
mov    %eax,%ecx
sub    %ebp,%ecx
test   %rax,%rax
cmovne %ecx,%r14d
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1060 <strncat@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1070 <rindex@plt>
test   %rax,%rax
jne    11d6 <parse_uri+0x76>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     11d6 <parse_uri+0x76>
lea    0xe32(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    1200 <parse_uri+0xa0>
lea    0xe14(%rip),%rsi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <strcat@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [11 x i8] c"index.html\00"
define void @parse_uri(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%12 = load i8*, i8** %6
%13 = call i8* @index(i8* %12, i8 signext 63)
store i8* %13, i8** %9
%14 = load i8*, i8** %9
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %23
16: ; preds = %4
%17 = load i8*, i8** %9
%18 = load i8*, i8** %6
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %10
br label %25
23: ; preds = %4
%24 = load i32, i32* %7
store i32 %24, i32* %10
br label %25
25: ; preds = %23, %16
%26 = load i8*, i8** %8
%27 = load i8*, i8** %5
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %8
%30 = load i8*, i8** %6
%31 = load i32, i32* %10
%32 = call i32 @strncat(i8* %29, i8* %30, i32 %31)
%33 = load i8*, i8** %8
%34 = call i8* @rindex(i8* %33, i8 signext 46)
store i8* %34, i8** %11
%35 = load i8*, i8** %11
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %50
37: ; preds = %25
%38 = load i8*, i8** %8
%39 = load i8*, i8** %8
%40 = call i32 @strlen(i8* %39)
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %38, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp ne i32 %45, 47
br i1 %46, label %47, label %50
47: ; preds = %37
%48 = load i8*, i8** %8
%49 = call i32 @strcat(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %47, %37, %25
%51 = load i8*, i8** %8
%52 = load i8*, i8** %8
%53 = call i32 @strlen(i8* %52)
%54 = sub nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %51, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 47
br i1 %59, label %60, label %63
60: ; preds = %50
%61 = load i8*, i8** %8
%62 = call i32 @strcat(i8* %61, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %63
63: ; preds = %60, %50
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
@.str.1 = [11 x i8] c"index.html\00"
define void @parse_uri(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = tail call i8* @index(i8* %1, i8 signext 63)
%6 = icmp eq i8* %5, null
%7 = ptrtoint i8* %5 to i64
%8 = ptrtoint i8* %1 to i64
%9 = sub i64 %7, %8
%10 = trunc i64 %9 to i32
%11 = select i1 %6, i32 %2, i32 %10
%12 = tail call i32 @strcpy(i8* %3, i8* %0)
%13 = tail call i32 @strncat(i8* %3, i8* %1, i32 %11)
%14 = tail call i8* @rindex(i8* %3, i8 signext 46)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %25
16: ; preds = %4
%17 = tail call i32 @strlen(i8* %3)
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %3, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 47
br i1 %22, label %25, label %23
23: ; preds = %16
%24 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %16, %4
%26 = tail call i32 @strlen(i8* %3)
%27 = add nsw i32 %26, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %3, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 47
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = tail call i32 @strcat(i8* %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %25
ret void
}