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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
void server(int readfd, int writefd) {
  int fd;
  ssize_t n;
  char buff[(100) + 1] = {0};
  if (-1 == (n = read(readfd, buff, (100)))) {
    printf("read error: %s", strerror(errno));
  } else if (0 == n) {
    perror("end-of-file while reading pathname");
  }
  buff[n] = '\0';
  if (-1 == (fd = open(buff, O_RDONLY))) {
    perror("open error");
    snprintf(buff + n, sizeof(buff) - n, ": can't oepn, %s\n", strerror(errno));
    n = strlen(buff);
    write(writefd, buff, n);
  } else {
    while (0 < (n = read(fd, buff, (100)))) {
      if (-1 == write(writefd, buff, n)) {
        printf("write error: %s", strerror(errno));
      }
    }
    if (-1 == close(fd)) {
      printf("close fd error: %s", strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x65,%edx
call   1070 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1090 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1206 <server+0x66>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xe04(%rip),%rdi
call   1050 <printf@plt>
jmp    1222 <server+0x82>
xor    %eax,%eax
cmp    -0x10(%rbp),%eax
jne    121d <server+0x7d>
lea    0xdf7(%rip),%rdi
call   10b0 <perror@plt>
jmp    1222 <server+0x82>
movslq -0x10(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rdi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12c9 <server+0x129>
lea    0xddc(%rip),%rdi
call   10b0 <perror@plt>
lea    -0x80(%rbp),%rax
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x90(%rbp)
movslq -0x10(%rbp),%rcx
mov    $0x65,%eax
sub    %rcx,%rax
mov    %eax,-0x84(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x90(%rbp),%rdi
mov    -0x84(%rbp),%esi
mov    %rax,%rcx
lea    0xd99(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
jmp    1367 <server+0x1c7>
jmp    12ce <server+0x12e>
mov    -0xc(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1090 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x10(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    132e <server+0x18e>
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1329 <server+0x189>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd2b(%rip),%rdi
call   1050 <printf@plt>
jmp    12ce <server+0x12e>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1362 <server+0x1c2>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd02(%rip),%rdi
call   1050 <printf@plt>
jmp    1367 <server+0x1c7>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %esi,%r15d
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movq   $0x0,0x5d(%rsp)
mov    %rsp,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     11f0 <server+0x60>
test   %r14d,%r14d
jne    120d <server+0x7d>
lea    0xe26(%rip),%rdi
call   10a0 <perror@plt>
jmp    120d <server+0x7d>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdfb(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
movslq %r14d,%rbx
movb   $0x0,(%rsp,%rbx,1)
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12dd <server+0x14d>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x64,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    129b <server+0x10b>
mov    %rsp,%rbx
mov    0x2d8b(%rip),%r12
lea    0xdfb(%rip),%r14
jmp    1273 <server+0xe3>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    129b <server+0x10b>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1260 <server+0xd0>
mov    (%r12),%edi
call   10b0 <strerror@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
jmp    1260 <server+0xd0>
mov    %ebp,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
je     12b4 <server+0x124>
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd96(%rip),%rdi
mov    %rax,%rsi
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
add    %rsp,%rbx
lea    0xd4b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x65,%ebp
sub    %r14d,%ebp
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd34(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    12a7 <server+0x117>

LLVM IR (-O0):
@.str = [15 x i8] c"read error: %s\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"end-of-file while reading pathname\00"
@O_RDONLY = global i32 0
@.str.2 = [11 x i8] c"open error\00"
@.str.3 = [18 x i8] c": can't oepn, %s\0A\00"
@.str.4 = [16 x i8] c"write error: %s\00"
@.str.5 = [19 x i8] c"close fd error: %s\00"
define void @server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [101 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = bitcast [101 x i8]* %7 to i8*
%9 = load i32, i32* %3
%10 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 100)
store i32 %11, i32* %6
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %23
17: ; preds = %2
%18 = load i32, i32* %6
%19 = icmp eq i32 0, %18
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
br label %23
23: ; preds = %22, %13
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 %25
store i8 0, i8* %26
%27 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %5
%30 = icmp eq i32 -1, %29
br i1 %30, label %31, label %50
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%33 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i32, i32* %6
%38 = sext i32 %37 to i64
%39 = sub i64 101, %38
%40 = trunc i64 %39 to i32
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @snprintf(i8* %36, i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%45 = call i32 @strlen(i8* %44)
store i32 %45, i32* %6
%46 = load i32, i32* %4
%47 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%48 = load i32, i32* %6
%49 = call i32 @write(i32 %46, i8* %47, i32 %48)
br label %76
50: ; preds = %23
br label %51
51: ; preds = %66, %50
%52 = load i32, i32* %5
%53 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 100)
store i32 %54, i32* %6
%55 = icmp slt i32 0, %54
br i1 %55, label %56, label %67
56: ; preds = %51
%57 = load i32, i32* %4
%58 = getelementptr inbounds [101 x i8], [101 x i8]* %7, i64 0, i64 0
%59 = load i32, i32* %6
%60 = call i32 @write(i32 %57, i8* %58, i32 %59)
%61 = icmp eq i32 -1, %60
br i1 %61, label %62, label %66
62: ; preds = %56
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %62, %56
br label %51
67: ; preds = %51
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
%70 = icmp eq i32 -1, %69
br i1 %70, label %71, label %75
71: ; preds = %67
%72 = load i32, i32* @errno
%73 = call i8* @strerror(i32 %72)
%74 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %73)
br label %75
75: ; preds = %71, %67
br label %76
76: ; preds = %75, %31
ret void
}

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