#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*/ errno;
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
int /*<<< orphan*/ write(int, char *, size_t);
void server(int readfd, int writefd) {
  int fd;
  ssize_t n;
  char buff[4096];
  while ((n = read(readfd, buff, 4096)) == 0)
    ;
  buff[n] = '\0';
  if ((fd = open(buff, O_RDONLY)) < 0) {
    snprintf(buff + n, sizeof(buff) - n, ": can't open %s\n", strerror(errno));
    n = strlen(buff);
    write(writefd, buff, n);
  } else {
    while ((n = read(fd, buff, 4096)) > 0) {
      write(writefd, buff, n);
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11a8 <server+0x38>
jmp    1181 <server+0x11>
mov    -0x18(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1248 <server+0xd8>
lea    -0x1020(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x1030(%rbp)
mov    $0x1000,%eax
sub    -0x18(%rbp),%rax
mov    %eax,-0x1024(%rbp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1030(%rbp),%rdi
mov    -0x1024(%rbp),%esi
mov    %rax,%rcx
lea    0xde5(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
jmp    128f <server+0x11f>
jmp    124d <server+0xdd>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jbe    1287 <server+0x117>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
jmp    124d <server+0xdd>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
je     1190 <server+0x20>
mov    %rax,%rbx
movb   $0x0,(%rsp,%rax,1)
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1216 <server+0xa6>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
je     1202 <server+0x92>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
jne    11e0 <server+0x70>
mov    %ebp,%edi
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <close@plt>
lea    (%rsp,%rbx,1),%r15
mov    $0x1000,%ebp
sub    %ebx,%ebp
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdca(%rip),%rdx
mov    %r15,%rdi
mov    %ebp,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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