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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ ECONNRESET;
scalar_t__ EINTR;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int write(int /*<<< orphan*/, char *, int);
void process_client_data(int clifd) {
  char buf[1024];
  ssize_t nbytes;
  int done = 0;
  while (1) {
    memset(buf, 0, sizeof(buf));
    nbytes = read(clifd, buf, sizeof(buf) - 1);
    if (nbytes == -1) {
      if (errno == EAGAIN) {
        done = 1;
        break;
      } else if (errno == ECONNRESET) {
        break;
      } else if (errno == EINTR) {
        continue;
      } else {
        perror("read() failed");
        break;
      }
    } else if (nbytes == 0) {
      printf("client closed\n");
      break;
    }
    if (write(STDIN_FILENO, buf, nbytes) != nbytes)
      perror("write() failed");
  }
}


x86 assembly (clang, -O0):
<process_client_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0xffffffff,-0x414(%rbp)
jne    121a <process_client_data+0xca>
mov    0x2e2f(%rip),%rax
mov    (%rax),%rax
mov    0x2e05(%rip),%rcx
cmp    (%rcx),%rax
jne    11cb <process_client_data+0x7b>
movl   $0x1,-0x418(%rbp)
jmp    1275 <process_client_data+0x125>
mov    0x2e06(%rip),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
jne    11ea <process_client_data+0x9a>
jmp    1275 <process_client_data+0x125>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2dd5(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <process_client_data+0xb9>
jmp    1168 <process_client_data+0x18>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
jmp    1275 <process_client_data+0x125>
cmpl   $0x0,-0x414(%rbp)
jne    1238 <process_client_data+0xe8>
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
jmp    1275 <process_client_data+0x125>
jmp    123d <process_client_data+0xed>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     1270 <process_client_data+0x120>
lea    0xdb2(%rip),%rdi
call   1070 <perror@plt>
jmp    1168 <process_client_data+0x18>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_client_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r15d
mov    %rsp,%rbx
mov    0x2e4a(%rip),%r13
mov    0x2e6b(%rip),%r14
mov    0x2e4c(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11d0 <process_client_data+0x80>
mov    %eax,%ebp
test   %eax,%eax
je     11ff <process_client_data+0xaf>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
cmp    %ebp,%eax
je     1180 <process_client_data+0x30>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
jmp    1180 <process_client_data+0x30>
nopw   0x0(%rax,%rax,1)
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     120b <process_client_data+0xbb>
cmp    (%r14),%rax
je     120b <process_client_data+0xbb>
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <process_client_data+0x30>
lea    0xe08(%rip),%rdi
call   1070 <perror@plt>
jmp    120b <process_client_data+0xbb>
lea    0xe17(%rip),%rdi
call   1030 <puts@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@ECONNRESET = global i64 0
@EINTR = global i64 0
@.str = [14 x i8] c"read() failed\00"
@.str.1 = [15 x i8] c"client closed\0A\00"
@STDIN_FILENO = global i32 0
@.str.2 = [15 x i8] c"write() failed\00"
define void @process_client_data(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
br label %6
6: ; preds = %1, %28, %46
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 1024)
%9 = load i32, i32* %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 1023)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %31
14: ; preds = %6
%15 = load i64, i64* @errno
%16 = load i64, i64* @EAGAIN
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14
store i32 1, i32* %5
br label %47
19: ; preds = %14
%20 = load i64, i64* @errno
%21 = load i64, i64* @ECONNRESET
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %47
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
br label %6
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %47
31: ; preds = %6
%32 = load i32, i32* %4
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %47
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i32, i32* @STDIN_FILENO
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%40 = load i32, i32* %4
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %4
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %37
br label %6
47: ; preds = %34, %29, %23, %18
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@ECONNRESET = global i64 0
@EINTR = global i64 0
@.str = [14 x i8] c"read() failed\00"
@STDIN_FILENO = global i32 0
@.str.2 = [15 x i8] c"write() failed\00"
@str = [14 x i8] c"client closed\00"
define void @process_client_data(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %25, %1
%5 = call i32 @memset(i8* %3, i32 0, i32 1024)
%6 = call i32 @read(i32 %0, i8* %3, i32 1023)
switch i32 %6, label %21 [
i32 -1, label %7
i32 0, label %19
]
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EAGAIN
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @ECONNRESET
%12 = icmp eq i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %28, label %14
14: ; preds = %7
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %8, %15
br i1 %16, label %25, label %17
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %28
19: ; preds = %4
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %28
21: ; preds = %4
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @write(i32 %22, i8* %3, i32 %6)
%24 = icmp eq i32 %23, %6
br i1 %24, label %25, label %26
25: ; preds = %21, %26, %14
br label %4
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %25
28: ; preds = %7, %19, %17
ret void
}