// File: /workspace/github_c_makefile_star2_O3/motet-a[P]libforks/tests/main.c
// Function: main
int main() {
  DIR *dir = opendir(".");
  check(dir);

  int exit_status = 0;

  const struct dirent *entry;
  while ((entry = readdir(dir)) != NULL) {
    if (strchr(entry->d_name, '.')) {
      continue;
    }
    if (strcmp(entry->d_name, "main") == 0) {
      continue;
    }

    printf("testing %s...", entry->d_name);
    fflush(stdout);
    char exec_path[64];
    check(snprintf(exec_path, sizeof exec_path, "./%s", entry->d_name) != -1);

    char *argv[] = {exec_path, NULL};
    char *envp[] = {
      "MALLOC_OPTIONS=S", // security options for OpenBSD
      NULL
    };

    pid_t child_pid = fork();
    check(child_pid != -1);

    if (child_pid == 0) {
      check(setpgid(getpid(), getpid()) == 0);
      check(execve(exec_path, argv, envp));
      abort();
    }

    char failure[99];
    failure[0] = '\0';

    int wait_status;
    check(waitpid(child_pid, &wait_status, 0) > 0);
    if (WIFEXITED(wait_status)) {
      if (WEXITSTATUS(wait_status) != 0) {
        snprintf(failure, sizeof failure, "exited with status %d", WEXITSTATUS(wait_status));
      }
    } else if (WIFSIGNALED(wait_status)) {
      snprintf(failure, sizeof failure, "received signal %d", WTERMSIG(wait_status));
    } else {
      abort();
    }

    // kill the whole process group
    int kill_res = killpg(child_pid, SIGKILL);
    if (kill_res == 0) {
      snprintf(failure, sizeof failure, "children were still running");
    } else {
      if (errno != ESRCH) {
        perror("killpg");
        abort();
      }
    }

    if (*failure) {
      exit_status = 1;
      printf(" error: %s\n", failure);
    } else {
      printf(" ok\n");
    }
  }

  check(closedir(dir) == 0);

  return exit_status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
leaq 0x1c42(%rip), %rdi      # 0x3026
callq 0x10b0
testq %rax, %rax
je 0x15ca
movq %rax, %rbx
movq %rax, %rdi
callq 0x11d0
testq %rax, %rax
je 0x15a6
movq %rax, %rbp
leaq 0x1c05(%rip), %r14      # 0x3015
leaq 0xa0(%rsp), %r13
leaq 0x30(%rsp), %r15
xorl %r12d, %r12d
addq $0x13, %rbp
movq %rbp, %rdi
movl $0x2e, %esi
callq 0x10c0
testq %rax, %rax
jne 0x1445
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1160
testl %eax, %eax
jne 0x145a
movq %rbx, %rdi
callq 0x11d0
movq %rax, %rbp
testq %rax, %rax
jne 0x1420
jmp 0x15a9
leaq 0x1bb9(%rip), %rdi      # 0x301a
movq %rbp, %rsi
xorl %eax, %eax
callq 0x10d0
movq 0x4b5e(%rip), %rax      # 0x5fd0
movq (%rax), %rdi
callq 0x11f0
movl $0x40, %esi
movq %r13, %rdi
leaq 0x1b9f(%rip), %rdx      # 0x3028
movq %rbp, %rcx
xorl %eax, %eax
callq 0x10e0
cmpl $-0x1, %eax
je 0x1653
movq %r13, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movaps 0x491f(%rip), %xmm0     # 0x5dd0
movaps %xmm0, 0x20(%rsp)
callq 0x1290
testl %eax, %eax
je 0x160e
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x15fd
movb $0x0, 0x30(%rsp)
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
jle 0x15ec
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x151a
movl %ecx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x1664
movl $0x63, %esi
movq %r15, %rdi
leaq 0x1bf6(%rip), %rdx      # 0x310e
jmp 0x1535
shrl $0x8, %eax
andl $0xff, %eax
je 0x153c
movl $0x63, %esi
movq %r15, %rdi
leaq 0x1bc5(%rip), %rdx      # 0x30f8
movl %eax, %ecx
xorl %eax, %eax
callq 0x10e0
movl %ebp, %edi
movl $0x9, %esi
callq 0x1200
testl %eax, %eax
je 0x1572
callq 0x1050
cmpl $0x3, (%rax)
jne 0x1669
cmpb $0x0, 0x30(%rsp)
jne 0x158a
leaq 0x1c17(%rip), %rdi      # 0x317f
callq 0x1070
jmp 0x1445
movups 0x1bb4(%rip), %xmm0     # 0x312d
movups %xmm0, 0x3c(%rsp)
movups 0x1b9c(%rip), %xmm0     # 0x3121
movaps %xmm0, 0x30(%rsp)
leaq 0x1bb3(%rip), %rdi      # 0x3144
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
movl $0x1, %r12d
jmp 0x1445
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x1130
testl %eax, %eax
jne 0x15db
movl %r12d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a33(%rip), %rdi      # 0x3004
movl $0x8, %esi
callq 0x167f
leaq 0x1b6e(%rip), %rdi      # 0x3150
movl $0x4b, %esi
callq 0x167f
leaq 0x1add(%rip), %rdi      # 0x30d0
movl $0x2d, %esi
callq 0x167f
leaq 0x1a7d(%rip), %rdi      # 0x3081
movl $0x21, %esi
callq 0x167f
callq 0x10a0
movl %eax, %ebx
callq 0x10a0
movl %ebx, %edi
movl %eax, %esi
callq 0x1260
testl %eax, %eax
jne 0x166e
leaq 0xa0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x1150
testl %eax, %eax
jne 0x1664
leaq 0x1a69(%rip), %rdi      # 0x30b2
movl $0x25, %esi
callq 0x167f
leaq 0x19d3(%rip), %rdi      # 0x302d
movl $0x18, %esi
callq 0x167f
callq 0x1040
callq 0x12b0
leaq 0x1a1c(%rip), %rdi      # 0x3091
movl $0x24, %esi
callq 0x167f