#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*/ 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[256];
  char buf[1024];
  memset(path, 0, 256);
  if (0 >= (r_cnt = read(readfd, path, 256))) {
    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");
    return (-1);
  }
  memset(buf, 0, 1024);
  while (0 < (r_cnt = read(fd, buf, 1024))) {
    write(writefd, buf, r_cnt);
    memset(buf, 0, 1024);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<fileserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x520,%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    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jl     11ed <fileserver+0x7d>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <fileserver+0x13d>
lea    -0x120(%rbp),%rsi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    123e <fileserver+0xce>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ad <fileserver+0x13d>
lea    -0x520(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%edi
lea    -0x520(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    129e <fileserver+0x12e>
mov    -0xc(%rbp),%edi
lea    -0x520(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
lea    -0x520(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
jmp    1251 <fileserver+0xe1>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x520,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileserver>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x508,%rsp
mov    %esi,%r15d
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1258 <fileserver+0xd8>
lea    0xe3e(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     127b <fileserver+0xfb>
mov    %eax,%ebp
lea    0x100(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    124f <fileserver+0xcf>
lea    0x100(%rsp),%rbx
nop
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1220 <fileserver+0xa0>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    126a <fileserver+0xea>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x508,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd96(%rip),%rdi
call   10a0 <perror@plt>
jmp    1264 <fileserver+0xe4>

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 [256 x i8]
%11 = alloca [1024 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 [256 x i8], [256 x i8]* %10, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 256)
%14 = load i32, i32* %4
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%16 = call i32 @read(i32 %14, i8* %15, i32 256)
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 %47
20: ; preds = %2
%21 = getelementptr inbounds [256 x i8], [256 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 [256 x i8], [256 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 %29
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %47
29: ; preds = %20
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 1024)
br label %32
32: ; preds = %37, %29
%33 = load i32, i32* %6
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 1024)
store i32 %35, i32* %8
%36 = icmp slt i32 0, %35
br i1 %36, label %37, label %44
37: ; preds = %32
%38 = load i32, i32* %5
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%40 = load i32, i32* %8
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%43 = call i32 @memset(i8* %42, i32 0, i32 1024)
br label %32
44: ; preds = %32
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %47
47: ; preds = %44, %27, %18
%48 = load i32, i32* %3
ret i32 %48
}

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 [256 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %5, i32 0, i32 256)
%8 = call i32 @read(i32 %0, i8* %5, i32 256)
%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 %31
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 %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %31
19: ; preds = %12
%20 = call i32 @memset(i8* %6, i32 0, i32 1024)
%21 = call i32 @read(i32 %15, i8* %6, i32 1024)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %19, %23
%24 = phi i32 [ %27, %23 ], [ %21, %19 ]
%25 = call i32 @write(i32 %1, i8* %6, i32 %24)
%26 = call i32 @memset(i8* %6, i32 0, i32 1024)
%27 = call i32 @read(i32 %15, i8* %6, i32 1024)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %23, label %29
29: ; preds = %23, %19
%30 = call i32 @close(i32 %15)
br label %31
31: ; preds = %29, %17, %10
%32 = phi i32 [ -1, %10 ], [ -1, %17 ], [ 0, %29 ]
ret i32 %32
}