#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*/ MSG_NOSIGNAL;
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
char *strtok(char *, char *);
char *parse_request(int socket, char *buffer) {
  if (recv(socket, buffer, 1024, NULL) > 0) {
    char *req_word = strtok(buffer, " ");
    char *file_to_send;
    if (strncmp(req_word, "GET", 3) == 0) {
      req_word = strtok(NULL, " ");
      if (strcmp(req_word, "/") == 0) {
        file_to_send = malloc((strlen("index.html") + 1) * sizeof(char));
        strcpy(file_to_send, "index.html");
        printf("Poziadavka na subor '%s'\n", "index.htm");
      } else {
        req_word++;
        file_to_send = malloc((strlen(req_word) + 1) * sizeof(char));
        strcpy(file_to_send, req_word);
        printf("Poziadavka na subor '%s'\n", file_to_send);
      }
      return file_to_send;
    } else {
      send(socket, "HTTP/1.1 400 BAD REQUEST\n\n", 26, MSG_NOSIGNAL);
      return "";
    }
  } else
    return "";
}


x86 assembly (clang, -O0):
<parse_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jle    12e7 <parse_request+0x157>
mov    -0x18(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe25(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12ba <parse_request+0x12a>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe04(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    1267 <parse_request+0xd7>
lea    0xde2(%rip),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdbe(%rip),%rsi
call   1050 <strcpy@plt>
lea    0xdbd(%rip),%rdi
lea    0xdd0(%rip),%rsi
call   1080 <printf@plt>
jmp    12ad <parse_request+0x11d>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x28(%rbp),%rsi
lea    0xd6b(%rip),%rdi
call   1080 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f2 <parse_request+0x162>
mov    -0xc(%rbp),%edi
mov    0x2d1c(%rip),%rax
mov    (%rax),%ecx
lea    0xd6a(%rip),%rsi
mov    $0x1a,%edx
call   1070 <send@plt>
lea    0xd73(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12f2 <parse_request+0x162>
lea    0xd63(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_request>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xea5(%rip),%r14
test   %rax,%rax
jle    1289 <parse_request+0xf9>
lea    0xe44(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strtok@plt>
lea    0xe37(%rip),%rsi
mov    %rax,%rdi
mov    $0x3,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11fe <parse_request+0x6e>
mov    0x2dfc(%rip),%rax
mov    (%rax),%ecx
lea    0xe4a(%rip),%rsi
mov    %ebp,%edi
mov    $0x1a,%edx
call   1070 <send@plt>
jmp    1289 <parse_request+0xf9>
lea    0xdfb(%rip),%rsi
xor    %edi,%edi
call   10b0 <strtok@plt>
mov    %rax,%rbp
lea    0xdf0(%rip),%rsi
mov    %rax,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1251 <parse_request+0xc1>
add    $0x1,%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
call   1050 <strcpy@plt>
lea    0xdc7(%rip),%rdi
mov    %r14,%rsi
jmp    1284 <parse_request+0xf4>
lea    0xdb0(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
lea    0xd96(%rip),%rdi
lea    0xda9(%rip),%rsi
call   1080 <printf@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [4 x i8] c"GET\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [11 x i8] c"index.html\00"
@.str.4 = [26 x i8] c"Poziadavka na subor '%s'\0A\00"
@.str.5 = [10 x i8] c"index.htm\00"
@.str.6 = [27 x i8] c"HTTP/1.1 400 BAD REQUEST\0A\0A\00"
@MSG_NOSIGNAL = global i32 0
@.str.7 = [1 x i8] zeroinitializer
define i8* @parse_request(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = call i64 @recv(i32 %8, i8* %9, i32 1024, i32* null)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %54
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i8* @strtok(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %50
18: ; preds = %12
%19 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %18
%24 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 1
%28 = trunc i64 %27 to i32
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = call i32 @strcpy(i8* %30, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %48
33: ; preds = %18
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
%36 = load i8*, i8** %6
%37 = call i32 @strlen(i8* %36)
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = mul i64 %39, 1
%41 = trunc i64 %40 to i32
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %7
%43 = load i8*, i8** %7
%44 = load i8*, i8** %6
%45 = call i32 @strcpy(i8* %43, i8* %44)
%46 = load i8*, i8** %7
%47 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %33, %23
%49 = load i8*, i8** %7
store i8* %49, i8** %3
br label %55
50: ; preds = %12
%51 = load i32, i32* %4
%52 = load i32, i32* @MSG_NOSIGNAL
%53 = call i32 @send(i32 %51, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 26, i32 %52)
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), i8** %3
br label %55
54: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), i8** %3
br label %55
55: ; preds = %54, %50, %48
%56 = load i8*, i8** %3
ret i8* %56
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [4 x i8] c"GET\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [11 x i8] c"index.html\00"
@.str.4 = [26 x i8] c"Poziadavka na subor '%s'\0A\00"
@.str.5 = [10 x i8] c"index.htm\00"
@.str.6 = [27 x i8] c"HTTP/1.1 400 BAD REQUEST\0A\0A\00"
@MSG_NOSIGNAL = global i32 0
@.str.7 = [1 x i8] zeroinitializer
define i8* @parse_request(i32 %0, i8* %1) {
%3 = tail call i64 @recv(i32 %0, i8* %1, i32 1024, i32* null)
%4 = icmp sgt i64 %3, 0
br i1 %4, label %5, label %29
5: ; preds = %2
%6 = tail call i8* @strtok(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %5
%10 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%15 = add nsw i32 %14, 1
%16 = tail call i8* @malloc(i32 %15)
%17 = tail call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %29
19: ; preds = %9
%20 = getelementptr inbounds i8, i8* %10, i64 1
%21 = tail call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, 1
%23 = tail call i8* @malloc(i32 %22)
%24 = tail call i32 @strcpy(i8* %23, i8* %20)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %29
26: ; preds = %5
%27 = load i32, i32* @MSG_NOSIGNAL
%28 = tail call i32 @send(i32 %0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 26, i32 %27)
br label %29
29: ; preds = %2, %26, %19, %13
%30 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), %26 ], [ %16, %13 ], [ %23, %19 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0), %2 ]
ret i8* %30
}