#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*/ count;

/* Variables and functions */
int /*<<< orphan*/ FASYNC;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ PTRACE_CONT;
int /*<<< orphan*/ PTRACE_DETACH;
int PTRACE_EVENT_EXIT;
int PTRACE_EVENT_STOP;
int /*<<< orphan*/ PTRACE_LISTEN;
int /*<<< orphan*/ PTRACE_O_TRACEEXIT;
int /*<<< orphan*/ PTRACE_SEIZE;
int /*<<< orphan*/ SIGCONT;
scalar_t__ SIGIO;
scalar_t__ SIGSTOP;
int SIGTRAP;
scalar_t__ SIGTSTP;
scalar_t__ SIGTTIN;
scalar_t__ SIGTTOU;
scalar_t__ WIFSTOPPED(int);
scalar_t__ WSTOPSIG(int);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ ptrace(int /*<<< orphan*/, int, ...);
int read(int, long long *, int);
int setup_count(long long, int);
int /*<<< orphan*/ stderr;
int strtol(char *, char **, int);
long long strtoll(char *, char **, int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  pid_t pid, waitedpid;
  int fd, status, ret = 0;
  char *end;
  long long count;
  if (argc != 2 && argc != 3) {
    fprintf(stderr,
            "Usage: %s pid [instruction-count]\n"
            "\n"
            "Wait for any process to finish or run a given number of "
            "instructions.\n",
            argv[0]);
    return -1;
  }
  errno = 0;
  pid = strtol(argv[1], &end, 10);
  if (errno) {
    perror("Invalid PID");
    return -1;
  } else if (end == argv[1] || *end != '\0') {
    fprintf(stderr, "Invalid PID\n");
    return -1;
  }
  if (ptrace(PTRACE_SEIZE, pid, 0, PTRACE_O_TRACEEXIT)) {
    fprintf(stderr, "PTRACE_SEIZE returned error, %d\n", errno);
    return -1;
  }
  if (argc == 3) {
    errno = 0;
    count = strtoll(argv[2], &end, 0);
    if (errno) {
      perror("Invalid instruction count");
      return -1;
    } else if (end == argv[2] || *end != '\0') {
      fprintf(stderr, "Invalid instruction count\n");
      return -1;
    }
    fd = setup_count(count, pid);
    kill(pid, SIGCONT);
  }
  while (1) {
    waitedpid = waitpid(pid, &status, 0);
    if (waitedpid != pid) {
      fprintf(stderr, "Error waiting for pid %d: %d\n", pid, waitedpid);
      ret = -1;
      break;
    }
    if ((status >> 8) == (SIGTRAP | (PTRACE_EVENT_EXIT << 8)))
      break;
    if (WIFSTOPPED(status)) {
      if ((status >> 16 != PTRACE_EVENT_STOP) ||
          (WSTOPSIG(status) != SIGSTOP && WSTOPSIG(status) != SIGTSTP &&
           WSTOPSIG(status) != SIGTTIN && WSTOPSIG(status) != SIGTTOU)) {
        if (argc == 3 && WSTOPSIG(status) == SIGIO) {
          ptrace(PTRACE_CONT, pid, 0, SIGSTOP);
          break;
        }
        ptrace(PTRACE_CONT, pid, 0, WSTOPSIG(status));
      } else {
        ptrace(PTRACE_LISTEN, pid, 0, 0);
      }
    } else {
      fprintf(stderr, "Unexpected wakeup from waitpid for pid %d\n", pid);
      ret = -1;
      break;
    }
  }
  if (argc == 3) {
    fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & !FASYNC);
    if (read(fd, &count, sizeof(count)) != sizeof(count))
      fprintf(stderr, "Could not read from perf event\n");
    else
      printf("Counted %lld instructions\n", count);
    close(fd);
  }
  printf("Detaching from pid %d\n", pid);
  ptrace(PTRACE_DETACH, pid);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     123b <main+0x5b>
cmpl   $0x3,-0x8(%rbp)
je     123b <main+0x5b>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd8(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    166f <main+0x48f>
mov    0x2d3e(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d18(%rip),%rax
cmpl   $0x0,(%rax)
je     1289 <main+0xa9>
lea    0xdf2(%rip),%rdi
call   10f0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    166f <main+0x48f>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
je     12ab <main+0xcb>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12ce <main+0xee>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    166f <main+0x48f>
jmp    12d3 <main+0xf3>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    0x2ce2(%rip),%rax
mov    (%rax),%ecx
xor    %edx,%edx
mov    $0x0,%al
call   10d0 <ptrace@plt>
cmp    $0x0,%eax
je     1326 <main+0x146>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
mov    0x2c76(%rip),%rax
mov    (%rax),%edx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    166f <main+0x48f>
cmpl   $0x3,-0x8(%rbp)
jne    13e6 <main+0x206>
mov    0x2c49(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1070 <strtoll@plt>
mov    %rax,-0x38(%rbp)
mov    0x2c25(%rip),%rax
cmpl   $0x0,(%rax)
je     137c <main+0x19c>
lea    0xd39(%rip),%rdi
call   10f0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    166f <main+0x48f>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    0x10(%rcx),%rax
je     139e <main+0x1be>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     13c1 <main+0x1e1>
mov    0x2beb(%rip),%rax
mov    (%rax),%edi
lea    0xd10(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    166f <main+0x48f>
jmp    13c6 <main+0x1e6>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <setup_count@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2bf9(%rip),%rax
mov    (%rax),%esi
call   10b0 <kill@plt>
jmp    13eb <main+0x20b>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   10e0 <waitpid@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1431 <main+0x251>
mov    0x2b81(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xcbb(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    15a0 <main+0x3c0>
mov    -0x20(%rbp),%eax
sar    $0x8,%eax
mov    0x2b22(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2b29(%rip),%rdx
mov    (%rdx),%edx
shl    $0x8,%edx
or     %edx,%ecx
cmp    %ecx,%eax
jne    145b <main+0x27b>
jmp    15a0 <main+0x3c0>
mov    -0x20(%rbp),%edi
call   10c0 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1575 <main+0x395>
mov    -0x20(%rbp),%eax
sar    $0x10,%eax
mov    0x2b3e(%rip),%rcx
cmp    (%rcx),%eax
jne    14e2 <main+0x302>
mov    -0x20(%rbp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2abf(%rip),%rcx
cmp    (%rcx),%rax
je     1559 <main+0x379>
mov    -0x20(%rbp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2b07(%rip),%rcx
cmp    (%rcx),%rax
je     1559 <main+0x379>
mov    -0x20(%rbp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2adf(%rip),%rcx
cmp    (%rcx),%rax
je     1559 <main+0x379>
mov    -0x20(%rbp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2b07(%rip),%rcx
cmp    (%rcx),%rax
je     1559 <main+0x379>
cmpl   $0x3,-0x8(%rbp)
jne    1528 <main+0x348>
mov    -0x20(%rbp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2a6d(%rip),%rcx
cmp    (%rcx),%rax
jne    1528 <main+0x348>
mov    0x2a4d(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    0x2a39(%rip),%rax
mov    (%rax),%rcx
xor    %edx,%edx
mov    $0x0,%al
call   10d0 <ptrace@plt>
jmp    15a0 <main+0x3c0>
mov    0x2a29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%edi
call   1100 <WSTOPSIG@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    %rax,%rcx
xor    %edx,%edx
mov    $0x0,%al
call   10d0 <ptrace@plt>
jmp    1570 <main+0x390>
mov    0x2a90(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %ecx,%ecx
mov    %ecx,%edx
mov    $0x0,%al
call   10d0 <ptrace@plt>
jmp    159b <main+0x3bb>
mov    0x2a14(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xb6f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    15a0 <main+0x3c0>
jmp    13eb <main+0x20b>
cmpl   $0x3,-0x8(%rbp)
jne    1647 <main+0x467>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x29d1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x29b2(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x48(%rbp),%edi
mov    -0x44(%rbp),%esi
mov    %eax,%edx
mov    0x29ca(%rip),%rax
cmpl   $0x0,(%rax)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
and    %eax,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
cltq
cmp    $0x8,%rax
je     162d <main+0x44d>
mov    0x2978(%rip),%rax
mov    (%rax),%edi
lea    0xb01(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    163f <main+0x45f>
mov    -0x38(%rbp),%rax
mov    %eax,%esi
lea    0xb08(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%esi
lea    0xb0c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2973(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   10d0 <ptrace@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    1255 <main+0x75>
mov    %edi,%r12d
mov    0x2d7b(%rip),%rbp
movl   $0x0,0x0(%rbp)
mov    0x8(%rbx),%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   10a0 <strtol@plt>
cmpl   $0x0,0x0(%rbp)
jne    1314 <main+0x134>
mov    %eax,%r15d
mov    0x10(%rsp),%rax
cmp    0x8(%rbx),%rax
je     123c <main+0x5c>
cmpb   $0x0,(%rax)
je     1285 <main+0xa5>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    126f <main+0x8f>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd98(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
mov    0x2d33(%rip),%rax
mov    (%rax),%ecx
mov    %r15d,%esi
xor    %edx,%edx
xor    %eax,%eax
call   10d0 <ptrace@plt>
test   %eax,%eax
je     12c3 <main+0xe3>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%edx
lea    0xdc9(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    126f <main+0x8f>
cmp    $0x3,%r12d
jne    1345 <main+0x165>
movl   $0x0,0x0(%rbp)
mov    0x10(%rbx),%rdi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1070 <strtoll@plt>
mov    %rax,0x20(%rsp)
cmpl   $0x0,0x0(%rbp)
jne    15a8 <main+0x3c8>
mov    0x10(%rsp),%rcx
cmp    0x10(%rbx),%rcx
je     12ff <main+0x11f>
cmpb   $0x0,(%rcx)
je     1325 <main+0x145>
mov    0x2c8a(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
jmp    124c <main+0x6c>
lea    0xd4f(%rip),%rdi
call   10f0 <perror@plt>
jmp    126f <main+0x8f>
mov    %rax,%rdi
mov    %r15d,%esi
call   1090 <setup_count@plt>
mov    %eax,0xc(%rsp)
mov    0x2c9d(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   10b0 <kill@plt>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   10e0 <waitpid@plt>
cmp    %r15d,%eax
jne    14b2 <main+0x2d2>
mov    0x2bfc(%rip),%rbp
mov    0x2c05(%rip),%r14
mov    0x2c46(%rip),%r13
xor    %ebx,%ebx
mov    %r15d,0x1c(%rsp)
jmp    13c9 <main+0x1e9>
mov    %r14,%r15
mov    %r13,%r14
mov    0x2bd0(%rip),%rax
mov    (%rax),%r13d
mov    0x8(%rsp),%edi
call   1100 <WSTOPSIG@plt>
mov    %r13d,%edi
mov    %r14,%r13
mov    %r15,%r14
mov    0x1c(%rsp),%r15d
mov    %r15d,%esi
xor    %edx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   10d0 <ptrace@plt>
mov    %r15d,%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   10e0 <waitpid@plt>
cmp    %r15d,%eax
jne    14b2 <main+0x2d2>
mov    0x8(%rsp),%edi
mov    %edi,%eax
sar    $0x8,%eax
mov    (%r14),%ecx
shl    $0x8,%ecx
or     0x0(%rbp),%ecx
cmp    %ecx,%eax
je     14d3 <main+0x2f3>
call   10c0 <WIFSTOPPED@plt>
test   %rax,%rax
je     1589 <main+0x3a9>
mov    0x8(%rsp),%edi
mov    %edi,%eax
sar    $0x10,%eax
cmp    0x0(%r13),%eax
jne    146c <main+0x28c>
call   1100 <WSTOPSIG@plt>
mov    0x2b44(%rip),%rcx
cmp    (%rcx),%rax
je     1450 <main+0x270>
mov    0x8(%rsp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2b8f(%rip),%rcx
cmp    (%rcx),%rax
je     1450 <main+0x270>
mov    0x8(%rsp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2b6a(%rip),%rcx
cmp    (%rcx),%rax
je     1450 <main+0x270>
mov    0x8(%rsp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2b95(%rip),%rcx
cmp    (%rcx),%rax
jne    146c <main+0x28c>
mov    0x2b99(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   10d0 <ptrace@plt>
jmp    13b1 <main+0x1d1>
cmp    $0x3,%r12d
jne    137b <main+0x19b>
mov    0x8(%rsp),%edi
call   1100 <WSTOPSIG@plt>
mov    0x2ae2(%rip),%rcx
cmp    (%rcx),%rax
jne    137b <main+0x19b>
mov    0x2ac2(%rip),%rax
mov    (%rax),%edi
mov    0x2ab1(%rip),%rax
mov    (%rax),%rcx
xor    %ebx,%ebx
mov    %r15d,%esi
xor    %edx,%edx
xor    %eax,%eax
call   10d0 <ptrace@plt>
jmp    14dd <main+0x2fd>
mov    0x2ad7(%rip),%rcx
mov    (%rcx),%edi
lea    0xc17(%rip),%rsi
mov    %r15d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
cmp    $0x3,%r12d
jne    1562 <main+0x382>
mov    0x2aa4(%rip),%rax
mov    (%rax),%r14d
mov    0x2a8a(%rip),%rax
mov    (%rax),%esi
mov    0xc(%rsp),%ebp
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2aa4(%rip),%rcx
xor    %edx,%edx
cmpl   $0x0,(%rcx)
sete   %dl
and    %eax,%edx
mov    %ebp,%edi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1060 <read@plt>
cmp    $0x8,%eax
jne    1542 <main+0x362>
mov    0x20(%rsp),%esi
lea    0xc07(%rip),%rdi
call   1040 <printf@plt>
jmp    1559 <main+0x379>
mov    0x2a47(%rip),%rax
mov    (%rax),%edi
lea    0xbd0(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xbf4(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
mov    0x2a58(%rip),%rax
mov    (%rax),%edi
mov    %r15d,%esi
xor    %eax,%eax
call   10d0 <ptrace@plt>
jmp    1274 <main+0x94>
mov    0x2a00(%rip),%rax
mov    (%rax),%edi
lea    0xb5e(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    14ce <main+0x2ee>
lea    0xaf5(%rip),%rdi
jmp    131b <main+0x13b>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [106 x i8] c"Usage: %s pid [instruction-count]\0A\0AWait for any process to finish or run a given number of instructions.\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"Invalid PID\00"
@.str.2 = [13 x i8] c"Invalid PID\0A\00"
@PTRACE_SEIZE = global i32 0
@PTRACE_O_TRACEEXIT = global i32 0
@.str.3 = [33 x i8] c"PTRACE_SEIZE returned error, %d\0A\00"
@.str.4 = [26 x i8] c"Invalid instruction count\00"
@.str.5 = [27 x i8] c"Invalid instruction count\0A\00"
@SIGCONT = global i32 0
@.str.6 = [30 x i8] c"Error waiting for pid %d: %d\0A\00"
@SIGTRAP = global i32 0
@PTRACE_EVENT_EXIT = global i32 0
@PTRACE_EVENT_STOP = global i32 0
@SIGSTOP = global i64 0
@SIGTSTP = global i64 0
@SIGTTIN = global i64 0
@SIGTTOU = global i64 0
@SIGIO = global i64 0
@PTRACE_CONT = global i32 0
@PTRACE_LISTEN = global i32 0
@.str.7 = [43 x i8] c"Unexpected wakeup from waitpid for pid %d\0A\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@FASYNC = global i32 0
@.str.8 = [32 x i8] c"Could not read from perf event\0A\00"
@.str.9 = [27 x i8] c"Counted %lld instructions\0A\00"
@.str.10 = [23 x i8] c"Detaching from pid %d\0A\00"
@PTRACE_DETACH = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %10
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %24
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %24
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 -1, i32* %3
br label %207
24: ; preds = %15, %2
store i32 0, i32* @errno
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @strtol(i8* %27, i8** %11, i32 10)
store i32 %28, i32* %6
%29 = load i32, i32* @errno
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %207
33: ; preds = %24
%34 = load i8*, i8** %11
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 1
%37 = load i8*, i8** %36
%38 = icmp eq i8* %34, %37
br i1 %38, label %44, label %39
39: ; preds = %33
%40 = load i8*, i8** %11
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39, %33
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %207
47: ; preds = %39
br label %48
48: ; preds = %47
%49 = load i32, i32* @PTRACE_SEIZE
%50 = load i32, i32* %6
%51 = load i32, i32* @PTRACE_O_TRACEEXIT
%52 = call i32 (i32, i32, ...) @ptrace(i32 %49, i32 %50, i32 0, i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %56)
store i32 -1, i32* %3
br label %207
58: ; preds = %48
%59 = load i32, i32* %4
%60 = icmp eq i32 %59, 3
br i1 %60, label %61, label %92
61: ; preds = %58
store i32 0, i32* @errno
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = load i8*, i8** %63
%65 = call i64 @strtoll(i8* %64, i8** %11, i32 0)
store i64 %65, i64* %12
%66 = load i32, i32* @errno
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %61
%69 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %207
70: ; preds = %61
%71 = load i8*, i8** %11
%72 = load i8**, i8*** %5
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load i8*, i8** %73
%75 = icmp eq i8* %71, %74
br i1 %75, label %81, label %76
76: ; preds = %70
%77 = load i8*, i8** %11
%78 = load i8, i8* %77
%79 = sext i8 %78 to i32
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %76, %70
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %207
84: ; preds = %76
br label %85
85: ; preds = %84
%86 = load i64, i64* %12
%87 = load i32, i32* %6
%88 = call i32 @setup_count(i64 %86, i32 %87)
store i32 %88, i32* %8
%89 = load i32, i32* %6
%90 = load i32, i32* @SIGCONT
%91 = call i32 @kill(i32 %89, i32 %90)
br label %92
92: ; preds = %85, %58
br label %93
93: ; preds = %92, %170
%94 = load i32, i32* %6
%95 = call i32 @waitpid(i32 %94, i32* %9, i32 0)
store i32 %95, i32* %7
%96 = load i32, i32* %7
%97 = load i32, i32* %6
%98 = icmp ne i32 %96, %97
br i1 %98, label %99, label %104
99: ; preds = %93
%100 = load i32, i32* @stderr
%101 = load i32, i32* %6
%102 = load i32, i32* %7
%103 = call i32 (i32, i8*, ...) @fprintf(i32 %100, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %101, i32 %102)
store i32 -1, i32* %10
br label %171
104: ; preds = %93
%105 = load i32, i32* %9
%106 = ashr i32 %105, 8
%107 = load i32, i32* @SIGTRAP
%108 = load i32, i32* @PTRACE_EVENT_EXIT
%109 = shl i32 %108, 8
%110 = or i32 %107, %109
%111 = icmp eq i32 %106, %110
br i1 %111, label %112, label %113
112: ; preds = %104
br label %171
113: ; preds = %104
%114 = load i32, i32* %9
%115 = call i64 @WIFSTOPPED(i32 %114)
%116 = icmp ne i64 %115, 0
br i1 %116, label %117, label %166
117: ; preds = %113
%118 = load i32, i32* %9
%119 = ashr i32 %118, 16
%120 = load i32, i32* @PTRACE_EVENT_STOP
%121 = icmp ne i32 %119, %120
br i1 %121, label %142, label %122
122: ; preds = %117
%123 = load i32, i32* %9
%124 = call i64 @WSTOPSIG(i32 %123)
%125 = load i64, i64* @SIGSTOP
%126 = icmp ne i64 %124, %125
br i1 %126, label %127, label %161
127: ; preds = %122
%128 = load i32, i32* %9
%129 = call i64 @WSTOPSIG(i32 %128)
%130 = load i64, i64* @SIGTSTP
%131 = icmp ne i64 %129, %130
br i1 %131, label %132, label %161
132: ; preds = %127
%133 = load i32, i32* %9
%134 = call i64 @WSTOPSIG(i32 %133)
%135 = load i64, i64* @SIGTTIN
%136 = icmp ne i64 %134, %135
br i1 %136, label %137, label %161
137: ; preds = %132
%138 = load i32, i32* %9
%139 = call i64 @WSTOPSIG(i32 %138)
%140 = load i64, i64* @SIGTTOU
%141 = icmp ne i64 %139, %140
br i1 %141, label %142, label %161
142: ; preds = %137, %117
%143 = load i32, i32* %4
%144 = icmp eq i32 %143, 3
br i1 %144, label %145, label %155
145: ; preds = %142
%146 = load i32, i32* %9
%147 = call i64 @WSTOPSIG(i32 %146)
%148 = load i64, i64* @SIGIO
%149 = icmp eq i64 %147, %148
br i1 %149, label %150, label %155
150: ; preds = %145
%151 = load i32, i32* @PTRACE_CONT
%152 = load i32, i32* %6
%153 = load i64, i64* @SIGSTOP
%154 = call i32 (i32, i32, ...) @ptrace(i32 %151, i32 %152, i32 0, i64 %153)
br label %171
155: ; preds = %145, %142
%156 = load i32, i32* @PTRACE_CONT
%157 = load i32, i32* %6
%158 = load i32, i32* %9
%159 = call i64 @WSTOPSIG(i32 %158)
%160 = call i32 (i32, i32, ...) @ptrace(i32 %156, i32 %157, i32 0, i64 %159)
br label %165
161: ; preds = %137, %132, %127, %122
%162 = load i32, i32* @PTRACE_LISTEN
%163 = load i32, i32* %6
%164 = call i32 (i32, i32, ...) @ptrace(i32 %162, i32 %163, i32 0, i32 0)
br label %165
165: ; preds = %161, %155
br label %170
166: ; preds = %113
%167 = load i32, i32* @stderr
%168 = load i32, i32* %6
%169 = call i32 (i32, i8*, ...) @fprintf(i32 %167, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %168)
store i32 -1, i32* %10
br label %171
170: ; preds = %165
br label %93
171: ; preds = %166, %150, %112, %99
%172 = load i32, i32* %4
%173 = icmp eq i32 %172, 3
br i1 %173, label %174, label %200
174: ; preds = %171
%175 = load i32, i32* %8
%176 = load i32, i32* @F_SETFL
%177 = load i32, i32* %8
%178 = load i32, i32* @F_GETFL
%179 = call i32 (i32, i32, ...) @fcntl(i32 %177, i32 %178)
%180 = load i32, i32* @FASYNC
%181 = icmp ne i32 %180, 0
%182 = xor i1 %181, true
%183 = zext i1 %182 to i32
%184 = and i32 %179, %183
%185 = call i32 (i32, i32, ...) @fcntl(i32 %175, i32 %176, i32 %184)
%186 = load i32, i32* %8
%187 = call i32 @read(i32 %186, i64* %12, i32 8)
%188 = sext i32 %187 to i64
%189 = icmp ne i64 %188, 8
br i1 %189, label %190, label %193
190: ; preds = %174
%191 = load i32, i32* @stderr
%192 = call i32 (i32, i8*, ...) @fprintf(i32 %191, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
br label %197
193: ; preds = %174
%194 = load i64, i64* %12
%195 = trunc i64 %194 to i32
%196 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %195)
br label %197
197: ; preds = %193, %190
%198 = load i32, i32* %8
%199 = call i32 @close(i32 %198)
br label %200
200: ; preds = %197, %171
%201 = load i32, i32* %6
%202 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %201)
%203 = load i32, i32* @PTRACE_DETACH
%204 = load i32, i32* %6
%205 = call i32 (i32, i32, ...) @ptrace(i32 %203, i32 %204)
%206 = load i32, i32* %10
store i32 %206, i32* %3
br label %207
207: ; preds = %200, %81, %68, %54, %44, %31, %18
%208 = load i32, i32* %3
ret i32 %208
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [106 x i8] c"Usage: %s pid [instruction-count]\0A\0AWait for any process to finish or run a given number of instructions.\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"Invalid PID\00"
@.str.2 = [13 x i8] c"Invalid PID\0A\00"
@PTRACE_SEIZE = global i32 0
@PTRACE_O_TRACEEXIT = global i32 0
@.str.3 = [33 x i8] c"PTRACE_SEIZE returned error, %d\0A\00"
@.str.4 = [26 x i8] c"Invalid instruction count\00"
@.str.5 = [27 x i8] c"Invalid instruction count\0A\00"
@SIGCONT = global i32 0
@.str.6 = [30 x i8] c"Error waiting for pid %d: %d\0A\00"
@SIGTRAP = global i32 0
@PTRACE_EVENT_EXIT = global i32 0
@PTRACE_EVENT_STOP = global i32 0
@SIGSTOP = global i64 0
@SIGTSTP = global i64 0
@SIGTTIN = global i64 0
@SIGTTOU = global i64 0
@SIGIO = global i64 0
@PTRACE_CONT = global i32 0
@PTRACE_LISTEN = global i32 0
@.str.7 = [43 x i8] c"Unexpected wakeup from waitpid for pid %d\0A\00"
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@FASYNC = global i32 0
@.str.8 = [32 x i8] c"Could not read from perf event\0A\00"
@.str.9 = [27 x i8] c"Counted %lld instructions\0A\00"
@.str.10 = [23 x i8] c"Detaching from pid %d\0A\00"
@PTRACE_DETACH = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast i32* %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = bitcast i64* %5 to i8*
%9 = and i32 %0, -2
%10 = icmp eq i32 %9, 2
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %1
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([106 x i8], [106 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %161
15: ; preds = %2
store i32 0, i32* @errno
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strtol(i8* %17, i8** %4, i32 10)
%19 = load i32, i32* @errno
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %161
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = load i8*, i8** %16
%26 = icmp eq i8* %24, %25
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i8, i8* %24
%29 = icmp eq i8 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27, %23
%31 = load i32, i32* @stderr
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %161
33: ; preds = %27
%34 = load i32, i32* @PTRACE_SEIZE
%35 = load i32, i32* @PTRACE_O_TRACEEXIT
%36 = tail call i32 (i32, i32, ...) @ptrace(i32 %34, i32 %18, i32 0, i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %161
42: ; preds = %33
%43 = icmp eq i32 %0, 3
br i1 %43, label %44, label %66
44: ; preds = %42
store i32 0, i32* @errno
%45 = getelementptr inbounds i8*, i8** %1, i64 2
%46 = load i8*, i8** %45
%47 = call i64 @strtoll(i8* %46, i8** %4, i32 0)
store i64 %47, i64* %5
%48 = load i32, i32* @errno
%49 = icmp eq i32 %48, 0
br i1 %49, label %52, label %50
50: ; preds = %44
%51 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %161
52: ; preds = %44
%53 = load i8*, i8** %4
%54 = load i8*, i8** %45
%55 = icmp eq i8* %53, %54
br i1 %55, label %59, label %56
56: ; preds = %52
%57 = load i8, i8* %53
%58 = icmp eq i8 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %56, %52
%60 = load i32, i32* @stderr
%61 = tail call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
br label %161
62: ; preds = %56
%63 = tail call i32 @setup_count(i64 %47, i32 %18)
%64 = load i32, i32* @SIGCONT
%65 = tail call i32 @kill(i32 %18, i32 %64)
br label %66
66: ; preds = %62, %42
%67 = phi i32 [ %63, %62 ], [ undef, %42 ]
%68 = call i32 @waitpid(i32 %18, i32* %3, i32 0)
%69 = icmp eq i32 %68, %18
br i1 %69, label %74, label %70
70: ; preds = %130, %66
%71 = phi i32 [ %68, %66 ], [ %131, %130 ]
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 %18, i32 %71)
br label %133
74: ; preds = %66, %130
%75 = load i32, i32* %3
%76 = ashr i32 %75, 8
%77 = load i32, i32* @SIGTRAP
%78 = load i32, i32* @PTRACE_EVENT_EXIT
%79 = shl i32 %78, 8
%80 = or i32 %79, %77
%81 = icmp eq i32 %76, %80
br i1 %81, label %133, label %82
82: ; preds = %74
%83 = call i64 @WIFSTOPPED(i32 %75)
%84 = icmp eq i64 %83, 0
br i1 %84, label %127, label %85
85: ; preds = %82
%86 = load i32, i32* %3
%87 = ashr i32 %86, 16
%88 = load i32, i32* @PTRACE_EVENT_STOP
%89 = icmp eq i32 %87, %88
br i1 %89, label %90, label %109
90: ; preds = %85
%91 = call i64 @WSTOPSIG(i32 %86)
%92 = load i64, i64* @SIGSTOP
%93 = icmp eq i64 %91, %92
br i1 %93, label %124, label %94
94: ; preds = %90
%95 = load i32, i32* %3
%96 = call i64 @WSTOPSIG(i32 %95)
%97 = load i64, i64* @SIGTSTP
%98 = icmp eq i64 %96, %97
br i1 %98, label %124, label %99
99: ; preds = %94
%100 = load i32, i32* %3
%101 = call i64 @WSTOPSIG(i32 %100)
%102 = load i64, i64* @SIGTTIN
%103 = icmp eq i64 %101, %102
br i1 %103, label %124, label %104
104: ; preds = %99
%105 = load i32, i32* %3
%106 = call i64 @WSTOPSIG(i32 %105)
%107 = load i64, i64* @SIGTTOU
%108 = icmp eq i64 %106, %107
br i1 %108, label %124, label %109
109: ; preds = %104, %85
br i1 %43, label %110, label %119
110: ; preds = %109
%111 = load i32, i32* %3
%112 = call i64 @WSTOPSIG(i32 %111)
%113 = load i64, i64* @SIGIO
%114 = icmp eq i64 %112, %113
br i1 %114, label %115, label %119
115: ; preds = %110
%116 = load i32, i32* @PTRACE_CONT
%117 = load i64, i64* @SIGSTOP
%118 = call i32 (i32, i32, ...) @ptrace(i32 %116, i32 %18, i32 0, i64 %117)
br label %135
119: ; preds = %110, %109
%120 = load i32, i32* @PTRACE_CONT
%121 = load i32, i32* %3
%122 = call i64 @WSTOPSIG(i32 %121)
%123 = call i32 (i32, i32, ...) @ptrace(i32 %120, i32 %18, i32 0, i64 %122)
br label %130
124: ; preds = %104, %99, %94, %90
%125 = load i32, i32* @PTRACE_LISTEN
%126 = call i32 (i32, i32, ...) @ptrace(i32 %125, i32 %18, i32 0, i32 0)
br label %130
127: ; preds = %82
%128 = load i32, i32* @stderr
%129 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %18)
br label %133
130: ; preds = %119, %124
%131 = call i32 @waitpid(i32 %18, i32* %3, i32 0)
%132 = icmp eq i32 %131, %18
br i1 %132, label %74, label %70
133: ; preds = %74, %127, %70
%134 = phi i32 [ -1, %70 ], [ -1, %127 ], [ 0, %74 ]
br i1 %43, label %135, label %156
135: ; preds = %115, %133
%136 = phi i32 [ 0, %115 ], [ %134, %133 ]
%137 = load i32, i32* @F_SETFL
%138 = load i32, i32* @F_GETFL
%139 = call i32 (i32, i32, ...) @fcntl(i32 %67, i32 %138)
%140 = load i32, i32* @FASYNC
%141 = icmp eq i32 %140, 0
%142 = zext i1 %141 to i32
%143 = and i32 %139, %142
%144 = call i32 (i32, i32, ...) @fcntl(i32 %67, i32 %137, i32 %143)
%145 = call i32 @read(i32 %67, i64* %5, i32 8)
%146 = icmp eq i32 %145, 8
br i1 %146, label %150, label %147
147: ; preds = %135
%148 = load i32, i32* @stderr
%149 = call i32 (i32, i8*, ...) @fprintf(i32 %148, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
br label %154
150: ; preds = %135
%151 = load i64, i64* %5
%152 = trunc i64 %151 to i32
%153 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0), i32 %152)
br label %154
154: ; preds = %150, %147
%155 = call i32 @close(i32 %67)
br label %156
156: ; preds = %154, %133
%157 = phi i32 [ %136, %154 ], [ %134, %133 ]
%158 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %18)
%159 = load i32, i32* @PTRACE_DETACH
%160 = call i32 (i32, i32, ...) @ptrace(i32 %159, i32 %18)
br label %161
161: ; preds = %156, %59, %50, %38, %30, %21, %11
%162 = phi i32 [ -1, %11 ], [ -1, %21 ], [ -1, %30 ], [ -1, %38 ], [ -1, %50 ], [ -1, %59 ], [ %157, %156 ]
ret i32 %162
}