#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__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int get_nfds(int *, int);
int /*<<< orphan*/ log_write_err(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int read_write_from_children_end(char *, int, int, int *, int *, char **, int *,
                                 int, int /*<<< orphan*/ *, int *, int *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int write(int, char *, int);
scalar_t__ write_fully(int, char *, int);
int read_write_from_children(int *outfds, int numchildren) {
  int i, nfds;
  fd_set rfds;
  char **buffers = (char **)malloc(sizeof(char *) * numchildren);
  int *buffered = (int *)malloc(sizeof(int) * numchildren);
  for (i = 0; i < numchildren; ++i) {
    buffers[i] = (char *)malloc(sizeof(char) * ((512 > 4096) ? 512 : 4096));
    buffered[i] = 0;
  }
  nfds = get_nfds(outfds, numchildren);
  FD_ZERO(&rfds);
  for (i = 0; i < numchildren; ++i) {
    FD_SET(outfds[i], &rfds);
  }
  while (1) {
    if (select(nfds, &rfds, NULL, NULL, NULL) < 0) {
      perror("Error: could not select child input");
    }
    for (i = 0; i < numchildren; ++i) {
      int curfd = outfds[i];
      if (FD_ISSET(curfd, &rfds)) {
        char *buf = buffers[i];
        int saved = buffered[i];
        while (1) {
          int nlpos;
          char *buf_part_top;
          int data_size =
              read(curfd, buf + saved, ((512 > 4096) ? 512 : 4096) - saved);
          if (data_size <= 0) {
            int rv = read_write_from_children_end(
                buf, saved, data_size, &numchildren, outfds, buffers, buffered,
                curfd, &rfds, &i, &nfds);
            if (rv <= 0) {
              return rv;
            }
            break;
          }
          data_size += saved;
          buf_part_top = buf + data_size;
          while (buf_part_top-- > buf) {
            if (*buf_part_top == 10) {
              break;
            }
          }
          nlpos = buf_part_top - buf;
          if (nlpos < 0) {
            if (write_fully(1, buf, data_size) < 0) {
              return -1;
            }
            saved = 0;
          } else {
            int part_one = nlpos + 1;
            int remaining_count = part_one;
            do {
              int written = write(1, buf, part_one);
              if (written < 0) {
                log_write_err(1);
                return -1;
              }
              buf += written;
              remaining_count -= written;
            } while (remaining_count > 0);
            saved = data_size - part_one;
            memmove(buf, buf + part_one, saved);
            buffered[i] = saved;
            break;
          }
        }
      } else {
        FD_SET(curfd, &rfds);
      }
    }
  }
}


x86 assembly (clang, -O0):
<read_write_from_children>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x90,%rsp
mov    %rdi,-0x20(%rbp)
mov    %esi,-0x24(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1254 <read_write_from_children+0x84>
mov    $0x1000,%edi
call   10a0 <malloc@plt>
mov    %rax,%rdx
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x40(%rbp),%rax
movslq -0x28(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1212 <read_write_from_children+0x42>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <get_nfds@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
call   1080 <FD_ZERO@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12a1 <read_write_from_children+0xd1>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x30(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1273 <read_write_from_children+0xa3>
jmp    12a6 <read_write_from_children+0xd6>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
cmp    $0x0,%rax
jge    12d3 <read_write_from_children+0x103>
lea    0xd32(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    150d <read_write_from_children+0x33d>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     14ee <read_write_from_children+0x31e>
mov    -0x38(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x54(%rbp)
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%rsi
movslq -0x54(%rbp),%rax
add    %rax,%rsi
mov    $0x1000,%edx
sub    -0x54(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jg     13b2 <read_write_from_children+0x1e2>
mov    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    -0x64(%rbp),%edx
mov    -0x20(%rbp),%r8
mov    -0x38(%rbp),%r9
mov    -0x40(%rbp),%r14
mov    -0x44(%rbp),%ebx
lea    -0x24(%rbp),%rcx
lea    -0x30(%rbp),%r11
lea    -0x28(%rbp),%r10
lea    -0x2c(%rbp),%rax
mov    %r14,(%rsp)
mov    %ebx,0x8(%rsp)
mov    %r11,0x10(%rsp)
mov    %r10,0x18(%rsp)
mov    %rax,0x20(%rsp)
call   10f0 <read_write_from_children_end@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
jg     13ad <read_write_from_children+0x1dd>
mov    -0x68(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1512 <read_write_from_children+0x342>
jmp    14e9 <read_write_from_children+0x319>
mov    -0x54(%rbp),%eax
add    -0x64(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x50(%rbp),%rax
movslq -0x64(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x60(%rbp)
cmp    -0x50(%rbp),%rax
jbe    13fd <read_write_from_children+0x22d>
mov    -0x60(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    13f8 <read_write_from_children+0x228>
jmp    13fd <read_write_from_children+0x22d>
jmp    13ca <read_write_from_children+0x1fa>
mov    -0x60(%rbp),%rax
mov    -0x50(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    1448 <read_write_from_children+0x278>
mov    -0x50(%rbp),%rsi
mov    -0x64(%rbp),%edx
mov    $0x1,%edi
call   1050 <write_fully@plt>
cmp    $0x0,%rax
jge    143c <read_write_from_children+0x26c>
movl   $0xffffffff,-0x14(%rbp)
jmp    1512 <read_write_from_children+0x342>
movl   $0x0,-0x54(%rbp)
jmp    14e4 <read_write_from_children+0x314>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
mov    -0x6c(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x6c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    148b <read_write_from_children+0x2bb>
mov    $0x1,%edi
call   10b0 <log_write_err@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    1512 <read_write_from_children+0x342>
mov    -0x74(%rbp),%ecx
mov    -0x50(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x74(%rbp),%ecx
mov    -0x70(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x70(%rbp)
cmpl   $0x0,-0x70(%rbp)
jg     1457 <read_write_from_children+0x287>
mov    -0x64(%rbp),%eax
sub    -0x6c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x50(%rbp),%rsi
movslq -0x6c(%rbp),%rax
add    %rax,%rsi
mov    -0x54(%rbp),%edx
call   10c0 <memmove@plt>
mov    -0x54(%rbp),%edx
mov    -0x40(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    14e9 <read_write_from_children+0x319>
jmp    1328 <read_write_from_children+0x158>
jmp    14fa <read_write_from_children+0x32a>
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10e0 <FD_SET@plt>
jmp    14ff <read_write_from_children+0x32f>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12da <read_write_from_children+0x10a>
jmp    12a6 <read_write_from_children+0xd6>
mov    -0x14(%rbp),%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_write_from_children>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebp
mov    %rdi,%r15
mov    %esi,0x10(%rsp)
lea    0x0(,%rbp,8),%edi
call   10a0 <malloc@plt>
mov    %rax,%r12
lea    0x0(,%rbp,4),%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
movl   $0x0,0x8(%rsp)
test   %ebp,%ebp
jle    124d <read_write_from_children+0x7d>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1000,%edi
call   10a0 <malloc@plt>
movslq %ebx,%rcx
mov    %rax,(%r12,%rcx,8)
movl   $0x0,0x0(%r13,%rcx,4)
mov    0x8(%rsp),%ebx
add    $0x1,%ebx
mov    %ebx,0x8(%rsp)
mov    0x10(%rsp),%ebp
cmp    %ebp,%ebx
jl     1220 <read_write_from_children+0x50>
mov    %r15,%rdi
mov    %ebp,%esi
call   1070 <get_nfds@plt>
mov    %eax,%r14d
mov    %eax,0x14(%rsp)
lea    0xc(%rsp),%rdi
call   1080 <FD_ZERO@plt>
movl   $0x0,0x8(%rsp)
test   %ebp,%ebp
mov    %r12,0x20(%rsp)
mov    %r13,0x18(%rsp)
jle    12b5 <read_write_from_children+0xe5>
mov    %r12,%r13
xor    %ebx,%ebx
lea    0xc(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%edi
mov    %r12,%rsi
call   10e0 <FD_SET@plt>
add    $0x1,%rbx
mov    %ebx,0x8(%rsp)
cmp    %ebp,%ebx
jl     1290 <read_write_from_children+0xc0>
mov    %r13,%r12
jmp    12b5 <read_write_from_children+0xe5>
nopl   (%rax)
mov    0x14(%rsp),%r14d
lea    0xc(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %rax,%rax
js     147a <read_write_from_children+0x2aa>
movl   $0x0,0x8(%rsp)
cmpl   $0x0,0x10(%rsp)
jle    12b0 <read_write_from_children+0xe0>
xor    %eax,%eax
cltq
mov    (%r15,%rax,4),%r13d
mov    %r13d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     13a0 <read_write_from_children+0x1d0>
movslq 0x8(%rsp),%rax
mov    (%r12,%rax,8),%rbp
mov    0x18(%rsp),%rcx
mov    (%rcx,%rax,4),%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%rax,%rbp,1),%rsi
mov    $0x1000,%edx
sub    %eax,%edx
mov    %r13d,%edi
call   1060 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    13b0 <read_write_from_children+0x1e0>
add    %ebx,%r12d
movslq %r12d,%rax
add    %rbp,%rax
xor    %ecx,%ecx
nopl   0x0(%rax,%rax,1)
mov    %rcx,%r14
cmp    %rbp,%rax
jbe    1366 <read_write_from_children+0x196>
cmpb   $0xa,-0x1(%rax)
lea    -0x1(%rax),%rax
lea    0x1(%r14),%rcx
jne    1350 <read_write_from_children+0x180>
mov    %r14d,%eax
not    %eax
add    %r12d,%eax
test   $0x80000000,%eax
je     1400 <read_write_from_children+0x230>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1050 <write_fully@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    1320 <read_write_from_children+0x150>
jmp    1495 <read_write_from_children+0x2c5>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
jmp    1460 <read_write_from_children+0x290>
sub    $0x8,%rsp
mov    %rbp,%rdi
mov    %ebx,%esi
mov    %r12d,%edx
lea    0x18(%rsp),%rcx
mov    %r15,%r8
mov    0x28(%rsp),%r12
mov    %r12,%r9
lea    0x1c(%rsp),%rax
push   %rax
lea    0x18(%rsp),%rax
push   %rax
lea    0x24(%rsp),%rbx
push   %rbx
push   %r13
mov    0x40(%rsp),%r13
push   %r13
call   10f0 <read_write_from_children_end@plt>
add    $0x30,%rsp
test   %eax,%eax
jg     1460 <read_write_from_children+0x290>
jmp    149a <read_write_from_children+0x2ca>
nopl   0x0(%rax)
sub    %r14,%r12
mov    %r12d,%ebx
mov    0x18(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1030 <write@plt>
test   %eax,%eax
js     148b <read_write_from_children+0x2bb>
mov    %eax,%ecx
add    %rcx,%rbp
sub    %eax,%ebx
jg     1410 <read_write_from_children+0x240>
movslq %r12d,%rsi
add    %rbp,%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
call   10c0 <memmove@plt>
movslq 0x8(%rsp),%rax
mov    %r14d,0x0(%r13,%rax,4)
mov    0x20(%rsp),%r12
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%eax
add    $0x1,%eax
mov    %eax,0x8(%rsp)
cmp    0x10(%rsp),%eax
jl     12e6 <read_write_from_children+0x116>
jmp    12b0 <read_write_from_children+0xe0>
lea    0xb7f(%rip),%rdi
call   10d0 <perror@plt>
jmp    12d5 <read_write_from_children+0x105>
mov    $0x1,%edi
call   10b0 <log_write_err@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Error: could not select child input\00"
define i32 @read_write_from_children(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = mul i64 8, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8**
store i8** %26, i8*** %9
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = mul i64 4, %28
%30 = trunc i64 %29 to i32
%31 = call i64 @malloc(i32 %30)
%32 = inttoptr i64 %31 to i32*
store i32* %32, i32** %10
store i32 0, i32* %6
br label %33
33: ; preds = %48, %2
%34 = load i32, i32* %6
%35 = load i32, i32* %5
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %51
37: ; preds = %33
%38 = call i64 @malloc(i32 4096)
%39 = inttoptr i64 %38 to i8*
%40 = load i8**, i8*** %9
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
store i8* %39, i8** %43
%44 = load i32*, i32** %10
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
store i32 0, i32* %47
br label %48
48: ; preds = %37
%49 = load i32, i32* %6
%50 = add nsw i32 %49, 1
store i32 %50, i32* %6
br label %33
51: ; preds = %33
%52 = load i32*, i32** %4
%53 = load i32, i32* %5
%54 = call i32 @get_nfds(i32* %52, i32 %53)
store i32 %54, i32* %7
%55 = call i32 @FD_ZERO(i32* %8)
store i32 0, i32* %6
br label %56
56: ; preds = %67, %51
%57 = load i32, i32* %6
%58 = load i32, i32* %5
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %70
60: ; preds = %56
%61 = load i32*, i32** %4
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32, i32* %61, i64 %63
%65 = load i32, i32* %64
%66 = call i32 @FD_SET(i32 %65, i32* %8)
br label %67
67: ; preds = %60
%68 = load i32, i32* %6
%69 = add nsw i32 %68, 1
store i32 %69, i32* %6
br label %56
70: ; preds = %56
br label %71
71: ; preds = %70, %211
%72 = load i32, i32* %7
%73 = call i64 @select(i32 %72, i32* %8, i32* null, i32* null, i32* null)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %77
77: ; preds = %75, %71
store i32 0, i32* %6
br label %78
78: ; preds = %208, %77
%79 = load i32, i32* %6
%80 = load i32, i32* %5
%81 = icmp slt i32 %79, %80
br i1 %81, label %82, label %211
82: ; preds = %78
%83 = load i32*, i32** %4
%84 = load i32, i32* %6
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %86
store i32 %87, i32* %11
%88 = load i32, i32* %11
%89 = call i64 @FD_ISSET(i32 %88, i32* %8)
%90 = icmp ne i64 %89, 0
br i1 %90, label %91, label %204
91: ; preds = %82
%92 = load i8**, i8*** %9
%93 = load i32, i32* %6
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i8*, i8** %92, i64 %94
%96 = load i8*, i8** %95
store i8* %96, i8** %12
%97 = load i32*, i32** %10
%98 = load i32, i32* %6
%99 = sext i32 %98 to i64
%100 = getelementptr inbounds i32, i32* %97, i64 %99
%101 = load i32, i32* %100
store i32 %101, i32* %13
br label %102
102: ; preds = %91, %202
%103 = load i32, i32* %11
%104 = load i8*, i8** %12
%105 = load i32, i32* %13
%106 = sext i32 %105 to i64
%107 = getelementptr inbounds i8, i8* %104, i64 %106
%108 = load i32, i32* %13
%109 = sub nsw i32 4096, %108
%110 = call i32 @read(i32 %103, i8* %107, i32 %109)
store i32 %110, i32* %16
%111 = load i32, i32* %16
%112 = icmp sle i32 %111, 0
br i1 %112, label %113, label %127
113: ; preds = %102
%114 = load i8*, i8** %12
%115 = load i32, i32* %13
%116 = load i32, i32* %16
%117 = load i32*, i32** %4
%118 = load i8**, i8*** %9
%119 = load i32*, i32** %10
%120 = load i32, i32* %11
%121 = call i32 @read_write_from_children_end(i8* %114, i32 %115, i32 %116, i32* %5, i32* %117, i8** %118, i32* %119, i32 %120, i32* %8, i32* %6, i32* %7)
store i32 %121, i32* %17
%122 = load i32, i32* %17
%123 = icmp sle i32 %122, 0
br i1 %123, label %124, label %126
124: ; preds = %113
%125 = load i32, i32* %17
store i32 %125, i32* %3
br label %212
126: ; preds = %113
br label %203
127: ; preds = %102
%128 = load i32, i32* %13
%129 = load i32, i32* %16
%130 = add nsw i32 %129, %128
store i32 %130, i32* %16
%131 = load i8*, i8** %12
%132 = load i32, i32* %16
%133 = sext i32 %132 to i64
%134 = getelementptr inbounds i8, i8* %131, i64 %133
store i8* %134, i8** %15
br label %135
135: ; preds = %146, %127
%136 = load i8*, i8** %15
%137 = getelementptr inbounds i8, i8* %136, i32 -1
store i8* %137, i8** %15
%138 = load i8*, i8** %12
%139 = icmp ugt i8* %136, %138
br i1 %139, label %140, label %147
140: ; preds = %135
%141 = load i8*, i8** %15
%142 = load i8, i8* %141
%143 = sext i8 %142 to i32
%144 = icmp eq i32 %143, 10
br i1 %144, label %145, label %146
145: ; preds = %140
br label %147
146: ; preds = %140
br label %135
147: ; preds = %145, %135
%148 = load i8*, i8** %15
%149 = load i8*, i8** %12
%150 = ptrtoint i8* %148 to i64
%151 = ptrtoint i8* %149 to i64
%152 = sub i64 %150, %151
%153 = trunc i64 %152 to i32
store i32 %153, i32* %14
%154 = load i32, i32* %14
%155 = icmp slt i32 %154, 0
br i1 %155, label %156, label %163
156: ; preds = %147
%157 = load i8*, i8** %12
%158 = load i32, i32* %16
%159 = call i64 @write_fully(i32 1, i8* %157, i32 %158)
%160 = icmp slt i64 %159, 0
br i1 %160, label %161, label %162
161: ; preds = %156
store i32 -1, i32* %3
br label %212
162: ; preds = %156
store i32 0, i32* %13
br label %202
163: ; preds = %147
%164 = load i32, i32* %14
%165 = add nsw i32 %164, 1
store i32 %165, i32* %18
%166 = load i32, i32* %18
store i32 %166, i32* %19
br label %167
167: ; preds = %183, %163
%168 = load i8*, i8** %12
%169 = load i32, i32* %18
%170 = call i32 @write(i32 1, i8* %168, i32 %169)
store i32 %170, i32* %20
%171 = load i32, i32* %20
%172 = icmp slt i32 %171, 0
br i1 %172, label %173, label %175
173: ; preds = %167
%174 = call i32 @log_write_err(i32 1)
store i32 -1, i32* %3
br label %212
175: ; preds = %167
%176 = load i32, i32* %20
%177 = load i8*, i8** %12
%178 = sext i32 %176 to i64
%179 = getelementptr inbounds i8, i8* %177, i64 %178
store i8* %179, i8** %12
%180 = load i32, i32* %20
%181 = load i32, i32* %19
%182 = sub nsw i32 %181, %180
store i32 %182, i32* %19
br label %183
183: ; preds = %175
%184 = load i32, i32* %19
%185 = icmp sgt i32 %184, 0
br i1 %185, label %167, label %186
186: ; preds = %183
%187 = load i32, i32* %16
%188 = load i32, i32* %18
%189 = sub nsw i32 %187, %188
store i32 %189, i32* %13
%190 = load i8*, i8** %12
%191 = load i8*, i8** %12
%192 = load i32, i32* %18
%193 = sext i32 %192 to i64
%194 = getelementptr inbounds i8, i8* %191, i64 %193
%195 = load i32, i32* %13
%196 = call i32 @memmove(i8* %190, i8* %194, i32 %195)
%197 = load i32, i32* %13
%198 = load i32*, i32** %10
%199 = load i32, i32* %6
%200 = sext i32 %199 to i64
%201 = getelementptr inbounds i32, i32* %198, i64 %200
store i32 %197, i32* %201
br label %203
202: ; preds = %162
br label %102
203: ; preds = %186, %126
br label %207
204: ; preds = %82
%205 = load i32, i32* %11
%206 = call i32 @FD_SET(i32 %205, i32* %8)
br label %207
207: ; preds = %204, %203
br label %208
208: ; preds = %207
%209 = load i32, i32* %6
%210 = add nsw i32 %209, 1
store i32 %210, i32* %6
br label %78
211: ; preds = %78
br label %71
212: ; preds = %173, %161, %124
%213 = load i32, i32* %3
ret i32 %213
}

LLVM IR (-O2):
@.str = [36 x i8] c"Error: could not select child input\00"
define i32 @read_write_from_children(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %1, i32* %3
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = shl i32 %1, 3
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8**
%13 = shl i32 %1, 2
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
store i32 0, i32* %4
%16 = icmp sgt i32 %1, 0
br i1 %16, label %17, label %28
17: ; preds = %2, %17
%18 = phi i32 [ %25, %17 ], [ 0, %2 ]
%19 = tail call i64 @malloc(i32 4096)
%20 = inttoptr i64 %19 to i8*
%21 = sext i32 %18 to i64
%22 = getelementptr inbounds i8*, i8** %12, i64 %21
store i8* %20, i8** %22
%23 = getelementptr inbounds i32, i32* %15, i64 %21
store i32 0, i32* %23
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
%26 = load i32, i32* %3
%27 = icmp slt i32 %25, %26
br i1 %27, label %17, label %28
28: ; preds = %17, %2
%29 = phi i32 [ %1, %2 ], [ %26, %17 ]
%30 = tail call i32 @get_nfds(i32* %0, i32 %29)
store i32 %30, i32* %5
%31 = call i32 @FD_ZERO(i32* %6)
store i32 0, i32* %4
%32 = icmp sgt i32 %29, 0
br i1 %32, label %33, label %41
33: ; preds = %28, %33
%34 = phi i32 [ %39, %33 ], [ 0, %28 ]
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %0, i64 %35
%37 = load i32, i32* %36
%38 = call i32 @FD_SET(i32 %37, i32* %6)
%39 = add nuw nsw i32 %34, 1
store i32 %39, i32* %4
%40 = icmp slt i32 %39, %29
br i1 %40, label %33, label %41
41: ; preds = %33, %28
br label %44
42: ; preds = %122, %50
%43 = load i32, i32* %5
br label %44
44: ; preds = %41, %42
%45 = phi i32 [ %43, %42 ], [ %30, %41 ]
%46 = call i64 @select(i32 %45, i32* %6, i32* null, i32* null, i32* null)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
store i32 0, i32* %4
%51 = load i32, i32* %3
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %42
53: ; preds = %50, %122
%54 = phi i32 [ %124, %122 ], [ 0, %50 ]
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i32, i32* %0, i64 %55
%57 = load i32, i32* %56
%58 = call i64 @FD_ISSET(i32 %57, i32* %6)
%59 = icmp eq i64 %58, 0
br i1 %59, label %120, label %60
60: ; preds = %53
%61 = load i32, i32* %4
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8*, i8** %12, i64 %62
%64 = load i8*, i8** %63
%65 = getelementptr inbounds i32, i32* %15, i64 %62
%66 = load i32, i32* %65
%67 = ptrtoint i8* %64 to i64
br label %68
68: ; preds = %94, %60
%69 = phi i32 [ %66, %60 ], [ 0, %94 ]
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %64, i64 %70
%72 = sub nsw i32 4096, %69
%73 = call i32 @read(i32 %57, i8* %71, i32 %72)
%74 = icmp slt i32 %73, 1
br i1 %74, label %75, label %78
75: ; preds = %68
%76 = call i32 @read_write_from_children_end(i8* %64, i32 %69, i32 %73, i32* %3, i32* %0, i8** %12, i32* %15, i32 %57, i32* %6, i32* %4, i32* %5)
%77 = icmp slt i32 %76, 1
br i1 %77, label %127, label %122
78: ; preds = %68
%79 = add nsw i32 %73, %69
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i8, i8* %64, i64 %80
br label %82
82: ; preds = %86, %78
%83 = phi i8* [ %81, %78 ], [ %84, %86 ]
%84 = getelementptr inbounds i8, i8* %83, i64 -1
%85 = icmp ugt i8* %83, %64
br i1 %85, label %86, label %89
86: ; preds = %82
%87 = load i8, i8* %84
%88 = icmp eq i8 %87, 10
br i1 %88, label %89, label %82
89: ; preds = %86, %82
%90 = ptrtoint i8* %84 to i64
%91 = sub i64 %90, %67
%92 = and i64 %91, 2147483648
%93 = icmp eq i64 %92, 0
br i1 %93, label %97, label %94
94: ; preds = %89
%95 = call i64 @write_fully(i32 1, i8* %64, i32 %79)
%96 = icmp slt i64 %95, 0
br i1 %96, label %127, label %68
97: ; preds = %89
%98 = trunc i64 %91 to i32
%99 = add nuw nsw i32 %98, 1
br label %100
100: ; preds = %107, %97
%101 = phi i8* [ %64, %97 ], [ %109, %107 ]
%102 = phi i32 [ %99, %97 ], [ %110, %107 ]
%103 = call i32 @write(i32 1, i8* %101, i32 %99)
%104 = icmp slt i32 %103, 0
br i1 %104, label %105, label %107
105: ; preds = %100
%106 = call i32 @log_write_err(i32 1)
br label %127
107: ; preds = %100
%108 = zext i32 %103 to i64
%109 = getelementptr inbounds i8, i8* %101, i64 %108
%110 = sub nsw i32 %102, %103
%111 = icmp sgt i32 %110, 0
br i1 %111, label %100, label %112
112: ; preds = %107
%113 = sub nsw i32 %79, %99
%114 = sext i32 %99 to i64
%115 = getelementptr inbounds i8, i8* %109, i64 %114
%116 = call i32 @memmove(i8* %109, i8* %115, i32 %113)
%117 = load i32, i32* %4
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds i32, i32* %15, i64 %118
store i32 %113, i32* %119
br label %122
120: ; preds = %53
%121 = call i32 @FD_SET(i32 %57, i32* %6)
br label %122
122: ; preds = %112, %75, %120
%123 = load i32, i32* %4
%124 = add nsw i32 %123, 1
store i32 %124, i32* %4
%125 = load i32, i32* %3
%126 = icmp slt i32 %124, %125
br i1 %126, label %53, label %42
127: ; preds = %75, %94, %105
%128 = phi i32 [ -1, %105 ], [ -1, %94 ], [ %76, %75 ]
ret i32 %128
}