#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 size_t ssize_t;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *);
size_t strlen(char *);
int /*<<< orphan*/ write(int, char *, size_t);
void server(int read_fd, int write_fd) {
  int fd;
  ssize_t n;
  char buff[1024 + 1];
  if ((n = read(read_fd, buff, 1024)) == 0) {
    printf("end-of-file while reading pathname\n");
    exit(0);
  }
  buff[n] = '\0';
  if ((fd = open(buff, O_RDONLY)) < 0) {
    snprintf(buff + n, sizeof(buff) - n, ": can't open\n");
    n = strlen(buff);
    write(write_fd, buff, n);
  } else {
    while ((n = read(fd, buff, 1024)) > 0) {
      write(write_fd, buff, n);
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11c6 <server+0x46>
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    123d <server+0xbd>
lea    -0x420(%rbp),%rdi
add    -0x18(%rbp),%rdi
mov    $0x401,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe14(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
jmp    1284 <server+0x104>
jmp    1242 <server+0xc2>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jbe    127c <server+0xfc>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
jmp    1242 <server+0xc2>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r15d
mov    %rsp,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %rax,%rax
je     1255 <server+0xd5>
mov    %rax,%r14
movb   $0x0,(%rsp,%rax,1)
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1216 <server+0x96>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1080 <read@plt>
test   %rax,%rax
je     1202 <server+0x82>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %rax,%rax
jne    11e0 <server+0x60>
mov    %ebp,%edi
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <close@plt>
lea    (%rsp,%r14,1),%rdi
mov    $0x401,%esi
sub    %r14d,%esi
lea    0xdd7(%rip),%rdx
call   1060 <snprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb2(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"end-of-file while reading pathname\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c": can't open\0A\00"
define void @server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca [1025 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 1024)
store i64 %10, i64* %6
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = load i64, i64* %6
%17 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 %16
store i8 0, i8* %17
%18 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %15
%23 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%24 = load i64, i64* %6
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i64, i64* %6
%27 = sub i64 1025, %26
%28 = trunc i64 %27 to i32
%29 = call i32 @snprintf(i8* %25, i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%30 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%31 = call i64 @strlen(i8* %30)
store i64 %31, i64* %6
%32 = load i32, i32* %4
%33 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%34 = load i64, i64* %6
%35 = call i32 @write(i32 %32, i8* %33, i64 %34)
br label %50
36: ; preds = %15
br label %37
37: ; preds = %42, %36
%38 = load i32, i32* %5
%39 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%40 = call i64 @read(i32 %38, i8* %39, i32 1024)
store i64 %40, i64* %6
%41 = icmp ugt i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i32, i32* %4
%44 = getelementptr inbounds [1025 x i8], [1025 x i8]* %7, i64 0, i64 0
%45 = load i64, i64* %6
%46 = call i32 @write(i32 %43, i8* %44, i64 %45)
br label %37
47: ; preds = %37
%48 = load i32, i32* %5
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %22
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c": can't open\0A\00"
@str = [35 x i8] c"end-of-file while reading pathname\00"
define void @server(i32 %0, i32 %1) {
%3 = alloca [1025 x i8]
%4 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%5 = call i64 @read(i32 %0, i8* %4, i32 1024)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 %5
store i8 0, i8* %11
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %4, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = call i64 @read(i32 %13, i8* %4, i32 1024)
%17 = icmp eq i64 %16, 0
br i1 %17, label %29, label %24
18: ; preds = %10
%19 = trunc i64 %5 to i32
%20 = sub i32 1025, %19
%21 = call i32 @snprintf(i8* %11, i32 %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = call i64 @strlen(i8* %4)
%23 = call i32 @write(i32 %1, i8* %4, i64 %22)
br label %31
24: ; preds = %15, %24
%25 = phi i64 [ %27, %24 ], [ %16, %15 ]
%26 = call i32 @write(i32 %1, i8* %4, i64 %25)
%27 = call i64 @read(i32 %13, i8* %4, i32 1024)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %24
29: ; preds = %24, %15
%30 = tail call i32 @close(i32 %13)
br label %31
31: ; preds = %29, %18
ret void
}