#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 STDOUT_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stdin;
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
int write(int, char *, size_t);
void client(int readfd, int writefd) {
  size_t len;
  ssize_t n;
  char buff[(100)] = {0};
  fgets(buff, (100), stdin);
  len = strlen(buff);
  if ('\n' == buff[len - 1]) {
    --len;
  }
  if (-1 == write(writefd, buff, len)) {
    printf("write error: %s", strerror(errno));
  }
  while (0 < (n = read(readfd, buff, (100)))) {
    if (-1 == write(STDOUT_FILENO, buff, n)) {
      printf("write error: %s", strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1080 <fgets@plt>
lea    -0x80(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
movsbl -0x80(%rbp,%rax,1),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    11db <client+0x6b>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1217 <client+0xa7>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdee(%rip),%rdi
call   1050 <printf@plt>
jmp    121c <client+0xac>
mov    -0x4(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1070 <read@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jae    1286 <client+0x116>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    -0x80(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1281 <client+0x111>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd84(%rip),%rdi
call   1050 <printf@plt>
jmp    121c <client+0xac>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
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)
movl   $0x0,0x60(%rsp)
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    -0x1(%rax),%rdx
cmpb   $0xa,-0x1(%rsp,%rax,1)
cmovne %rax,%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11f4 <client+0x94>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe14(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x64,%edx
call   1060 <read@plt>
test   %rax,%rax
je     126d <client+0x10d>
mov    0x2de1(%rip),%r15
mov    %rsp,%rbx
mov    0x2db7(%rip),%r12
lea    0xde0(%rip),%r14
jmp    1244 <client+0xe4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
test   %rax,%rax
je     126d <client+0x10d>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1230 <client+0xd0>
mov    (%r12),%edi
call   1080 <strerror@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
jmp    1230 <client+0xd0>
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stdin = global i32 0
@.str = [16 x i8] c"write error: %s\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
define void @client(i32 %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %4, i32 100, i32 %5)
%7 = call i64 @strlen(i8* %4)
%8 = add i64 %7, -1
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 10
%12 = select i1 %11, i64 %8, i64 %7
%13 = call i32 @write(i32 %1, i8* %4, i64 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %15, %2
%20 = call i64 @read(i32 %0, i8* %4, i32 100)
%21 = icmp eq i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %19, %31
%23 = phi i64 [ %32, %31 ], [ %20, %19 ]
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @write(i32 %24, i8* %4, i64 %23)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %27, %22
%32 = call i64 @read(i32 %0, i8* %4, i32 100)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %22
34: ; preds = %31, %19
ret void
}