#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 int32_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int get_line(int, char *, int);
int /*<<< orphan*/ headers(int, char const *);
int /*<<< orphan*/ not_found(int);
int /*<<< orphan*/ printf(char *, char const *, int);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
void serve_file(int32_t client, const char *filename) {
  FILE *resource = NULL;
  int32_t numchars = 1;
  char buf[1024];
  buf[0] = 'A';
  buf[1] = '\0';
  printf("Serve file %s to %d\n", filename, client);
  while ((numchars > 0) && strcmp("\n", buf)) {
    numchars = get_line(client, buf, sizeof(buf));
  }
  resource = fopen(filename, "r");
  if (resource == NULL) {
    not_found(client);
  } else {
    headers(client, filename);
    fgets(buf, sizeof(buf), resource);
    while (!feof(resource)) {
      send(client, buf, strlen(buf), 0);
      fgets(buf, sizeof(buf), resource);
    }
  }
  fclose(resource);
}


x86 assembly (clang, -O0):
<serve_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movb   $0x41,-0x420(%rbp)
movb   $0x0,-0x41f(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xe13(%rip),%rdi
call   1070 <printf@plt>
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x421(%rbp)
jle    1224 <serve_file+0x74>
lea    -0x420(%rbp),%rsi
lea    0xe01(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x421(%rbp)
mov    -0x421(%rbp),%al
test   $0x1,%al
jne    1237 <serve_file+0x87>
jmp    1253 <serve_file+0xa3>
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <get_line@plt>
mov    %eax,-0x1c(%rbp)
jmp    11f2 <serve_file+0x42>
mov    -0x10(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    127f <serve_file+0xcf>
mov    -0x4(%rbp),%edi
call   10b0 <not_found@plt>
jmp    1316 <serve_file+0x166>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10c0 <headers@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
mov    -0x18(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12be <serve_file+0x10e>
jmp    1311 <serve_file+0x161>
mov    -0x4(%rbp),%eax
mov    %eax,-0x434(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x420(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1080 <fgets@plt>
jmp    12a0 <serve_file+0xf0>
jmp    1316 <serve_file+0x166>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movw   $0x41,(%rsp)
lea    0xe31(%rip),%rdi
mov    %ebp,%edx
call   1070 <printf@plt>
lea    0xe36(%rip),%r15
mov    %rsp,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1203 <serve_file+0x53>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <get_line@plt>
test   %eax,%eax
jg     11e0 <serve_file+0x30>
lea    0xe0b(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1284 <serve_file+0xd4>
mov    %ebp,%edi
mov    %r14,%rsi
call   10c0 <headers@plt>
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
jne    128b <serve_file+0xdb>
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <feof@plt>
test   %eax,%eax
je     1250 <serve_file+0xa0>
jmp    128b <serve_file+0xdb>
mov    %ebp,%edi
call   10b0 <not_found@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Serve file %s to %d\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [2 x i8] c"r\00"
define void @serve_file(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca [1024 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32* null, i32** %5
store i32 1, i32* %6
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
store i8 65, i8* %8
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 1
store i8 0, i8* %9
%10 = load i8*, i8** %4
%11 = load i32, i32* %3
%12 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %11)
br label %13
13: ; preds = %22, %2
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%18 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = icmp ne i64 %18, 0
br label %20
20: ; preds = %16, %13
%21 = phi i1 [ false, %13 ], [ %19, %16 ]
br i1 %21, label %22, label %26
22: ; preds = %20
%23 = load i32, i32* %3
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%25 = call i32 @get_line(i32 %23, i8* %24, i32 1024)
store i32 %25, i32* %6
br label %13
26: ; preds = %20
%27 = load i8*, i8** %4
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %5
%29 = load i32*, i32** %5
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* %3
%33 = call i32 @not_found(i32 %32)
br label %56
34: ; preds = %26
%35 = load i32, i32* %3
%36 = load i8*, i8** %4
%37 = call i32 @headers(i32 %35, i8* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%39 = load i32*, i32** %5
%40 = call i32 @fgets(i8* %38, i32 1024, i32* %39)
br label %41
41: ; preds = %46, %34
%42 = load i32*, i32** %5
%43 = call i32 @feof(i32* %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br i1 %45, label %46, label %55
46: ; preds = %41
%47 = load i32, i32* %3
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @send(i32 %47, i8* %48, i32 %50, i32 0)
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%53 = load i32*, i32** %5
%54 = call i32 @fgets(i8* %52, i32 1024, i32* %53)
br label %41
55: ; preds = %41
br label %56
56: ; preds = %55, %31
%57 = load i32*, i32** %5
%58 = call i32 @fclose(i32* %57)
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"Serve file %s to %d\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [2 x i8] c"r\00"
define void @serve_file(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8 65, i8* %4
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 1
store i8 0, i8* %5
%6 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %0)
br label %7
7: ; preds = %2, %10
%8 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = call i32 @get_line(i32 %0, i8* %4, i32 1024)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %7, label %13
13: ; preds = %10, %7
%14 = call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @not_found(i32 %0)
br label %29
18: ; preds = %13
%19 = call i32 @headers(i32 %0, i8* %1)
%20 = call i32 @fgets(i8* %4, i32 1024, i32* %14)
%21 = call i32 @feof(i32* %14)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18, %23
%24 = call i32 @strlen(i8* %4)
%25 = call i32 @send(i32 %0, i8* %4, i32 %24, i32 0)
%26 = call i32 @fgets(i8* %4, i32 1024, i32* %14)
%27 = call i32 @feof(i32* %14)
%28 = icmp eq i32 %27, 0
br i1 %28, label %23, label %29
29: ; preds = %23, %18, %16
%30 = call i32 @fclose(i32* %14)
ret void
}