#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 /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
void client(int readfd, int writefd) {
  char buff[1024];
  int n;
  printf("Client: Enter file name: ");
  if (fgets(buff, 1024, stdin) == NULL)
    printf("Client: filename read error\n");
  n = strlen(buff);
  if (buff[n - 1] == '\n')
    n--;
  if (write(writefd, buff, n) != n)
    printf("Client: filename write error\n");
  while ((n = read(readfd, buff, 1024)) > 0)
    if (write(1, buff, n) != n)
      printf("Client: data write error\n");
  if (n < 0)
    printf("client: data read error\n");
}


x86 assembly (clang, -O0):
<client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xe98(%rip),%rdi
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    119d <client+0x4d>
lea    0xe82(%rip),%rdi
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11da <client+0x8a>
mov    -0x414(%rbp),%eax
add    $0xffffffff,%eax
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     1207 <client+0xb7>
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
jmp    120c <client+0xbc>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    1263 <client+0x113>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     125e <client+0x10e>
lea    0xdfc(%rip),%rdi
call   1050 <printf@plt>
jmp    120c <client+0xbc>
cmpl   $0x0,-0x414(%rbp)
jge    127c <client+0x12c>
lea    0xdf8(%rip),%rdi
call   1050 <printf@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xe86(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
test   %rax,%rax
jne    11a6 <client+0x46>
lea    0xec7(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbp
lea    -0x1(%rbp),%rax
cmpb   $0xa,-0x1(%rsp,%rbp,1)
cmove  %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     11dd <client+0x7d>
lea    0xe73(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1233 <client+0xd3>
mov    %rsp,%rbp
lea    0xe35(%rip),%r14
jmp    1216 <client+0xb6>
nop
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1233 <client+0xd3>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
cmp    %ebx,%eax
je     1200 <client+0xa0>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    1200 <client+0xa0>
test   %ebx,%ebx
jns    1243 <client+0xe3>
lea    0xddc(%rip),%rdi
call   1030 <puts@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Client: Enter file name: \00"
@stdin = global i32 0
@.str.1 = [29 x i8] c"Client: filename read error\0A\00"
@.str.2 = [30 x i8] c"Client: filename write error\0A\00"
@.str.3 = [26 x i8] c"Client: data write error\0A\00"
@.str.4 = [25 x i8] c"client: data read error\0A\00"
define void @client(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32* @fgets(i8* %8, i32 1024, i32 %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = load i32, i32* %6
%26 = add nsw i32 %25, -1
store i32 %26, i32* %6
br label %27
27: ; preds = %24, %14
%28 = load i32, i32* %4
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = load i32, i32* %6
%31 = call i32 @write(i32 %28, i8* %29, i32 %30)
%32 = load i32, i32* %6
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %27
br label %37
37: ; preds = %50, %36
%38 = load i32, i32* %3
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 1024)
store i32 %40, i32* %6
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %37
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = load i32, i32* %6
%45 = call i32 @write(i32 1, i8* %43, i32 %44)
%46 = load i32, i32* %6
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %42
%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, %42
br label %37
51: ; preds = %37
%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 ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Client: Enter file name: \00"
@stdin = global i32 0
@str = [24 x i8] c"client: data read error\00"
@str.5 = [25 x i8] c"Client: data write error\00"
@str.6 = [29 x i8] c"Client: filename write error\00"
@str.7 = [28 x i8] c"Client: filename read error\00"
define void @client(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @stdin
%7 = call i32* @fgets(i8* %4, i32 1024, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 @strlen(i8* %4)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
%18 = select i1 %17, i32 %13, i32 %12
%19 = call i32 @write(i32 %1, i8* %4, i32 %18)
%20 = icmp eq i32 %19, %18
br i1 %20, label %23, label %21
21: ; preds = %11
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.6, i64 0, i64 0))
br label %23
23: ; preds = %21, %11
%24 = call i32 @read(i32 %0, i8* %4, i32 1024)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %35
26: ; preds = %23, %32
%27 = phi i32 [ %33, %32 ], [ %24, %23 ]
%28 = call i32 @write(i32 1, i8* %4, i32 %27)
%29 = icmp eq i32 %28, %27
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = call i32 @read(i32 %0, i8* %4, i32 1024)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %26, label %35
35: ; preds = %32, %23
%36 = phi i32 [ %24, %23 ], [ %33, %32 ]
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
ret void
}