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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int getpid();
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*/ write(int, int *, int);
void child_code(int *parent_to_child, int *child_to_parent) {
  int pid;
  close(child_to_parent[0]);
  pid = getpid();
  write(child_to_parent[1], &pid, sizeof(pid));
  printf("SENDING %d TO PARENT\n", pid);
  close(parent_to_child[1]);
  fd_set set;
  int ret;
  char msg[1024];
  FD_ZERO(&set);
  FD_SET(parent_to_child[0], &set);
  while (1) {
    ret = select(FD_SETSIZE, &set, NULL, NULL, NULL);
    if (ret > 0) {
      ret = read(parent_to_child[0], msg, 1024);
      if (ret > 0) {
        printf("CHILD RECV: [%s]\n", msg);
      } else {
        break;
      }
    } else {
      perror("select");
    }
  }
}


x86 assembly (clang, -O0):
<child_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%esi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
lea    -0x18(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1274 <child_code+0xe4>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    126a <child_code+0xda>
lea    -0x420(%rbp),%rsi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126f <child_code+0xdf>
jmp    1285 <child_code+0xf5>
jmp    1280 <child_code+0xf0>
lea    0xdad(%rip),%rdi
call   10a0 <perror@plt>
jmp    1202 <child_code+0x72>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child_code>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    %rdi,%r15
mov    (%rsi),%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
mov    %eax,0x8(%rsp)
mov    0x4(%rbx),%edi
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
lea    0xe2c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x4(%r15),%edi
call   1060 <close@plt>
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1080 <FD_ZERO@plt>
mov    (%r15),%edi
mov    %r14,%rsi
call   10b0 <FD_SET@plt>
mov    0x2ddb(%rip),%rbp
lea    0x10(%rsp),%rbx
lea    0xe05(%rip),%r12
lea    0xe10(%rip),%r13
jmp    1230 <child_code+0xa0>
mov    %r13,%rdi
call   10a0 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%rbp),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %eax,%eax
jle    121a <child_code+0x8a>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    1269 <child_code+0xd9>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1230 <child_code+0xa0>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [22 x i8] c"SENDING %d TO PARENT\0A\00"
@FD_SETSIZE = global i32 0
@.str.1 = [18 x i8] c"CHILD RECV: [%s]\0A\00"
@.str.2 = [7 x i8] c"select\00"
define void @child_code(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* %1
%8 = tail call i32 @close(i32 %7)
%9 = tail call i32 (...) @getpid()
store i32 %9, i32* %3
%10 = getelementptr inbounds i32, i32* %1, i64 1
%11 = load i32, i32* %10
%12 = call i32 @write(i32 %11, i32* %3, i32 4)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %9)
%14 = getelementptr inbounds i32, i32* %0, i64 1
%15 = load i32, i32* %14
%16 = tail call i32 @close(i32 %15)
%17 = bitcast i32* %4 to i8*
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = call i32 @FD_ZERO(i32* %4)
%20 = load i32, i32* %0
%21 = call i32 @FD_SET(i32 %20, i32* %4)
br label %22
22: ; preds = %34, %2
%23 = load i32, i32* @FD_SETSIZE
%24 = call i32 @select(i32 %23, i32* %4, i32* null, i32* null, i32* null)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* %0
%28 = call i32 @read(i32 %27, i8* %18, i32 1024)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %34
32: ; preds = %22
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
br label %22
35: ; preds = %26
ret void
}