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

/* Variables and functions */
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int write(int, char *, int);
void server(int readfd, int writefd) {
  char buff[1024];
  int n, fd;
  if ((n = read(readfd, buff, 1024)) <= 0)
    printf("Server: filename read error\n");
  buff[n] = '\0';
  if ((fd = open(buff, 0)) < 0) {
    strcat(buff, " can't open\n");
    n = strlen(buff);
    if (write(writefd, buff, n) != n)
      printf("Server: errmsg write error\n");
  } else {
    while ((n = read(fd, buff, 1024)) > 0)
      if (write(writefd, buff, n) != n)
        printf("Server: data write error\n");
    if (n < 0)
      printf("Server: read error\n");
  }
}


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    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jg     11a0 <server+0x40>
lea    0xe65(%rip),%rdi
call   1050 <printf@plt>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0x418(%rbp)
cmp    $0x0,%eax
jge    1223 <server+0xc3>
lea    -0x410(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     121e <server+0xbe>
lea    0xe11(%rip),%rdi
call   1050 <printf@plt>
jmp    129e <server+0x13e>
jmp    1228 <server+0xc8>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    1280 <server+0x120>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     127b <server+0x11b>
lea    0xdd0(%rip),%rdi
call   1050 <printf@plt>
jmp    1228 <server+0xc8>
cmpl   $0x0,-0x414(%rbp)
jge    1299 <server+0x139>
lea    0xdcc(%rip),%rdi
call   1050 <printf@plt>
jmp    129e <server+0x13e>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %esi,%r14d
mov    %rsp,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     1191 <server+0x31>
lea    0xec8(%rip),%rdi
call   1030 <puts@plt>
movslq %ebx,%rax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
xor    %esi,%esi
call   1070 <open@plt>
test   %eax,%eax
js     1220 <server+0xc0>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1201 <server+0xa1>
mov    %rsp,%rbx
lea    0xe58(%rip),%r15
jmp    11e6 <server+0x86>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1201 <server+0xa1>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11d0 <server+0x70>
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    11d0 <server+0x70>
test   %ebp,%ebp
jns    1259 <server+0xf9>
lea    0xe01(%rip),%rdi
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
lea    0xdd9(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1259 <server+0xf9>
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Server: filename read error\0A\00"
@.str.1 = [13 x i8] c" can't open\0A\00"
@.str.2 = [28 x i8] c"Server: errmsg write error\0A\00"
@.str.3 = [26 x i8] c"Server: data write error\0A\00"
@.str.4 = [20 x i8] c"Server: read error\0A\00"
define void @server(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 1024)
store i32 %10, i32* %6
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %16
store i8 0, i8* %17
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = call i32 @open(i8* %18, i32 0)
store i32 %19, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %35
21: ; preds = %14
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @strcat(i8* %22, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %4
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %6
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
%30 = load i32, i32* %6
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
br label %57
35: ; preds = %14
br label %36
36: ; preds = %50, %35
%37 = load i32, i32* %7
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 1024)
store i32 %39, i32* %6
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %36
%42 = load i32, i32* %4
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = load i32, i32* %6
%45 = call i32 @write(i32 %42, i8* %43, i32 %44)
%46 = load i32, i32* %6
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %41
br label %36
51: ; preds = %36
%52 = load i32, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
br label %57
57: ; preds = %56, %34
ret void
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c" can't open\0A\00"
@str = [19 x i8] c"Server: read error\00"
@str.5 = [25 x i8] c"Server: data write error\00"
@str.6 = [27 x i8] c"Server: errmsg write error\00"
@str.7 = [28 x i8] c"Server: filename read error\00"
define void @server(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 1024)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = sext i32 %5 to i64
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %10
store i8 0, i8* %11
%12 = call i32 @open(i8* %4, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = call i32 @read(i32 %12, i8* %4, i32 1024)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %24, label %33
17: ; preds = %9
%18 = call i32 @strcat(i8* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @strlen(i8* %4)
%20 = call i32 @write(i32 %1, i8* %4, i32 %19)
%21 = icmp eq i32 %20, %19
br i1 %21, label %38, label %22
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
br label %38
24: ; preds = %14, %30
%25 = phi i32 [ %31, %30 ], [ %15, %14 ]
%26 = call i32 @write(i32 %1, i8* %4, i32 %25)
%27 = icmp eq i32 %26, %25
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %24
%31 = call i32 @read(i32 %12, i8* %4, i32 1024)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %24, label %33
33: ; preds = %30, %14
%34 = phi i32 [ %15, %14 ], [ %31, %30 ]
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %33, %36, %17, %22
ret void
}