#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;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int write(int, char *, size_t);
void server(int readfd, int writefd) {
  int fd;
  ssize_t n;
  char buf[1024];
  if ((n = read(readfd, buf, 1024)) <= 0) {
    printf("%s\n", strerror(errno));
    exit(1);
  }
  buf[n] = '\0';
  if (write(writefd, buf, n) == -1) {
    printf("%s\n", strerror(errno));
    exit(1);
  }
  close(readfd);
  close(writefd);
}


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   1060 <read@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
ja     11ba <server+0x5a>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1209 <server+0xa9>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
je     11ac <server+0x4c>
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11ac <server+0x4c>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
add    $0x408,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <close@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3f(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%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 [1024 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 1024)
store i64 %10, i64* %6
%11 = icmp ule i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = load i64, i64* %6
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %18
store i8 0, i8* %19
%20 = load i32, i32* %4
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%22 = load i64, i64* %6
%23 = call i32 @write(i32 %20, i8* %21, i64 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %17
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
@errno = global i32 0
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 i64 @read(i32 %0, i8* %4, i32 1024)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %5
store i8 0, i8* %13
%14 = call i32 @write(i32 %1, i8* %4, i64 %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = tail call i32 @close(i32 %0)
%23 = tail call i32 @close(i32 %1)
ret void
}