#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 */
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
scalar_t__ EACCES;
int /*<<< orphan*/ O_RDONLY;
int client_200(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ client_403(int);
int /*<<< orphan*/ client_404(int);
int /*<<< orphan*/ client_write_file(int, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ get_file_type(char const *, char *);
int open(char *, int /*<<< orphan*/);
char *safe_malloc(int);
int /*<<< orphan*/ server_error(int, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
void write_client(int sock, const char *path) {
  struct stat buf;
  char *rel_path = safe_malloc(256);
  strcpy(rel_path, ".");
  strcat(rel_path, path);
  int fd = open(rel_path, O_RDONLY);
  if (fd < 0) {
    free(rel_path);
    client_404(sock);
    return;
  }
  if ((stat(rel_path, &buf)) < 0) {
    free(rel_path);
    close(fd);
    if (errno == EACCES)
      client_403(sock);
    else
      client_404(sock);
    return;
  }
  char type[256];
  get_file_type(path, type);
  int fail = client_200(sock, buf.st_size, type);
  if (!fail) {
    client_write_file(sock, fd);
    free(rel_path);
  } else {
    free(rel_path);
    server_error(sock, "writing 200 request to client");
    return;
  }
}


x86 assembly (clang, -O0):
<write_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x100,%edi
call   1060 <safe_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10e0 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1242 <write_client+0x72>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x4(%rbp),%edi
call   1090 <client_404@plt>
jmp    1304 <write_client+0x134>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    129e <write_client+0xce>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%rax
mov    0x2d65(%rip),%rcx
cmp    (%rcx),%rax
jne    1291 <write_client+0xc1>
mov    -0x4(%rbp),%edi
call   1030 <client_403@plt>
jmp    1299 <write_client+0xc9>
mov    -0x4(%rbp),%edi
call   1090 <client_404@plt>
jmp    1304 <write_client+0x134>
mov    -0x10(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   10a0 <get_file_type@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
lea    -0x130(%rbp),%rdx
call   10b0 <client_200@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
jne    12ec <write_client+0x11c>
mov    -0x4(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10f0 <client_write_file@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
jmp    1304 <write_client+0x134>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x4(%rbp),%edi
lea    0xd03(%rip),%rsi
call   10d0 <server_error@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
mov    $0x100,%edi
call   1060 <safe_malloc@plt>
mov    %rax,%rbx
lea    0xe07(%rip),%rsi
mov    %rax,%rdi
call   1050 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   10e0 <strcat@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     1271 <write_client+0xa1>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
js     1290 <write_client+0xc0>
lea    0x10(%rsp),%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   10a0 <get_file_type@plt>
mov    0x8(%rsp),%esi
mov    %r14d,%edi
mov    %r12,%rdx
call   10b0 <client_200@plt>
test   %eax,%eax
je     12bf <write_client+0xef>
mov    %rbx,%rdi
call   1040 <free@plt>
lea    0xd9b(%rip),%rsi
mov    %r14d,%edi
call   10d0 <server_error@plt>
jmp    12db <write_client+0x10b>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14d,%edi
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <client_404@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    0x2d30(%rip),%rcx
cmp    (%rcx),%rax
jne    12d3 <write_client+0x103>
mov    %r14d,%edi
call   1030 <client_403@plt>
jmp    12db <write_client+0x10b>
mov    %r14d,%edi
mov    %ebp,%esi
call   10f0 <client_write_file@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
jmp    12db <write_client+0x10b>
mov    %r14d,%edi
call   1090 <client_404@plt>
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@.str.1 = [30 x i8] c"writing 200 request to client\00"
define void @write_client(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = call i8* @safe_malloc(i32 256)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %6
%14 = load i8*, i8** %4
%15 = call i32 @strcat(i8* %13, i8* %14)
%16 = load i8*, i8** %6
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %2
%22 = load i8*, i8** %6
%23 = call i32 @free(i8* %22)
%24 = load i32, i32* %3
%25 = call i32 @client_404(i32 %24)
br label %67
26: ; preds = %2
%27 = load i8*, i8** %6
%28 = call i64 @stat(i8* %27, %struct.stat* %5)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
%35 = load i64, i64* @errno
%36 = load i64, i64* @EACCES
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i32, i32* %3
%40 = call i32 @client_403(i32 %39)
br label %44
41: ; preds = %30
%42 = load i32, i32* %3
%43 = call i32 @client_404(i32 %42)
br label %44
44: ; preds = %41, %38
br label %67
45: ; preds = %26
%46 = load i8*, i8** %4
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%48 = call i32 @get_file_type(i8* %46, i8* %47)
%49 = load i32, i32* %3
%50 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%53 = call i32 @client_200(i32 %49, i32 %51, i8* %52)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp ne i32 %54, 0
br i1 %55, label %62, label %56
56: ; preds = %45
%57 = load i32, i32* %3
%58 = load i32, i32* %7
%59 = call i32 @client_write_file(i32 %57, i32 %58)
%60 = load i8*, i8** %6
%61 = call i32 @free(i8* %60)
br label %67
62: ; preds = %45
%63 = load i8*, i8** %6
%64 = call i32 @free(i8* %63)
%65 = load i32, i32* %3
%66 = call i32 @server_error(i32 %65, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %67
67: ; preds = %21, %44, %62, %56
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EACCES = global i64 0
@.str.1 = [30 x i8] c"writing 200 request to client\00"
define void @write_client(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = tail call i8* @safe_malloc(i32 256)
%7 = tail call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @strcat(i8* %6, i8* %1)
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %6, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @free(i8* %6)
%14 = tail call i32 @client_404(i32 %0)
br label %42
15: ; preds = %2
%16 = call i64 @stat(i8* %6, %struct.stat* %3)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %15
%19 = call i32 @free(i8* %6)
%20 = call i32 @close(i32 %10)
%21 = load i64, i64* @errno
%22 = load i64, i64* @EACCES
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @client_403(i32 %0)
br label %42
26: ; preds = %18
%27 = call i32 @client_404(i32 %0)
br label %42
28: ; preds = %15
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = call i32 @get_file_type(i8* %1, i8* %29)
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @client_200(i32 %0, i32 %32, i8* %29)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = call i32 @client_write_file(i32 %0, i32 %10)
%37 = call i32 @free(i8* %6)
br label %41
38: ; preds = %28
%39 = call i32 @free(i8* %6)
%40 = call i32 @server_error(i32 %0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %41
41: ; preds = %35, %38
br label %42
42: ; preds = %24, %26, %41, %12
ret void
}