#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 pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int STDERR_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char const *, char *const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ wait(int *);
int spawn_sync_read(const char *filename, const char *const argv[], int max,
                    char *out) {
  pid_t pid;
  int fd[2];
  int status;
  int size;
  int eof;
  if (pipe(fd) != 0) {
    return -1;
  }
  pid = fork();
  if (pid == -1) {
    return -1;
  }
  if (pid == 0) {
    close(fd[0]);
    close(STDERR_FILENO);
    dup2(fd[1], STDOUT_FILENO);
    exit(execvp(filename, (char *const *)argv));
  }
  close(fd[1]);
  for (--max, size = 0, eof = 0; !eof;) {
    char buf[512];
    int chunk = read(fd[0], buf, sizeof(buf));
    if (chunk < 0) {
      break;
    }
    eof = (chunk < (int)sizeof(buf));
    if (size + chunk > max) {
      chunk = max - size;
    }
    memcpy(&out[size], buf, chunk);
    size += chunk;
  }
  out[size] = '\0';
  close(fd[0]);
  wait(&status);
  return size;
}


x86 assembly (clang, -O0):
<spawn_sync_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x34(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11c9 <spawn_sync_read+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <spawn_sync_read+0x183>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e9 <spawn_sync_read+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    1313 <spawn_sync_read+0x183>
cmpl   $0x0,-0x2c(%rbp)
jne    122e <spawn_sync_read+0x9e>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <execvp@plt>
mov    %eax,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1263 <spawn_sync_read+0xd3>
jmp    12f0 <spawn_sync_read+0x160>
mov    -0x34(%rbp),%edi
lea    -0x240(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0x0,-0x244(%rbp)
jge    128f <spawn_sync_read+0xff>
jmp    12f0 <spawn_sync_read+0x160>
cmpl   $0x200,-0x244(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
add    -0x244(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    12c2 <spawn_sync_read+0x132>
mov    -0x1c(%rbp),%eax
sub    -0x3c(%rbp),%eax
mov    %eax,-0x244(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
lea    -0x240(%rbp),%rsi
mov    -0x244(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x244(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    124d <spawn_sync_read+0xbd>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
lea    -0x38(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spawn_sync_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1255 <spawn_sync_read+0xc5>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1255 <spawn_sync_read+0xc5>
test   %ebp,%ebp
je     1269 <spawn_sync_read+0xd9>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %ebx,%ebx
lea    0x10(%rsp),%r15
nop
mov    0x8(%rsp),%edi
mov    %r15,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1238 <spawn_sync_read+0xa8>
mov    %eax,%r12d
lea    (%r12,%rbx,1),%eax
movslq %ebx,%rbp
not    %ebx
add    %r13d,%ebx
cmp    %r13d,%eax
cmovl  %r12d,%ebx
lea    (%r14,%rbp,1),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <memcpy@plt>
add    %ebx,%ebp
mov    %ebp,%ebx
cmp    $0x1ff,%r12d
ja     11f0 <spawn_sync_read+0x60>
jmp    123a <spawn_sync_read+0xaa>
mov    %ebx,%ebp
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0x10(%rsp),%rdi
call   10a0 <wait@plt>
mov    %ebp,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <execvp@plt>
mov    %eax,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @spawn_sync_read(i8* %0, i8** %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [512 x i8]
%16 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%18 = call i64 @pipe(i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 -1, i32* %5
br label %95
21: ; preds = %4
%22 = call i32 (...) @fork()
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %5
br label %95
26: ; preds = %21
%27 = load i32, i32* %10
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %26
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* @STDERR_FILENO
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%36 = load i32, i32* %35
%37 = load i32, i32* @STDOUT_FILENO
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = load i8*, i8** %6
%40 = load i8**, i8*** %7
%41 = call i32 @execvp(i8* %39, i8** %40)
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %26
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* %8
%48 = add nsw i32 %47, -1
store i32 %48, i32* %8
store i32 0, i32* %13
store i32 0, i32* %14
br label %49
49: ; preds = %74, %43
%50 = load i32, i32* %14
%51 = icmp ne i32 %50, 0
%52 = xor i1 %51, true
br i1 %52, label %53, label %85
53: ; preds = %49
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%55 = load i32, i32* %54
%56 = getelementptr inbounds [512 x i8], [512 x i8]* %15, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 512)
store i32 %57, i32* %16
%58 = load i32, i32* %16
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %53
br label %85
61: ; preds = %53
%62 = load i32, i32* %16
%63 = icmp slt i32 %62, 512
%64 = zext i1 %63 to i32
store i32 %64, i32* %14
%65 = load i32, i32* %13
%66 = load i32, i32* %16
%67 = add nsw i32 %65, %66
%68 = load i32, i32* %8
%69 = icmp sgt i32 %67, %68
br i1 %69, label %70, label %74
70: ; preds = %61
%71 = load i32, i32* %8
%72 = load i32, i32* %13
%73 = sub nsw i32 %71, %72
store i32 %73, i32* %16
br label %74
74: ; preds = %70, %61
%75 = load i8*, i8** %9
%76 = load i32, i32* %13
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i8, i8* %75, i64 %77
%79 = getelementptr inbounds [512 x i8], [512 x i8]* %15, i64 0, i64 0
%80 = load i32, i32* %16
%81 = call i32 @memcpy(i8* %78, i8* %79, i32 %80)
%82 = load i32, i32* %16
%83 = load i32, i32* %13
%84 = add nsw i32 %83, %82
store i32 %84, i32* %13
br label %49
85: ; preds = %60, %49
%86 = load i8*, i8** %9
%87 = load i32, i32* %13
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %86, i64 %88
store i8 0, i8* %89
%90 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%91 = load i32, i32* %90
%92 = call i32 @close(i32 %91)
%93 = call i32 @wait(i32* %12)
%94 = load i32, i32* %13
store i32 %94, i32* %5
br label %95
95: ; preds = %85, %25, %20
%96 = load i32, i32* %5
ret i32 %96
}

LLVM IR (-O2):
@STDERR_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @spawn_sync_read(i8* %0, i8** %1, i32 %2, i8* %3) {
%5 = alloca [2 x i32]
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = bitcast [2 x i32]* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%11 = call i64 @pipe(i32* %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %55
13: ; preds = %4
%14 = call i32 (...) @fork()
switch i32 %14, label %26 [
i32 -1, label %55
i32 0, label %15
]
15: ; preds = %13
%16 = load i32, i32* %10
%17 = call i32 @close(i32 %16)
%18 = load i32, i32* @STDERR_FILENO
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%21 = load i32, i32* %20
%22 = load i32, i32* @STDOUT_FILENO
%23 = call i32 @dup2(i32 %21, i32 %22)
%24 = call i32 @execvp(i8* %0, i8** %1)
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %13
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
br label %31
31: ; preds = %26, %37
%32 = phi i32 [ 0, %26 ], [ %47, %37 ]
%33 = load i32, i32* %10
%34 = call i32 @read(i32 %33, i8* %30, i32 512)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
br label %48
37: ; preds = %31
%38 = icmp ugt i32 %34, 511
%39 = add nsw i32 %34, %32
%40 = icmp slt i32 %39, %2
%41 = xor i32 %32, -1
%42 = add i32 %41, %2
%43 = select i1 %40, i32 %34, i32 %42
%44 = sext i32 %32 to i64
%45 = getelementptr inbounds i8, i8* %3, i64 %44
%46 = call i32 @memcpy(i8* %45, i8* %30, i32 %43)
%47 = add nsw i32 %43, %32
br i1 %38, label %31, label %48
48: ; preds = %37, %36
%49 = phi i32 [ %32, %36 ], [ %47, %37 ]
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %3, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* %10
%53 = call i32 @close(i32 %52)
%54 = call i32 @wait(i32* %6)
br label %55
55: ; preds = %13, %4, %48
%56 = phi i32 [ %49, %48 ], [ -1, %4 ], [ %14, %13 ]
ret i32 %56
}