#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 /*<<< orphan*/ request;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int create_tcp_endpoint(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strtok(char *, char *);
int /*<<< orphan*/ text(int, char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fdnet, fd, count, sock;
  char filename[100], request[1024], reply[1024];
  if ((sock = create_tcp_endpoint(8000)) < 0) {
    fprintf(stderr, "Cannot create endpoint\n");
    exit(1);
  }
  while (1) {
    fdnet = accept(sock, NULL, NULL);
    fprintf(stderr, "connected on fd %d\n", fdnet);
    read(fdnet, request, sizeof(request));
    strtok(request, " ");
    strcpy(filename, "/home/netlab/heinz/html");
    strcat(filename, strtok(NULL, " "));
    printf("Requested file is %s\n", filename);
    fd = open(filename, O_RDONLY);
    if (fd < 0) {
      text(fdnet, "HTTP/1.1 404 Not Found\n");
      text(fdnet, "Content-Type: text/html\n\n");
      text(fdnet, "<HTML><BODY> Not Found </BODY></HTML>");
    } else {
      text(fdnet, "HTTP/1.1 200 OK\n");
      text(fdnet, "Content-Type: text/html\n\n");
      while ((count = read(fd, reply, 1024)) > 0)
        write(fdnet, reply, count);
    }
    close(fd);
    close(fdnet);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x890,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1f40,%edi
call   10a0 <create_tcp_endpoint@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1219 <main+0x49>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
jmp    121e <main+0x4e>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x8(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x480(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
lea    -0x480(%rbp),%rdi
lea    0xdc0(%rip),%rsi
call   10c0 <strtok@plt>
lea    -0x80(%rbp),%rdi
lea    0xdb2(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x80(%rbp),%rax
mov    %rax,-0x888(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd95(%rip),%rsi
call   10c0 <strtok@plt>
mov    -0x888(%rbp),%rdi
mov    %eax,%esi
call   10e0 <strcat@plt>
lea    -0x80(%rbp),%rsi
lea    0xd91(%rip),%rdi
call   1060 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    130b <main+0x13b>
mov    -0x8(%rbp),%edi
lea    0xd79(%rip),%rsi
call   1030 <text@plt>
mov    -0x8(%rbp),%edi
lea    0xd82(%rip),%rsi
call   1030 <text@plt>
mov    -0x8(%rbp),%edi
lea    0xd8d(%rip),%rsi
call   1030 <text@plt>
jmp    1365 <main+0x195>
mov    -0x8(%rbp),%edi
lea    0xd9f(%rip),%rsi
call   1030 <text@plt>
mov    -0x8(%rbp),%edi
lea    0xd50(%rip),%rsi
call   1030 <text@plt>
mov    -0xc(%rbp),%edi
lea    -0x880(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1360 <main+0x190>
mov    -0x8(%rbp),%edi
lea    -0x880(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1050 <write@plt>
jmp    1329 <main+0x159>
jmp    1365 <main+0x195>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
jmp    121e <main+0x4e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x888,%rsp
mov    $0x1f40,%edi
call   10a0 <create_tcp_endpoint@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     1337 <main+0x167>
lea    0x480(%rsp),%r13
lea    0xe26(%rip),%rbp
lea    0x10(%rsp),%rbx
lea    0x80(%rsp),%r14
jmp    125a <main+0x8a>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0xe35(%rip),%rsi
call   1030 <text@plt>
mov    %r12d,%edi
lea    0xe3e(%rip),%rsi
call   1030 <text@plt>
mov    %r12d,%edi
lea    0xe49(%rip),%rsi
call   1030 <text@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%r12d
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
call   10c0 <strtok@plt>
mov    %rbx,%rdi
lea    0xd85(%rip),%rsi
call   1040 <strcpy@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   10c0 <strtok@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   10e0 <strcat@plt>
lea    0xd7d(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <open@plt>
mov    %eax,%r15d
mov    %r12d,%edi
test   %eax,%eax
js     1220 <main+0x50>
lea    0xdbd(%rip),%rsi
call   1030 <text@plt>
mov    %r12d,%edi
lea    0xd6e(%rip),%rsi
call   1030 <text@plt>
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    124a <main+0x7a>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
jmp    1310 <main+0x140>
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
lea    0xcb9(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [24 x i8] c"Cannot create endpoint\0A\00"
@.str.1 = [20 x i8] c"connected on fd %d\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [24 x i8] c"/home/netlab/heinz/html\00"
@.str.4 = [22 x i8] c"Requested file is %s\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [24 x i8] c"HTTP/1.1 404 Not Found\0A\00"
@.str.6 = [26 x i8] c"Content-Type: text/html\0A\0A\00"
@.str.7 = [38 x i8] c"<HTML><BODY> Not Found </BODY></HTML>\00"
@.str.8 = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
store i32 0, i32* %1
%9 = call i32 @create_tcp_endpoint(i32 8000)
store i32 %9, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
br label %16
16: ; preds = %15, %62
%17 = load i32, i32* %5
%18 = call i32 @accept(i32 %17, i32* null, i32* null)
store i32 %18, i32* %2
%19 = load i32, i32* @stderr
%20 = load i32, i32* %2
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %2
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 1024)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = call i32 @strtok(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%28 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%30 = call i32 @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @strcat(i8* %29, i32 %30)
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %3
%37 = load i32, i32* %3
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %16
%40 = load i32, i32* %2
%41 = call i32 @text(i32 %40, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%42 = load i32, i32* %2
%43 = call i32 @text(i32 %42, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%44 = load i32, i32* %2
%45 = call i32 @text(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
br label %62
46: ; preds = %16
%47 = load i32, i32* %2
%48 = call i32 @text(i32 %47, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%49 = load i32, i32* %2
%50 = call i32 @text(i32 %49, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %56, %46
%52 = load i32, i32* %3
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 1024)
store i32 %54, i32* %4
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i32, i32* %2
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %4
%60 = call i32 @write(i32 %57, i8* %58, i32 %59)
br label %51
61: ; preds = %51
br label %62
62: ; preds = %61, %39
%63 = load i32, i32* %3
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %2
%66 = call i32 @close(i32 %65)
br label %16
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [24 x i8] c"Cannot create endpoint\0A\00"
@.str.1 = [20 x i8] c"connected on fd %d\0A\00"
@.str.2 = [2 x i8] c" \00"
@.str.3 = [24 x i8] c"/home/netlab/heinz/html\00"
@.str.4 = [22 x i8] c"Requested file is %s\0A\00"
@O_RDONLY = global i32 0
@.str.5 = [24 x i8] c"HTTP/1.1 404 Not Found\0A\00"
@.str.6 = [26 x i8] c"Content-Type: text/html\0A\0A\00"
@.str.7 = [38 x i8] c"<HTML><BODY> Not Found </BODY></HTML>\00"
@.str.8 = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @create_tcp_endpoint(i32 8000)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0, %40
%14 = call i32 @accept(i32 %7, i32* null, i32* null)
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%17 = call i32 @read(i32 %14, i8* %5, i32 1024)
%18 = call i32 @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @strcat(i8* %4, i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %4, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %13
%27 = call i32 @text(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @text(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%29 = call i32 @text(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
br label %40
30: ; preds = %13
%31 = call i32 @text(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%32 = call i32 @text(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 @read(i32 %24, i8* %6, i32 1024)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30, %35
%36 = phi i32 [ %38, %35 ], [ %33, %30 ]
%37 = call i32 @write(i32 %14, i8* %6, i32 %36)
%38 = call i32 @read(i32 %24, i8* %6, i32 1024)
%39 = icmp sgt i32 %38, 0
br i1 %39, label %35, label %40
40: ; preds = %35, %30, %26
%41 = call i32 @close(i32 %24)
%42 = call i32 @close(i32 %14)
br label %13
}