#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ MSG_NOSIGNAL;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
char *index(char *, char);
scalar_t__ malloc(int);
int /*<<< orphan*/ memmove(char *, char *, int);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *get_file_name(int client_socket) {
  char *buf = (char *)malloc(1024);
  ssize_t num_read = recv(client_socket, buf, 1024 - 1, MSG_NOSIGNAL);
  if (num_read < 3) {
    free(buf);
    return NULL;
  }
  *(buf + num_read) = 0;
  char *space1 = index(buf, ' ');
  while (' ' == *(++space1) && space1 < buf + num_read)
    ;
  while ('/' == *space1 && space1 < buf + num_read)
    ++space1;
  if (space1 != NULL && (space1 - buf) + 3 < num_read) {
    char *space2 = index(space1, ' ');
    char *special = index(space1, '?');
    if (special != NULL && special < space2 || space2 == NULL)
      space2 = special;
    if (space2 != NULL) {
      *space2 = 0;
      memmove(buf, space1, space2 - space1 + 1);
      fprintf(stderr, "Requested file: %s\n", buf);
      return buf;
    }
  }
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<get_file_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
mov    $0x3ff,%edx
call   1040 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jge    11b6 <get_file_name+0x56>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1360 <get_file_name+0x200>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    $0x20,%esi
call   1050 <index@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
movsbl 0x1(%rax),%edx
xor    %eax,%eax
mov    $0x20,%ecx
cmp    %edx,%ecx
mov    %al,-0x39(%rbp)
jne    1211 <get_file_name+0xb1>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    1221 <get_file_name+0xc1>
jmp    1226 <get_file_name+0xc6>
jmp    11d4 <get_file_name+0x74>
jmp    122b <get_file_name+0xcb>
mov    -0x28(%rbp),%rax
movsbl (%rax),%edx
xor    %eax,%eax
mov    $0x2f,%ecx
cmp    %edx,%ecx
mov    %al,-0x3a(%rbp)
jne    125c <get_file_name+0xfc>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
test   $0x1,%al
jne    126c <get_file_name+0x10c>
jmp    127d <get_file_name+0x11d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    122b <get_file_name+0xcb>
cmpq   $0x0,-0x28(%rbp)
je     134f <get_file_name+0x1ef>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    $0x3,%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jge    134f <get_file_name+0x1ef>
mov    -0x28(%rbp),%rdi
mov    $0x20,%esi
call   1050 <index@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x3f,%esi
call   1050 <index@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12e1 <get_file_name+0x181>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     12ec <get_file_name+0x18c>
cmpq   $0x0,-0x30(%rbp)
jne    12f4 <get_file_name+0x194>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     134a <get_file_name+0x1ea>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%edx
call   1080 <memmove@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xcc8(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1360 <get_file_name+0x200>
jmp    134f <get_file_name+0x1ef>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    0x2e5a(%rip),%rax
mov    (%rax),%ecx
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1040 <recv@plt>
cmp    $0x2,%eax
jle    127a <get_file_name+0x11a>
mov    %eax,%r15d
lea    (%r14,%r15,1),%rbx
movb   $0x0,(%r14,%r15,1)
mov    %r14,%rdi
mov    $0x20,%esi
call   1050 <index@plt>
add    $0x1,%rax
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%rax),%edx
lea    0x1(%rax),%rcx
cmp    $0x20,%dl
jne    11d4 <get_file_name+0x74>
cmp    %rbx,%rax
mov    %rcx,%rax
jb     11c0 <get_file_name+0x60>
lea    -0x1(%rcx),%r12
cmp    %rbx,%r12
jae    1205 <get_file_name+0xa5>
cmp    $0x2f,%dl
jne    1205 <get_file_name+0xa5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0x1(%rcx),%r12
cmp    %rbx,%rcx
jae    1201 <get_file_name+0xa1>
cmpb   $0x2f,(%rcx)
mov    %r12,%rcx
je     11f0 <get_file_name+0x90>
add    $0xffffffffffffffff,%r12
mov    %r12,%rax
sub    %r14,%rax
add    $0x3,%rax
cmp    %r15,%rax
jge    127a <get_file_name+0x11a>
mov    %r12,%rdi
mov    $0x20,%esi
call   1050 <index@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    $0x3f,%esi
call   1050 <index@plt>
test   %rax,%rax
je     1242 <get_file_name+0xe2>
test   %r15,%r15
je     124a <get_file_name+0xea>
cmp    %r15,%rax
jae    1247 <get_file_name+0xe7>
jmp    124a <get_file_name+0xea>
test   %r15,%r15
je     127a <get_file_name+0x11a>
mov    %r15,%rax
movb   $0x0,(%rax)
sub    %r12d,%eax
add    $0x1,%eax
mov    %r14,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1080 <memmove@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
jmp    1285 <get_file_name+0x125>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Requested file: %s\0A\00"
define i8* @get_file_name(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 %0, i32* %3
%9 = call i64 @malloc(i32 1024)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %4
%11 = load i32, i32* %3
%12 = load i8*, i8** %4
%13 = load i32, i32* @MSG_NOSIGNAL
%14 = call i32 @recv(i32 %11, i8* %12, i32 1023, i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i8*, i8** %4
%19 = call i32 @free(i8* %18)
store i8* null, i8** %2
br label %113
20: ; preds = %1
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
%25 = load i8*, i8** %4
%26 = call i8* @index(i8* %25, i8 signext 32)
store i8* %26, i8** %6
br label %27
27: ; preds = %42, %20
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 32, %31
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = load i8*, i8** %6
%35 = load i8*, i8** %4
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
%39 = icmp ult i8* %34, %38
br label %40
40: ; preds = %33, %27
%41 = phi i1 [ false, %27 ], [ %39, %33 ]
br i1 %41, label %42, label %43
42: ; preds = %40
br label %27
43: ; preds = %40
br label %44
44: ; preds = %58, %43
%45 = load i8*, i8** %6
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 47, %47
br i1 %48, label %49, label %56
49: ; preds = %44
%50 = load i8*, i8** %6
%51 = load i8*, i8** %4
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = icmp ult i8* %50, %54
br label %56
56: ; preds = %49, %44
%57 = phi i1 [ false, %44 ], [ %55, %49 ]
br i1 %57, label %58, label %61
58: ; preds = %56
%59 = load i8*, i8** %6
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %6
br label %44
61: ; preds = %56
%62 = load i8*, i8** %6
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %110
64: ; preds = %61
%65 = load i8*, i8** %6
%66 = load i8*, i8** %4
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
%70 = add nsw i64 %69, 3
%71 = load i32, i32* %5
%72 = sext i32 %71 to i64
%73 = icmp slt i64 %70, %72
br i1 %73, label %74, label %110
74: ; preds = %64
%75 = load i8*, i8** %6
%76 = call i8* @index(i8* %75, i8 signext 32)
store i8* %76, i8** %7
%77 = load i8*, i8** %6
%78 = call i8* @index(i8* %77, i8 signext 63)
store i8* %78, i8** %8
%79 = load i8*, i8** %8
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %85
81: ; preds = %74
%82 = load i8*, i8** %8
%83 = load i8*, i8** %7
%84 = icmp ult i8* %82, %83
br i1 %84, label %88, label %85
85: ; preds = %81, %74
%86 = load i8*, i8** %7
%87 = icmp eq i8* %86, null
br i1 %87, label %88, label %90
88: ; preds = %85, %81
%89 = load i8*, i8** %8
store i8* %89, i8** %7
br label %90
90: ; preds = %88, %85
%91 = load i8*, i8** %7
%92 = icmp ne i8* %91, null
br i1 %92, label %93, label %109
93: ; preds = %90
%94 = load i8*, i8** %7
store i8 0, i8* %94
%95 = load i8*, i8** %4
%96 = load i8*, i8** %6
%97 = load i8*, i8** %7
%98 = load i8*, i8** %6
%99 = ptrtoint i8* %97 to i64
%100 = ptrtoint i8* %98 to i64
%101 = sub i64 %99, %100
%102 = add nsw i64 %101, 1
%103 = trunc i64 %102 to i32
%104 = call i32 @memmove(i8* %95, i8* %96, i32 %103)
%105 = load i32, i32* @stderr
%106 = load i8*, i8** %4
%107 = call i32 @fprintf(i32 %105, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %106)
%108 = load i8*, i8** %4
store i8* %108, i8** %2
br label %113
109: ; preds = %90
br label %110
110: ; preds = %109, %64, %61
%111 = load i8*, i8** %4
%112 = call i32 @free(i8* %111)
store i8* null, i8** %2
br label %113
113: ; preds = %110, %93, %17
%114 = load i8*, i8** %2
ret i8* %114
}

LLVM IR (-O2):
@MSG_NOSIGNAL = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Requested file: %s\0A\00"
define i8* @get_file_name(i32 %0) {
%2 = tail call i64 @malloc(i32 1024)
%3 = inttoptr i64 %2 to i8*
%4 = load i32, i32* @MSG_NOSIGNAL
%5 = tail call i32 @recv(i32 %0, i8* %3, i32 1023, i32 %4)
%6 = icmp slt i32 %5, 3
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @free(i8* %3)
br label %61
9: ; preds = %1
%10 = zext i32 %5 to i64
%11 = getelementptr inbounds i8, i8* %3, i64 %10
store i8 0, i8* %11
%12 = tail call i8* @index(i8* %3, i8 signext 32)
br label %13
13: ; preds = %13, %9
%14 = phi i8* [ %12, %9 ], [ %15, %13 ]
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 32
%18 = icmp ult i8* %15, %11
%19 = and i1 %17, %18
br i1 %19, label %13, label %20
20: ; preds = %13
%21 = icmp eq i8 %16, 47
%22 = icmp ult i8* %15, %11
%23 = and i1 %22, %21
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = getelementptr inbounds i8, i8* %14, i64 2
br label %26
26: ; preds = %24, %26
%27 = phi i8* [ %25, %24 ], [ %32, %26 ]
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 47
%30 = icmp ult i8* %27, %11
%31 = and i1 %30, %29
%32 = getelementptr inbounds i8, i8* %27, i64 1
br i1 %31, label %26, label %33
33: ; preds = %26, %20
%34 = phi i8* [ %15, %20 ], [ %27, %26 ]
%35 = ptrtoint i8* %34 to i64
%36 = sub i64 3, %2
%37 = add i64 %36, %35
%38 = icmp slt i64 %37, %10
br i1 %38, label %39, label %59
39: ; preds = %33
%40 = tail call i8* @index(i8* %34, i8 signext 32)
%41 = tail call i8* @index(i8* %34, i8 signext 63)
%42 = icmp eq i8* %41, null
br i1 %42, label %47, label %43
43: ; preds = %39
%44 = icmp ult i8* %41, %40
%45 = icmp eq i8* %40, null
%46 = or i1 %45, %44
br i1 %46, label %50, label %49
47: ; preds = %39
%48 = icmp eq i8* %40, null
br i1 %48, label %59, label %49
49: ; preds = %43, %47
br label %50
50: ; preds = %43, %49
%51 = phi i8* [ %40, %49 ], [ %41, %43 ]
store i8 0, i8* %51
%52 = ptrtoint i8* %51 to i64
%53 = sub i64 %52, %35
%54 = trunc i64 %53 to i32
%55 = add i32 %54, 1
%56 = tail call i32 @memmove(i8* %3, i8* %34, i32 %55)
%57 = load i32, i32* @stderr
%58 = tail call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %3)
br label %61
59: ; preds = %47, %33
%60 = tail call i32 @free(i8* %3)
br label %61
61: ; preds = %50, %59, %7
%62 = phi i8* [ null, %7 ], [ null, %59 ], [ %3, %50 ]
ret i8* %62
}