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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void handle_request(int msgsock, int child_to_parent) {
  char buffer[1024];
  int sz;
  int ret;
  fd_set set;
  while (1) {
    FD_ZERO(&set);
    FD_SET(msgsock, &set);
    if ((ret = select(FD_SETSIZE, &set, NULL, NULL, NULL)) < 0) {
      if (errno == EINTR) {
        continue;
      } else {
        perror("select");
        exit(-1);
      }
    }
    if (FD_ISSET(msgsock, &set)) {
      memset(buffer, 0, 1024);
      sz = read(msgsock, buffer, 1024);
      if (sz <= 0) {
        break;
      }
      printf("RECV: [%s]\n", buffer);
      write(msgsock, buffer, strlen(buffer));
      write(child_to_parent, buffer, strlen(buffer));
    }
  }
  printf("Client disconnected\n");
  close(msgsock);
}


x86 assembly (clang, -O0):
<handle_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x41c(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x41c(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    -0x41c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
mov    %eax,-0x418(%rbp)
cmp    $0x0,%eax
jge    1250 <handle_request+0x90>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <handle_request+0x7a>
jmp    11d1 <handle_request+0x11>
lea    0xdbf(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x41c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     132b <handle_request+0x16b>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     12a8 <handle_request+0xe8>
jmp    1330 <handle_request+0x170>
lea    -0x410(%rbp),%rsi
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x42c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11d1 <handle_request+0x11>
lea    0xcdc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0xc(%rsp),%rbp
mov    0x2dee(%rip),%r13
lea    0x10(%rsp),%r15
lea    0xe09(%rip),%r12
jmp    121a <handle_request+0x4a>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    12d4 <handle_request+0x104>
mov    %rbp,%rdi
call   10b0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10f0 <FD_SET@plt>
mov    0x0(%r13),%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <select@plt>
test   %eax,%eax
js     1200 <handle_request+0x30>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     121a <handle_request+0x4a>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   10a0 <read@plt>
test   %eax,%eax
jle    12af <handle_request+0xdf>
mov    %r12,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
jmp    121a <handle_request+0x4a>
lea    0xd5d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd25(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xffffffff,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [7 x i8] c"select\00"
@.str.1 = [12 x i8] c"RECV: [%s]\0A\00"
@.str.2 = [21 x i8] c"Client disconnected\0A\00"
define void @handle_request(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %9
9: ; preds = %2, %20, %50
%10 = call i32 @FD_ZERO(i32* %8)
%11 = load i32, i32* %3
%12 = call i32 @FD_SET(i32 %11, i32* %8)
%13 = load i32, i32* @FD_SETSIZE
%14 = call i32 @select(i32 %13, i32* %8, i32* null, i32* null, i32* null)
store i32 %14, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %9
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16
br label %9
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %9
%25 = load i32, i32* %3
%26 = call i64 @FD_ISSET(i32 %25, i32* %8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %50
28: ; preds = %24
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 1024)
%31 = load i32, i32* %3
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 1024)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp sle i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %28
br label %51
37: ; preds = %28
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %3
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @write(i32 %40, i8* %41, i32 %43)
%45 = load i32, i32* %4
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = call i32 @write(i32 %45, i8* %46, i32 %48)
br label %50
50: ; preds = %37, %24
br label %9
51: ; preds = %36
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* %3
%54 = call i32 @close(i32 %53)
ret void
}

LLVM IR (-O2):
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [7 x i8] c"select\00"
@.str.1 = [12 x i8] c"RECV: [%s]\0A\00"
@str = [20 x i8] c"Client disconnected\00"
define void @handle_request(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
br label %7
7: ; preds = %23, %2
%8 = call i32 @FD_ZERO(i32* %4)
%9 = call i32 @FD_SET(i32 %0, i32* %4)
%10 = load i32, i32* @FD_SETSIZE
%11 = call i32 @select(i32 %10, i32* %4, i32* null, i32* null, i32* null)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %7
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %23, label %17
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %7
%21 = call i64 @FD_ISSET(i32 %0, i32* %4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20, %28, %13
br label %7
24: ; preds = %20
%25 = call i32 @memset(i8* %5, i32 0, i32 1024)
%26 = call i32 @read(i32 %0, i8* %5, i32 1024)
%27 = icmp slt i32 %26, 1
br i1 %27, label %34, label %28
28: ; preds = %24
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%30 = call i32 @strlen(i8* %5)
%31 = call i32 @write(i32 %0, i8* %5, i32 %30)
%32 = call i32 @strlen(i8* %5)
%33 = call i32 @write(i32 %1, i8* %5, i32 %32)
br label %23
34: ; preds = %24
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%36 = call i32 @close(i32 %0)
ret void
}