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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int fileserver(int readfd, int writefd) {
  int fd = 0;
  int len = 0;
  ssize_t r_cnt = 0;
  ssize_t w_cnt = 0;
  char path[1024];
  char buf[4096];
  memset(path, 0, 1024);
  if (0 >= (r_cnt = read(readfd, path, 1024))) {
    printf("[Server] error: you never give the filepath!\n");
    return (-1);
  }
  printf("[Server] read file: %s\n", path);
  if (0 > (fd = open(path, O_RDONLY))) {
    perror("[Server] open");
    exit(-1);
  }
  memset(buf, 0, 4096);
  while (0 < (r_cnt = read(fd, buf, 4096))) {
    write(writefd, buf, r_cnt);
    memset(buf, 0, 4096);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<fileserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jl     11fd <fileserver+0x7d>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <fileserver+0x13b>
lea    -0x420(%rbp),%rsi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    124c <fileserver+0xcc>
lea    0xe09(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x1420(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%edi
lea    -0x1420(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    12ac <fileserver+0x12c>
mov    -0xc(%rbp),%edi
lea    -0x1420(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
lea    -0x1420(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
jmp    125f <fileserver+0xdf>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileserver>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1408,%rsp
mov    %esi,%r15d
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1268 <fileserver+0xd8>
lea    0xe2e(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     128b <fileserver+0xfb>
mov    %eax,%ebp
lea    0x400(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    125f <fileserver+0xcf>
lea    0x400(%rsp),%rbx
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1230 <fileserver+0xa0>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    127a <fileserver+0xea>
lea    0xdb7(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x1408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd86(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [46 x i8] c"[Server] error: you never give the filepath!\0A\00"
@.str.1 = [24 x i8] c"[Server] read file: %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"[Server] open\00"
define i32 @fileserver(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca [4096 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 1024)
%14 = load i32, i32* %4
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 1024)
store i32 %16, i32* %8
%17 = icmp sge i32 0, %16
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
20: ; preds = %2
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = icmp sgt i32 0, %25
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %20
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i32 0, i32 4096)
br label %33
33: ; preds = %38, %30
%34 = load i32, i32* %6
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 4096)
store i32 %36, i32* %8
%37 = icmp slt i32 0, %36
br i1 %37, label %38, label %45
38: ; preds = %33
%39 = load i32, i32* %5
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%41 = load i32, i32* %8
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
%43 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 4096)
br label %33
45: ; preds = %33
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
store i32 0, i32* %3
br label %48
48: ; preds = %45, %18
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str.1 = [24 x i8] c"[Server] read file: %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"[Server] open\00"
@str = [45 x i8] c"[Server] error: you never give the filepath!\00"
define i32 @fileserver(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %5, i32 0, i32 1024)
%8 = call i32 @read(i32 %0, i8* %5, i32 1024)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
br label %32
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %5, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %12
%21 = call i32 @memset(i8* %6, i32 0, i32 4096)
%22 = call i32 @read(i32 %15, i8* %6, i32 4096)
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20, %24
%25 = phi i32 [ %28, %24 ], [ %22, %20 ]
%26 = call i32 @write(i32 %1, i8* %6, i32 %25)
%27 = call i32 @memset(i8* %6, i32 0, i32 4096)
%28 = call i32 @read(i32 %15, i8* %6, i32 4096)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %24, label %30
30: ; preds = %24, %20
%31 = call i32 @close(i32 %15)
br label %32
32: ; preds = %30, %10
%33 = phi i32 [ -1, %10 ], [ 0, %30 ]
ret i32 %33
}