#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_n(int, unsigned char *, scalar_t__);
void client_process(int max, fd_set *set, int remotefd, int localfd) {
  unsigned char buffer[1024] = {0};
  ssize_t readen;
  if (FD_ISSET(localfd, set)) {
    readen = read(localfd, buffer, sizeof(buffer));
    if (readen > 0)
      write_n(remotefd, buffer, readen);
  }
  if (FD_ISSET(remotefd, set)) {
    readen = read(remotefd, buffer, sizeof(buffer));
    if (readen > 0)
      write_n(localfd, buffer, readen);
    else {
      fprintf(stderr, "read error\n");
      close(remotefd);
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<client_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11f5 <client_process+0x85>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jle    11f0 <client_process+0x80>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1080 <write_n@plt>
jmp    11f5 <client_process+0x85>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     127b <client_process+0x10b>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jle    124f <client_process+0xdf>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x428(%rbp),%rdx
call   1080 <write_n@plt>
jmp    1276 <client_process+0x106>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    127b <client_process+0x10b>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_process>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %ecx,%ebp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rsp,%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11c3 <client_process+0x53>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    11c3 <client_process+0x53>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    %rax,%rdx
call   1080 <write_n@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11f5 <client_process+0x85>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1201 <client_process+0x91>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1080 <write_n@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdef(%rip),%rsi
call   1070 <fprintf@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"read error\0A\00"
define void @client_process(i32 %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i64
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = bitcast [1024 x i8]* %9 to i8*
%12 = load i32, i32* %8
%13 = load i32*, i32** %6
%14 = call i64 @FD_ISSET(i32 %12, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %4
%17 = load i32, i32* %8
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%19 = call i64 @read(i32 %17, i8* %18, i32 1024)
store i64 %19, i64* %10
%20 = load i64, i64* %10
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = load i32, i32* %7
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%25 = load i64, i64* %10
%26 = call i32 @write_n(i32 %23, i8* %24, i64 %25)
br label %27
27: ; preds = %22, %16
br label %28
28: ; preds = %27, %4
%29 = load i32, i32* %7
%30 = load i32*, i32** %6
%31 = call i64 @FD_ISSET(i32 %29, i32* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %51
33: ; preds = %28
%34 = load i32, i32* %7
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%36 = call i64 @read(i32 %34, i8* %35, i32 1024)
store i64 %36, i64* %10
%37 = load i64, i64* %10
%38 = icmp sgt i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %33
%40 = load i32, i32* %8
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%42 = load i64, i64* %10
%43 = call i32 @write_n(i32 %40, i8* %41, i64 %42)
br label %50
44: ; preds = %33
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%47 = load i32, i32* %7
%48 = call i32 @close(i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %39
br label %51
51: ; preds = %50, %28
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"read error\0A\00"
define void @client_process(i32 %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = tail call i64 @FD_ISSET(i32 %3, i32* %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = call i64 @read(i32 %3, i8* %6, i32 1024)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @write_n(i32 %2, i8* %6, i64 %10)
br label %14
14: ; preds = %9, %12, %4
%15 = call i64 @FD_ISSET(i32 %2, i32* %1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %14
%18 = call i64 @read(i32 %2, i8* %6, i32 1024)
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @write_n(i32 %3, i8* %6, i64 %18)
br label %27
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @close(i32 %2)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20, %14
ret void
}