#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*/ u_char;
struct timeval {
  int member_0;
  scalar_t__ tv_usec;
  int /*<<< orphan*/ tv_sec;
  int /*<<< orphan*/ member_1;
};
typedef int pid_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int STDIN_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ asprintf(char **, char *, char *);
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ errno;
int execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int pipe(int *);
size_t read(int, char *, int);
char *realloc(char *, size_t);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ x_perflog(char *, ...);
int /*<<< orphan*/ x_printf(char *, ...);
char *x_command_run(char *command_str, int flags) {
  int num;
  int fd1[2], fd2[2];
  pid_t pid;
  struct timeval start;
  gettimeofday(&start, NULL);
  num = pipe(fd1);
  if (num == -1) {
    x_printf("ERROR: x_command_run failed to create fd1 pipe (%s)",
             strerror(errno));
    return NULL;
  }
  num = pipe(fd2);
  if (num == -1) {
    x_printf("ERROR: x_command_run failed to create fd2 pipe");
    return NULL;
  }
  x_perflog("PERF: x_command_run running '%s'", command_str);
  pid = fork();
  if (pid == -1) {
    x_printf("ERROR: x_command_run failed to fork '%s'", strerror(errno));
    return NULL;
  }
  if (pid > 0) {
    close(fd1[0]);
    close(fd2[1]);
    size_t recvdata_len = 0;
    char *recvdata = NULL;
    int finished = 0;
    while (!finished) {
      fd_set read_fds;
      FD_ZERO(&read_fds);
      FD_SET(fd2[0], &read_fds);
      struct timeval timeout = {30, 0};
      num = select(fd2[0] + 1, &read_fds, NULL, NULL, &timeout);
      if (num > 0) {
        char buf[1024];
        size_t nbytes = read(fd2[0], buf, 1023);
        if (nbytes > 0) {
          buf[nbytes] = '\0';
          if (recvdata) {
            char *tmp = realloc(recvdata, recvdata_len + nbytes);
            if (!tmp) {
              free(recvdata);
              return NULL;
            }
            recvdata = tmp;
            memcpy(recvdata + recvdata_len, buf, nbytes);
            recvdata_len += nbytes;
          } else {
            recvdata = (char *)calloc(nbytes, sizeof(u_char));
            memcpy(recvdata, buf, nbytes);
            recvdata_len = nbytes;
          }
        } else {
          finished = 1;
        }
      } else if (num == 0) {
        finished = 1;
      } else {
        finished = 1;
      }
    }
    if (recvdata && recvdata_len > 0) {
      char *tmp = realloc(recvdata, recvdata_len + 1);
      if (!tmp) {
        free(recvdata);
        return NULL;
      }
      recvdata = tmp;
      recvdata[recvdata_len] = '\0';
      recvdata_len++;
    }
    struct timeval end;
    gettimeofday(&end, NULL);
    x_perflog("PERF: x_command_run took %lu.%u to run '%s'",
              (end.tv_sec - start.tv_sec), (end.tv_usec - start.tv_usec),
              command_str);
    close(fd1[1]);
    close(fd2[0]);
    waitpid(-1, NULL, WNOHANG);
    return recvdata;
  } else {
    close(fd1[1]);
    close(fd2[0]);
    if (fd1[0] != STDIN_FILENO) {
      if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
        fprintf(stdout, "dup2 error to stdin");
      close(fd1[0]);
    }
    if (fd2[1] != STDOUT_FILENO) {
      if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
        fprintf(stdout, "dup2 error to stdout");
      close(fd2[1]);
    }
    char *redir_command_str;
    asprintf(&redir_command_str, "%s 2>&1", command_str);
    num = execlp("sh", "sh", "-c", redir_command_str, NULL);
    free(redir_command_str);
    if (num == -1) {
      fprintf(stdout, "execlp error");
    }
    exit(1);
  }
}


x86 assembly (clang, -O0):
<x_command_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
lea    -0x20(%rbp),%rdi
call   1090 <pipe@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12b0 <x_command_run+0x60>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   1150 <strerror@plt>
mov    %eax,%esi
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1050 <x_printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    16ff <x_command_run+0x4af>
lea    -0x28(%rbp),%rdi
call   1090 <pipe@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12e1 <x_command_run+0x91>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <x_printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    16ff <x_command_run+0x4af>
mov    -0x10(%rbp),%rsi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <x_perflog@plt>
mov    $0x0,%al
call   1170 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1332 <x_command_run+0xe2>
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   1150 <strerror@plt>
mov    %eax,%esi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1050 <x_printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    16ff <x_command_run+0x4af>
cmpl   $0x0,-0x2c(%rbp)
jle    15e0 <x_command_run+0x390>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1379 <x_command_run+0x129>
jmp    1501 <x_command_run+0x2b1>
lea    -0x60(%rbp),%rdi
call   10e0 <FD_ZERO@plt>
mov    -0x28(%rbp),%edi
lea    -0x60(%rbp),%rsi
call   1160 <FD_SET@plt>
mov    0xd8b(%rip),%rax
mov    %rax,-0x78(%rbp)
mov    0xd88(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0xd85(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    -0x28(%rbp),%edi
add    $0x1,%edi
lea    -0x60(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x78(%rbp),%r8
mov    %rcx,%rdx
call   10f0 <select@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    14da <x_command_run+0x28a>
mov    -0x28(%rbp),%edi
lea    -0x480(%rbp),%rsi
mov    $0x3ff,%edx
call   10a0 <read@plt>
mov    %rax,-0x488(%rbp)
cmpq   $0x0,-0x488(%rbp)
jbe    14ce <x_command_run+0x27e>
mov    -0x488(%rbp),%rax
movb   $0x0,-0x480(%rbp,%rax,1)
cmpq   $0x0,-0x58(%rbp)
je     1492 <x_command_run+0x242>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
add    -0x488(%rbp),%rsi
call   1100 <realloc@plt>
mov    %rax,-0x490(%rbp)
cmpq   $0x0,-0x490(%rbp)
jne    1458 <x_command_run+0x208>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    16ff <x_command_run+0x4af>
mov    -0x490(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
add    -0x50(%rbp),%rdi
lea    -0x480(%rbp),%rsi
mov    -0x488(%rbp),%rdx
call   10d0 <memcpy@plt>
mov    -0x488(%rbp),%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    14c9 <x_command_run+0x279>
mov    -0x488(%rbp),%rdi
mov    $0x4,%esi
call   10b0 <calloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
lea    -0x480(%rbp),%rsi
mov    -0x488(%rbp),%rdx
call   10d0 <memcpy@plt>
mov    -0x488(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    14d5 <x_command_run+0x285>
movl   $0x1,-0x5c(%rbp)
jmp    14fc <x_command_run+0x2ac>
cmpl   $0x0,-0x18(%rbp)
jne    14f0 <x_command_run+0x2a0>
movl   $0x1,-0x5c(%rbp)
jmp    14f7 <x_command_run+0x2a7>
movl   $0x1,-0x5c(%rbp)
jmp    14fc <x_command_run+0x2ac>
jmp    1363 <x_command_run+0x113>
cmpq   $0x0,-0x58(%rbp)
je     1576 <x_command_run+0x326>
cmpq   $0x0,-0x50(%rbp)
jbe    1576 <x_command_run+0x326>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
add    $0x1,%rsi
call   1100 <realloc@plt>
mov    %rax,-0x498(%rbp)
cmpq   $0x0,-0x498(%rbp)
jne    1553 <x_command_run+0x303>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    16ff <x_command_run+0x4af>
mov    -0x498(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
mov    -0x50(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
lea    -0x4b0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x4a0(%rbp),%esi
sub    -0x38(%rbp),%esi
mov    -0x4a8(%rbp),%rdx
sub    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xb08(%rip),%rdi
mov    $0x0,%al
call   1040 <x_perflog@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    0x2a1d(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1120 <waitpid@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    16ff <x_command_run+0x4af>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%eax
mov    0x29c6(%rip),%rcx
cmp    (%rcx),%eax
je     163f <x_command_run+0x3ef>
mov    -0x20(%rbp),%edi
mov    0x29b4(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    0x29a6(%rip),%rcx
cmp    (%rcx),%eax
je     1637 <x_command_run+0x3e7>
mov    0x299f(%rip),%rax
mov    (%rax),%edi
lea    0xaa7(%rip),%rsi
call   10c0 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%eax
mov    0x29a7(%rip),%rcx
cmp    (%rcx),%eax
je     168e <x_command_run+0x43e>
mov    -0x24(%rbp),%edi
mov    0x2995(%rip),%rax
mov    (%rax),%esi
call   1060 <dup2@plt>
mov    0x2987(%rip),%rcx
cmp    (%rcx),%eax
je     1686 <x_command_run+0x436>
mov    0x2950(%rip),%rax
mov    (%rax),%edi
lea    0xa6c(%rip),%rsi
call   10c0 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rdx
lea    -0x4b8(%rbp),%rdi
lea    0xa62(%rip),%rsi
call   1110 <asprintf@plt>
mov    -0x4b8(%rbp),%rcx
lea    0xa57(%rip),%rsi
lea    0xa53(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1140 <execlp@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4b8(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    16f5 <x_command_run+0x4a5>
mov    0x28e1(%rip),%rax
mov    (%rax),%edi
lea    0xa20(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x4c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<x_command_run>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x458,%rsp
mov    %rdi,%rbx
lea    0x38(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
je     13c0 <x_command_run+0x170>
mov    %rsp,%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
je     13da <x_command_run+0x18a>
lea    0xdc8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <x_perflog@plt>
xor    %eax,%eax
call   1170 <fork@plt>
cmp    $0xffffffff,%eax
je     1471 <x_command_run+0x221>
mov    %rbx,0x10(%rsp)
test   %eax,%eax
jle    14b4 <x_command_run+0x264>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
xor    %r13d,%r13d
lea    0x1c(%rsp),%r15
xor    %r12d,%r12d
jmp    1313 <x_command_run+0xc3>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x4,%esi
call   10b0 <calloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
lea    0x50(%rsp),%rsi
mov    %r14,%rdx
call   10d0 <memcpy@plt>
mov    %r14,%r12
mov    %r15,%rdi
call   10e0 <FD_ZERO@plt>
mov    (%rsp),%edi
mov    %r15,%rsi
call   1160 <FD_SET@plt>
mov    0xe03(%rip),%rax
mov    %rax,0x30(%rsp)
movups 0xde7(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
mov    (%rsp),%edi
add    $0x1,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x20(%rsp),%r8
call   10f0 <select@plt>
test   %eax,%eax
jle    13f0 <x_command_run+0x1a0>
mov    (%rsp),%edi
lea    0x50(%rsp),%rsi
mov    $0x3ff,%edx
call   10a0 <read@plt>
test   %rax,%rax
je     13f0 <x_command_run+0x1a0>
mov    %rax,%r14
movb   $0x0,0x50(%rsp,%rax,1)
test   %r13,%r13
je     12f0 <x_command_run+0xa0>
mov    %r14,%rbp
add    %r12,%rbp
mov    %r13,%rdi
mov    %rbp,%rsi
call   1100 <realloc@plt>
test   %rax,%rax
je     14a7 <x_command_run+0x257>
mov    %rax,%rbx
add    %rax,%r12
mov    %r12,%rdi
lea    0x50(%rsp),%rsi
mov    %r14,%rdx
call   10d0 <memcpy@plt>
mov    %rbx,%r13
mov    %rbp,%r12
jmp    1313 <x_command_run+0xc3>
mov    0x2c09(%rip),%rax
mov    (%rax),%edi
call   1150 <strerror@plt>
lea    0xc2b(%rip),%rdi
jmp    1486 <x_command_run+0x236>
lea    0xc53(%rip),%rdi
xor    %r13d,%r13d
xor    %eax,%eax
call   1050 <x_printf@plt>
jmp    1492 <x_command_run+0x242>
test   %r13,%r13
je     1418 <x_command_run+0x1c8>
test   %r12,%r12
je     1418 <x_command_run+0x1c8>
lea    0x1(%r12),%rsi
mov    %r13,%rdi
call   1100 <realloc@plt>
test   %rax,%rax
je     14a7 <x_command_run+0x257>
movb   $0x0,(%rax,%r12,1)
mov    %rax,%r13
lea    0x50(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    0x60(%rsp),%esi
sub    0x48(%rsp),%esi
mov    0x58(%rsp),%rdx
sub    0x40(%rsp),%rdx
lea    0xc70(%rip),%rdi
mov    0x10(%rsp),%rcx
xor    %eax,%eax
call   1040 <x_perflog@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    (%rsp),%edi
call   1080 <close@plt>
mov    0x2b7f(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
xor    %esi,%esi
call   1120 <waitpid@plt>
jmp    1492 <x_command_run+0x242>
mov    0x2b58(%rip),%rax
mov    (%rax),%edi
call   1150 <strerror@plt>
lea    0xbfe(%rip),%rdi
xor    %r13d,%r13d
mov    %eax,%esi
xor    %eax,%eax
call   1050 <x_printf@plt>
mov    %r13,%rax
add    $0x458,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
jmp    1492 <x_command_run+0x242>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
mov    (%rsp),%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
mov    0x2af0(%rip),%rbx
mov    (%rbx),%esi
cmp    %esi,%edi
je     14fd <x_command_run+0x2ad>
call   1060 <dup2@plt>
cmp    (%rbx),%eax
je     14f4 <x_command_run+0x2a4>
mov    0x2ae2(%rip),%rax
mov    (%rax),%edi
lea    0xbea(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2ae8(%rip),%rbx
mov    (%rbx),%esi
cmp    %esi,%edi
je     1535 <x_command_run+0x2e5>
call   1060 <dup2@plt>
cmp    (%rbx),%eax
je     152c <x_command_run+0x2dc>
mov    0x2aaa(%rip),%rax
mov    (%rax),%edi
lea    0xbc6(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
lea    0xbc6(%rip),%rsi
lea    0x50(%rsp),%rdi
mov    0x10(%rsp),%rdx
call   1110 <asprintf@plt>
mov    0x50(%rsp),%rcx
lea    0xbb3(%rip),%rdi
lea    0xbaf(%rip),%rdx
mov    %rdi,%rsi
xor    %r8d,%r8d
call   1140 <execlp@plt>
mov    %eax,%ebx
mov    0x50(%rsp),%rdi
call   1030 <free@plt>
cmp    $0xffffffff,%ebx
jne    158f <x_command_run+0x33f>
mov    0x2a47(%rip),%rax
mov    (%rax),%edi
lea    0xb86(%rip),%rsi
call   10c0 <fprintf@plt>
mov    $0x1,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i64, i32, i32 }
@.str = [52 x i8] c"ERROR: x_command_run failed to create fd1 pipe (%s)\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"ERROR: x_command_run failed to create fd2 pipe\00"
@.str.2 = [33 x i8] c"PERF: x_command_run running '%s'\00"
@.str.3 = [41 x i8] c"ERROR: x_command_run failed to fork '%s'\00"
@__const.x_command_run.timeout = %struct.timeval { i32 30, i64 0, i32 0, i32 0 }
@.str.4 = [44 x i8] c"PERF: x_command_run took %lu.%u to run '%s'\00"
@WNOHANG = global i32 0
@STDIN_FILENO = global i32 0
@stdout = global i32 0
@.str.5 = [20 x i8] c"dup2 error to stdin\00"
@STDOUT_FILENO = global i32 0
@.str.6 = [21 x i8] c"dup2 error to stdout\00"
@.str.7 = [8 x i8] c"%s 2>&1\00"
@.str.8 = [3 x i8] c"sh\00"
@.str.9 = [3 x i8] c"-c\00"
@.str.10 = [13 x i8] c"execlp error\00"
define i8* @x_command_run(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [2 x i32]
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.timeval
%16 = alloca [1024 x i8]
%17 = alloca i64
%18 = alloca i8*
%19 = alloca i8*
%20 = alloca %struct.timeval
%21 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%22 = call i32 @gettimeofday(%struct.timeval* %10, i32* null)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%24 = call i32 @pipe(i32* %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %2
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 (i8*, ...) @x_printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %29)
store i8* null, i8** %3
br label %231
31: ; preds = %2
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%33 = call i32 @pipe(i32* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 (i8*, ...) @x_printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %3
br label %231
38: ; preds = %31
%39 = load i8*, i8** %4
%40 = call i32 (i8*, ...) @x_perflog(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %39)
%41 = call i32 (...) @fork()
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 (i8*, ...) @x_printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %46)
store i8* null, i8** %3
br label %231
48: ; preds = %38
%49 = load i32, i32* %9
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %173
51: ; preds = %48
%52 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
store i64 0, i64* %11
store i8* null, i8** %12
store i32 0, i32* %13
br label %58
58: ; preds = %126, %51
%59 = load i32, i32* %13
%60 = icmp ne i32 %59, 0
%61 = xor i1 %60, true
br i1 %61, label %62, label %127
62: ; preds = %58
%63 = call i32 @FD_ZERO(i32* %14)
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%65 = load i32, i32* %64
%66 = call i32 @FD_SET(i32 %65, i32* %14)
%67 = bitcast %struct.timeval* %15 to i8*
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%69 = load i32, i32* %68
%70 = add nsw i32 %69, 1
%71 = call i32 @select(i32 %70, i32* %14, i32* null, i32* null, %struct.timeval* %15)
store i32 %71, i32* %6
%72 = load i32, i32* %6
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %120
74: ; preds = %62
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%76 = load i32, i32* %75
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%78 = call i64 @read(i32 %76, i8* %77, i32 1023)
store i64 %78, i64* %17
%79 = load i64, i64* %17
%80 = icmp ugt i64 %79, 0
br i1 %80, label %81, label %118
81: ; preds = %74
%82 = load i64, i64* %17
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 %82
store i8 0, i8* %83
%84 = load i8*, i8** %12
%85 = icmp ne i8* %84, null
br i1 %85, label %86, label %108
86: ; preds = %81
%87 = load i8*, i8** %12
%88 = load i64, i64* %11
%89 = load i64, i64* %17
%90 = add i64 %88, %89
%91 = call i8* @realloc(i8* %87, i64 %90)
store i8* %91, i8** %18
%92 = load i8*, i8** %18
%93 = icmp ne i8* %92, null
br i1 %93, label %97, label %94
94: ; preds = %86
%95 = load i8*, i8** %12
%96 = call i32 @free(i8* %95)
store i8* null, i8** %3
br label %231
97: ; preds = %86
%98 = load i8*, i8** %18
store i8* %98, i8** %12
%99 = load i8*, i8** %12
%100 = load i64, i64* %11
%101 = getelementptr inbounds i8, i8* %99, i64 %100
%102 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%103 = load i64, i64* %17
%104 = call i32 @memcpy(i8* %101, i8* %102, i64 %103)
%105 = load i64, i64* %17
%106 = load i64, i64* %11
%107 = add i64 %106, %105
store i64 %107, i64* %11
br label %117
108: ; preds = %81
%109 = load i64, i64* %17
%110 = call i64 @calloc(i64 %109, i32 4)
%111 = inttoptr i64 %110 to i8*
store i8* %111, i8** %12
%112 = load i8*, i8** %12
%113 = getelementptr inbounds [1024 x i8], [1024 x i8]* %16, i64 0, i64 0
%114 = load i64, i64* %17
%115 = call i32 @memcpy(i8* %112, i8* %113, i64 %114)
%116 = load i64, i64* %17
store i64 %116, i64* %11
br label %117
117: ; preds = %108, %97
br label %119
118: ; preds = %74
store i32 1, i32* %13
br label %119
119: ; preds = %118, %117
br label %126
120: ; preds = %62
%121 = load i32, i32* %6
%122 = icmp eq i32 %121, 0
br i1 %122, label %123, label %124
123: ; preds = %120
store i32 1, i32* %13
br label %125
124: ; preds = %120
store i32 1, i32* %13
br label %125
125: ; preds = %124, %123
br label %126
126: ; preds = %125, %119
br label %58
127: ; preds = %58
%128 = load i8*, i8** %12
%129 = icmp ne i8* %128, null
br i1 %129, label %130, label %150
130: ; preds = %127
%131 = load i64, i64* %11
%132 = icmp ugt i64 %131, 0
br i1 %132, label %133, label %150
133: ; preds = %130
%134 = load i8*, i8** %12
%135 = load i64, i64* %11
%136 = add i64 %135, 1
%137 = call i8* @realloc(i8* %134, i64 %136)
store i8* %137, i8** %19
%138 = load i8*, i8** %19
%139 = icmp ne i8* %138, null
br i1 %139, label %143, label %140
140: ; preds = %133
%141 = load i8*, i8** %12
%142 = call i32 @free(i8* %141)
store i8* null, i8** %3
br label %231
143: ; preds = %133
%144 = load i8*, i8** %19
store i8* %144, i8** %12
%145 = load i8*, i8** %12
%146 = load i64, i64* %11
%147 = getelementptr inbounds i8, i8* %145, i64 %146
store i8 0, i8* %147
%148 = load i64, i64* %11
%149 = add i64 %148, 1
store i64 %149, i64* %11
br label %150
150: ; preds = %143, %130, %127
%151 = call i32 @gettimeofday(%struct.timeval* %20, i32* null)
%152 = getelementptr inbounds %struct.timeval, %struct.timeval* %20, i32 0, i32 2
%153 = load i32, i32* %152
%154 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 2
%155 = load i32, i32* %154
%156 = sub nsw i32 %153, %155
%157 = getelementptr inbounds %struct.timeval, %struct.timeval* %20, i32 0, i32 1
%158 = load i64, i64* %157
%159 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i32 0, i32 1
%160 = load i64, i64* %159
%161 = sub nsw i64 %158, %160
%162 = load i8*, i8** %4
%163 = call i32 (i8*, ...) @x_perflog(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %156, i64 %161, i8* %162)
%164 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%165 = load i32, i32* %164
%166 = call i32 @close(i32 %165)
%167 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%168 = load i32, i32* %167
%169 = call i32 @close(i32 %168)
%170 = load i32, i32* @WNOHANG
%171 = call i32 @waitpid(i32 -1, i32* null, i32 %170)
%172 = load i8*, i8** %12
store i8* %172, i8** %3
br label %231
173: ; preds = %48
%174 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 1
%175 = load i32, i32* %174
%176 = call i32 @close(i32 %175)
%177 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%178 = load i32, i32* %177
%179 = call i32 @close(i32 %178)
%180 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%181 = load i32, i32* %180
%182 = load i32, i32* @STDIN_FILENO
%183 = icmp ne i32 %181, %182
br i1 %183, label %184, label %198
184: ; preds = %173
%185 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%186 = load i32, i32* %185
%187 = load i32, i32* @STDIN_FILENO
%188 = call i32 @dup2(i32 %186, i32 %187)
%189 = load i32, i32* @STDIN_FILENO
%190 = icmp ne i32 %188, %189
br i1 %190, label %191, label %194
191: ; preds = %184
%192 = load i32, i32* @stdout
%193 = call i32 @fprintf(i32 %192, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %194
194: ; preds = %191, %184
%195 = getelementptr inbounds [2 x i32], [2 x i32]* %7, i64 0, i64 0
%196 = load i32, i32* %195
%197 = call i32 @close(i32 %196)
br label %198
198: ; preds = %194, %173
%199 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%200 = load i32, i32* %199
%201 = load i32, i32* @STDOUT_FILENO
%202 = icmp ne i32 %200, %201
br i1 %202, label %203, label %217
203: ; preds = %198
%204 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%205 = load i32, i32* %204
%206 = load i32, i32* @STDOUT_FILENO
%207 = call i32 @dup2(i32 %205, i32 %206)
%208 = load i32, i32* @STDOUT_FILENO
%209 = icmp ne i32 %207, %208
br i1 %209, label %210, label %213
210: ; preds = %203
%211 = load i32, i32* @stdout
%212 = call i32 @fprintf(i32 %211, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %213
213: ; preds = %210, %203
%214 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%215 = load i32, i32* %214
%216 = call i32 @close(i32 %215)
br label %217
217: ; preds = %213, %198
%218 = load i8*, i8** %4
%219 = call i32 @asprintf(i8** %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* %218)
%220 = load i8*, i8** %21
%221 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %220, i32* null)
store i32 %221, i32* %6
%222 = load i8*, i8** %21
%223 = call i32 @free(i8* %222)
%224 = load i32, i32* %6
%225 = icmp eq i32 %224, -1
br i1 %225, label %226, label %229
226: ; preds = %217
%227 = load i32, i32* @stdout
%228 = call i32 @fprintf(i32 %227, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
br label %229
229: ; preds = %226, %217
%230 = call i32 @exit(i32 1)
unreachable
231: ; preds = %150, %140, %94, %44, %36, %27
%232 = load i8*, i8** %3
ret i8* %232
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64, i32, i32 }
@.str = [52 x i8] c"ERROR: x_command_run failed to create fd1 pipe (%s)\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"ERROR: x_command_run failed to create fd2 pipe\00"
@.str.2 = [33 x i8] c"PERF: x_command_run running '%s'\00"
@.str.3 = [41 x i8] c"ERROR: x_command_run failed to fork '%s'\00"
@__const.x_command_run.timeout = %struct.timeval { i32 30, i64 0, i32 0, i32 0 }
@.str.4 = [44 x i8] c"PERF: x_command_run took %lu.%u to run '%s'\00"
@WNOHANG = global i32 0
@STDIN_FILENO = global i32 0
@stdout = global i32 0
@.str.5 = [20 x i8] c"dup2 error to stdin\00"
@STDOUT_FILENO = global i32 0
@.str.6 = [21 x i8] c"dup2 error to stdout\00"
@.str.7 = [8 x i8] c"%s 2>&1\00"
@.str.8 = [3 x i8] c"sh\00"
@.str.9 = [3 x i8] c"-c\00"
@.str.10 = [13 x i8] c"execlp error\00"
define i8* @x_command_run(i8* %0, i32 %1) {
%3 = alloca [2 x i32]
%4 = alloca [2 x i32]
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca [1024 x i8]
%9 = alloca %struct.timeval
%10 = alloca i8*
%11 = bitcast [2 x i32]* %3 to i8*
%12 = bitcast [2 x i32]* %4 to i8*
%13 = bitcast %struct.timeval* %5 to i8*
%14 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 (i8*, ...) @x_printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %163
22: ; preds = %2
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%24 = call i32 @pipe(i32* %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 (i8*, ...) @x_printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %163
28: ; preds = %22
%29 = call i32 (i8*, ...) @x_perflog(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%30 = call i32 (...) @fork()
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* @errno
%34 = call i32 @strerror(i32 %33)
%35 = call i32 (i8*, ...) @x_printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %34)
br label %163
36: ; preds = %28
%37 = icmp sgt i32 %30, 0
br i1 %37, label %38, label %116
38: ; preds = %36
%39 = load i32, i32* %15
%40 = call i32 @close(i32 %39)
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = bitcast i32* %6 to i8*
%45 = bitcast %struct.timeval* %7 to i8*
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
br label %47
47: ; preds = %79, %38
%48 = phi i8* [ null, %38 ], [ %81, %79 ]
%49 = phi i64 [ 0, %38 ], [ %80, %79 ]
%50 = call i32 @FD_ZERO(i32* %6)
%51 = load i32, i32* %23
%52 = call i32 @FD_SET(i32 %51, i32* %6)
%53 = load i32, i32* %23
%54 = add nsw i32 %53, 1
%55 = call i32 @select(i32 %54, i32* %6, i32* null, i32* null, %struct.timeval* %7)
%56 = icmp sgt i32 %55, 0
br i1 %56, label %58, label %57
57: ; preds = %47
br label %82
58: ; preds = %47
%59 = load i32, i32* %23
%60 = call i64 @read(i32 %59, i8* %46, i32 1023)
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %58
br label %82
63: ; preds = %58
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %60
store i8 0, i8* %64
%65 = icmp eq i8* %48, null
br i1 %65, label %73, label %66
66: ; preds = %63
%67 = add i64 %60, %49
%68 = call i8* @realloc(i8* %48, i64 %67)
%69 = icmp eq i8* %68, null
br i1 %69, label %77, label %70
70: ; preds = %66
%71 = getelementptr inbounds i8, i8* %68, i64 %49
%72 = call i32 @memcpy(i8* %71, i8* %46, i64 %60)
br label %79
73: ; preds = %63
%74 = call i64 @calloc(i64 %60, i32 4)
%75 = inttoptr i64 %74 to i8*
%76 = call i32 @memcpy(i8* %75, i8* %46, i64 %60)
br label %79
77: ; preds = %66
%78 = call i32 @free(i8* %48)
br label %163
79: ; preds = %73, %70
%80 = phi i64 [ %60, %73 ], [ %67, %70 ]
%81 = phi i8* [ %75, %73 ], [ %68, %70 ]
br label %47
82: ; preds = %62, %57
%83 = icmp ne i8* %48, null
%84 = icmp ne i64 %49, 0
%85 = and i1 %83, %84
br i1 %85, label %86, label %94
86: ; preds = %82
%87 = add i64 %49, 1
%88 = call i8* @realloc(i8* %48, i64 %87)
%89 = icmp eq i8* %88, null
br i1 %89, label %90, label %92
90: ; preds = %86
%91 = call i32 @free(i8* %48)
br label %163
92: ; preds = %86
%93 = getelementptr inbounds i8, i8* %88, i64 %49
store i8 0, i8* %93
br label %94
94: ; preds = %92, %82
%95 = phi i8* [ %88, %92 ], [ %48, %82 ]
%96 = bitcast %struct.timeval* %9 to i8*
%97 = call i32 @gettimeofday(%struct.timeval* %9, i32* null)
%98 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 2
%99 = load i32, i32* %98
%100 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 2
%101 = load i32, i32* %100
%102 = sub nsw i32 %99, %101
%103 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i64 0, i32 1
%104 = load i64, i64* %103
%105 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%106 = load i64, i64* %105
%107 = sub nsw i64 %104, %106
%108 = call i32 (i8*, ...) @x_perflog(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %102, i64 %107, i8* %0)
%109 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%110 = load i32, i32* %109
%111 = call i32 @close(i32 %110)
%112 = load i32, i32* %23
%113 = call i32 @close(i32 %112)
%114 = load i32, i32* @WNOHANG
%115 = call i32 @waitpid(i32 -1, i32* null, i32 %114)
br label %163
116: ; preds = %36
%117 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%118 = load i32, i32* %117
%119 = call i32 @close(i32 %118)
%120 = load i32, i32* %23
%121 = call i32 @close(i32 %120)
%122 = load i32, i32* %15
%123 = load i32, i32* @STDIN_FILENO
%124 = icmp eq i32 %122, %123
br i1 %124, label %135, label %125
125: ; preds = %116
%126 = call i32 @dup2(i32 %122, i32 %123)
%127 = load i32, i32* @STDIN_FILENO
%128 = icmp eq i32 %126, %127
br i1 %128, label %132, label %129
129: ; preds = %125
%130 = load i32, i32* @stdout
%131 = call i32 @fprintf(i32 %130, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %132
132: ; preds = %129, %125
%133 = load i32, i32* %15
%134 = call i32 @close(i32 %133)
br label %135
135: ; preds = %132, %116
%136 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%137 = load i32, i32* %136
%138 = load i32, i32* @STDOUT_FILENO
%139 = icmp eq i32 %137, %138
br i1 %139, label %150, label %140
140: ; preds = %135
%141 = call i32 @dup2(i32 %137, i32 %138)
%142 = load i32, i32* @STDOUT_FILENO
%143 = icmp eq i32 %141, %142
br i1 %143, label %147, label %144
144: ; preds = %140
%145 = load i32, i32* @stdout
%146 = call i32 @fprintf(i32 %145, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %147
147: ; preds = %144, %140
%148 = load i32, i32* %136
%149 = call i32 @close(i32 %148)
br label %150
150: ; preds = %147, %135
%151 = bitcast i8** %10 to i8*
%152 = call i32 @asprintf(i8** %10, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* %0)
%153 = load i8*, i8** %10
%154 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.9, i64 0, i64 0), i8* %153, i32* null)
%155 = load i8*, i8** %10
%156 = call i32 @free(i8* %155)
%157 = icmp eq i32 %154, -1
br i1 %157, label %158, label %161
158: ; preds = %150
%159 = load i32, i32* @stdout
%160 = call i32 @fprintf(i32 %159, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
br label %161
161: ; preds = %158, %150
%162 = call i32 @exit(i32 1)
unreachable
163: ; preds = %90, %77, %94, %32, %26, %18
%164 = phi i8* [ null, %18 ], [ null, %26 ], [ null, %32 ], [ %95, %94 ], [ null, %77 ], [ null, %90 ]
ret i8* %164
}