#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 */
int /*<<< orphan*/ FD_CLR(int, int /*<<< orphan*/ *);
int get_nfds(int *, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ write_fully(int, char *, int);
int read_write_from_children_end(char *buf, int count, int data_size,
                                 int *pnumchildren, int *outfds, char **buffers,
                                 int *buffered, int curfd, fd_set *prfds,
                                 int *pi, int *nfds) {
  int j;
  if (count > 0) {
    if (write_fully(1, buf, count) < 0) {
      return -1;
    }
  }
  if (data_size < 0) {
    perror("Error: Could not read from child stdout");
    return -1;
  }
  for (j = *pi + 1; j < *pnumchildren; ++j) {
    outfds[j - 1] = outfds[j];
    buffers[j - 1] = buffers[j];
    buffered[j - 1] = buffered[j];
  }
  --*pnumchildren;
  if (*pnumchildren == 0) {
    return 0;
  }
  FD_CLR(curfd, prfds);
  *nfds = get_nfds(outfds, *pnumchildren);
  --*pi;
  return 1;
}


x86 assembly (clang, -O0):
<read_write_from_children_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x30(%rbp),%rax
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11a7 <read_write_from_children_end+0x67>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write_fully@plt>
cmp    $0x0,%rax
jge    11a2 <read_write_from_children_end+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <read_write_from_children_end+0x15e>
jmp    11a7 <read_write_from_children_end+0x67>
cmpl   $0x0,-0x18(%rbp)
jge    11c9 <read_write_from_children_end+0x89>
lea    0xe48(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <read_write_from_children_end+0x15e>
mov    0x28(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%eax
jge    1245 <read_write_from_children_end+0x105>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x28(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    0x10(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    0x10(%rbp),%rax
mov    -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11d5 <read_write_from_children_end+0x95>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1269 <read_write_from_children_end+0x129>
movl   $0x0,-0x4(%rbp)
jmp    129e <read_write_from_children_end+0x15e>
mov    0x18(%rbp),%edi
mov    0x20(%rbp),%rsi
call   1060 <FD_CLR@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1040 <get_nfds@plt>
mov    %eax,%ecx
mov    0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    0x28(%rbp),%rax
mov    (%rax),%ecx
add    $0xffffffff,%ecx
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_write_from_children_end>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r9,%rbx
mov    %r8,%r15
mov    %rcx,%r14
mov    %edx,%ebp
mov    %esi,%edx
test   %esi,%esi
jle    116e <read_write_from_children_end+0x2e>
mov    %rdi,%rsi
mov    $0x1,%edi
call   1030 <write_fully@plt>
test   %rax,%rax
js     11fa <read_write_from_children_end+0xba>
test   %ebp,%ebp
js     11ee <read_write_from_children_end+0xae>
mov    0x48(%rsp),%rbp
mov    0x0(%rbp),%eax
lea    0x1(%rax),%edx
mov    (%r14),%esi
cmp    %esi,%edx
jge    11b7 <read_write_from_children_end+0x77>
mov    0x30(%rsp),%rcx
movslq %edx,%rdx
mov    (%r15,%rdx,4),%esi
cltq
mov    %esi,(%r15,%rax,4)
mov    (%rbx,%rdx,8),%rsi
mov    %rsi,(%rbx,%rax,8)
mov    (%rcx,%rdx,4),%esi
mov    %esi,(%rcx,%rax,4)
add    $0x1,%rdx
movslq (%r14),%rsi
add    $0x1,%eax
cmp    %rsi,%rdx
jl     1190 <read_write_from_children_end+0x50>
add    $0xffffffff,%esi
mov    %esi,(%r14)
je     11ea <read_write_from_children_end+0xaa>
mov    0x50(%rsp),%rbx
mov    0x40(%rsp),%rsi
mov    0x38(%rsp),%edi
call   1060 <FD_CLR@plt>
mov    (%r14),%esi
mov    %r15,%rdi
call   1040 <get_nfds@plt>
mov    %eax,(%rbx)
addl   $0xffffffff,0x0(%rbp)
mov    $0x1,%eax
jmp    11ff <read_write_from_children_end+0xbf>
xor    %eax,%eax
jmp    11ff <read_write_from_children_end+0xbf>
lea    0xe0b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"Error: Could not read from child stdout\00"
define i32 @read_write_from_children_end(i8* %0, i32 %1, i32 %2, i32* %3, i32* %4, i8** %5, i32* %6, i32 %7, i32* %8, i32* %9, i32* %10) {
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca i8**
%19 = alloca i32*
%20 = alloca i32
%21 = alloca i32*
%22 = alloca i32*
%23 = alloca i32*
%24 = alloca i32
store i8* %0, i8** %13
store i32 %1, i32* %14
store i32 %2, i32* %15
store i32* %3, i32** %16
store i32* %4, i32** %17
store i8** %5, i8*** %18
store i32* %6, i32** %19
store i32 %7, i32* %20
store i32* %8, i32** %21
store i32* %9, i32** %22
store i32* %10, i32** %23
%25 = load i32, i32* %14
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %11
%28 = load i8*, i8** %13
%29 = load i32, i32* %14
%30 = call i64 @write_fully(i32 1, i8* %28, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 -1, i32* %12
br label %102
33: ; preds = %27
br label %34
34: ; preds = %33, %11
%35 = load i32, i32* %15
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %12
br label %102
39: ; preds = %34
%40 = load i32*, i32** %22
%41 = load i32, i32* %40
%42 = add nsw i32 %41, 1
store i32 %42, i32* %24
br label %43
43: ; preds = %79, %39
%44 = load i32, i32* %24
%45 = load i32*, i32** %16
%46 = load i32, i32* %45
%47 = icmp slt i32 %44, %46
br i1 %47, label %48, label %82
48: ; preds = %43
%49 = load i32*, i32** %17
%50 = load i32, i32* %24
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = load i32*, i32** %17
%55 = load i32, i32* %24
%56 = sub nsw i32 %55, 1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i32, i32* %54, i64 %57
store i32 %53, i32* %58
%59 = load i8**, i8*** %18
%60 = load i32, i32* %24
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8*, i8** %59, i64 %61
%63 = load i8*, i8** %62
%64 = load i8**, i8*** %18
%65 = load i32, i32* %24
%66 = sub nsw i32 %65, 1
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8*, i8** %64, i64 %67
store i8* %63, i8** %68
%69 = load i32*, i32** %19
%70 = load i32, i32* %24
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i32, i32* %69, i64 %71
%73 = load i32, i32* %72
%74 = load i32*, i32** %19
%75 = load i32, i32* %24
%76 = sub nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i32, i32* %74, i64 %77
store i32 %73, i32* %78
br label %79
79: ; preds = %48
%80 = load i32, i32* %24
%81 = add nsw i32 %80, 1
store i32 %81, i32* %24
br label %43
82: ; preds = %43
%83 = load i32*, i32** %16
%84 = load i32, i32* %83
%85 = add nsw i32 %84, -1
store i32 %85, i32* %83
%86 = load i32*, i32** %16
%87 = load i32, i32* %86
%88 = icmp eq i32 %87, 0
br i1 %88, label %89, label %90
89: ; preds = %82
store i32 0, i32* %12
br label %102
90: ; preds = %82
%91 = load i32, i32* %20
%92 = load i32*, i32** %21
%93 = call i32 @FD_CLR(i32 %91, i32* %92)
%94 = load i32*, i32** %17
%95 = load i32*, i32** %16
%96 = load i32, i32* %95
%97 = call i32 @get_nfds(i32* %94, i32 %96)
%98 = load i32*, i32** %23
store i32 %97, i32* %98
%99 = load i32*, i32** %22
%100 = load i32, i32* %99
%101 = add nsw i32 %100, -1
store i32 %101, i32* %99
store i32 1, i32* %12
br label %102
102: ; preds = %90, %89, %37, %32
%103 = load i32, i32* %12
ret i32 %103
}

LLVM IR (-O2):
@.str = [40 x i8] c"Error: Could not read from child stdout\00"
define i32 @read_write_from_children_end(i8* %0, i32 %1, i32 %2, i32* nocapture %3, i32* %4, i8** nocapture %5, i32* nocapture %6, i32 %7, i32* %8, i32* nocapture %9, i32* nocapture writeonly %10) {
%12 = icmp sgt i32 %1, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = tail call i64 @write_fully(i32 1, i8* %0, i32 %1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %55, label %16
16: ; preds = %13, %11
%17 = icmp slt i32 %2, 0
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %55
20: ; preds = %16
%21 = load i32, i32* %9
%22 = add i32 %21, 1
%23 = load i32, i32* %3
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %45
25: ; preds = %20
%26 = sext i32 %22 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ %26, %25 ], [ %40, %27 ]
%29 = phi i32 [ %21, %25 ], [ %44, %27 ]
%30 = getelementptr inbounds i32, i32* %4, i64 %28
%31 = load i32, i32* %30
%32 = sext i32 %29 to i64
%33 = getelementptr inbounds i32, i32* %4, i64 %32
store i32 %31, i32* %33
%34 = getelementptr inbounds i8*, i8** %5, i64 %28
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8*, i8** %5, i64 %32
store i8* %35, i8** %36
%37 = getelementptr inbounds i32, i32* %6, i64 %28
%38 = load i32, i32* %37
%39 = getelementptr inbounds i32, i32* %6, i64 %32
store i32 %38, i32* %39
%40 = add nsw i64 %28, 1
%41 = load i32, i32* %3
%42 = sext i32 %41 to i64
%43 = icmp slt i64 %40, %42
%44 = trunc i64 %28 to i32
br i1 %43, label %27, label %45
45: ; preds = %27, %20
%46 = phi i32 [ %23, %20 ], [ %41, %27 ]
%47 = add nsw i32 %46, -1
store i32 %47, i32* %3
%48 = icmp eq i32 %47, 0
br i1 %48, label %55, label %49
49: ; preds = %45
%50 = tail call i32 @FD_CLR(i32 %7, i32* %8)
%51 = load i32, i32* %3
%52 = tail call i32 @get_nfds(i32* %4, i32 %51)
store i32 %52, i32* %10
%53 = load i32, i32* %9
%54 = add nsw i32 %53, -1
store i32 %54, i32* %9
br label %55
55: ; preds = %45, %13, %49, %18
%56 = phi i32 [ -1, %18 ], [ 1, %49 ], [ -1, %13 ], [ 0, %45 ]
ret i32 %56
}