#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int strlen(char *);
char *strstr(char const *, char *);
char *get_filename(const char *str) {
  char *str_start = strstr(str, "GET /");
  if (str_start != NULL) {
    char *filename = str_start + strlen("GET /");
    char *str_end = strchr(filename, ' ');
    if (str_end != NULL) {
      *str_end = '\0';
    } else {
      fprintf(stderr, "Invalid request!, 'space' not found\n");
      return NULL;
    }
    printf("Filename:'%s'\n", filename);
    return filename;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<get_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1207 <get_filename+0xb7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe76(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x20,%esi
call   1040 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c8 <get_filename+0x78>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
jmp    11ea <get_filename+0x9a>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    120f <get_filename+0xbf>
mov    -0x20(%rbp),%rsi
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120f <get_filename+0xbf>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filename>:
push   %rbx
lea    0xea8(%rip),%rsi
call   1070 <strstr@plt>
test   %rax,%rax
je     11b1 <get_filename+0x61>
mov    %rax,%rbx
lea    0xe94(%rip),%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     119c <get_filename+0x4c>
movb   $0x0,(%rax)
lea    0xe99(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
jmp    11b3 <get_filename+0x63>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"GET /\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Invalid request!, 'space' not found\0A\00"
@.str.2 = [15 x i8] c"Filename:'%s'\0A\00"
define i8* @get_filename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i8* @strstr(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %29
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = call i8* @strchr(i8* %16, i8 signext 32)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = load i8*, i8** %6
store i8 0, i8* %21
br label %25
22: ; preds = %11
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %30
25: ; preds = %20
%26 = load i8*, i8** %5
%27 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = load i8*, i8** %5
store i8* %28, i8** %2
br label %30
29: ; preds = %1
store i8* null, i8** %2
br label %30
30: ; preds = %29, %25, %22
%31 = load i8*, i8** %2
ret i8* %31
}

LLVM IR (-O2):
@.str = [6 x i8] c"GET /\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"Invalid request!, 'space' not found\0A\00"
@.str.2 = [15 x i8] c"Filename:'%s'\0A\00"
define i8* @get_filename(i8* %0) {
%2 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %2, i64 %6
%8 = tail call i8* @strchr(i8* %7, i8 signext 32)
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %4
store i8 0, i8* %8
%11 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %7)
br label %15
12: ; preds = %4
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %1, %12, %10
%16 = phi i8* [ %7, %10 ], [ null, %12 ], [ null, %1 ]
ret i8* %16
}