// File: /workspace/github_c_makefile_star2_O2/motet-a[P]libforks/examples/fizz_buzz_threads.c
// Function: main
int main() {
  assert(libforks_start(&conn) == libforks_OK);

  int exit_fd;

  assert(libforks_fork(
    conn,
    NULL, // pid_ptr
    &threads_output_fd, // socket_fd_ptr
    &exit_fd, // exit_fd_ptr
    sort_main
  ) == libforks_OK);

  pthread_t fizzer, buzzer;
  assert(pthread_create(&fizzer, NULL, fizzer_main, NULL) == 0);
  assert(pthread_create(&buzzer, NULL, buzzer_main, NULL) == 0);

  void *res;
  assert(pthread_join(fizzer, &res) == 0);
  assert(pthread_join(buzzer, &res) == 0);

  // `shutdown` is necessary here to send EOF to sort’s stdin
  assert(shutdown(threads_output_fd, SHUT_WR) == 0);
  assert(close(threads_output_fd) == 0);

  // `sort` exits here, the fork server receives SIGCHLD and writes
  // the exit event to `exit_fd`. The event stays in the `exit_fd`
  // buffer until we read it so there’s no race condition.

  libforks_ExitEvent ee;
  assert(read(exit_fd, &ee, sizeof ee) == sizeof ee);
  close(exit_fd);

  assert(libforks_stop(conn) == libforks_OK);
  return 0;
}

// Assembly:
subq $0x28, %rsp
leaq 0x4df1(%rip), %rdi      # 0x6138
callq 0x1dee
testl %eax, %eax
jne 0x144d
movq 0x4ddd(%rip), %rdi      # 0x6138
leaq 0x4dde(%rip), %rdx      # 0x6140
leaq 0x212(%rip), %r8        # 0x157b
leaq 0x4(%rsp), %rcx
xorl %esi, %esi
callq 0x26ea
testl %eax, %eax
jne 0x1466
leaq 0x272(%rip), %rdx       # 0x15f6
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x11b0
testl %eax, %eax
jne 0x1483
leaq 0x694(%rip), %rdx       # 0x1a35
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x11b0
testl %eax, %eax
jne 0x14a2
movq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1200
testl %eax, %eax
jne 0x14c1
movq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1200
testl %eax, %eax
jne 0x14e0
movl 0x4d55(%rip), %edi      # 0x6140
pushq $0x1
popq %rsi
callq 0x10a0
testl %eax, %eax
jne 0x14ff
movl 0x4d3f(%rip), %edi      # 0x6140
callq 0x10f0
testl %eax, %eax
jne 0x151e
movl 0x4(%rsp), %edi
leaq 0x20(%rsp), %rsi
pushq $0x8
popq %rdx
callq 0x1120
cmpq $0x8, %rax
jne 0x153d
movl 0x4(%rsp), %edi
callq 0x10f0
movq 0x4cff(%rip), %rdi      # 0x6138
callq 0x288e
testl %eax, %eax
jne 0x155c
xorl %eax, %eax
addq $0x28, %rsp
retq
leaq 0x1bb0(%rip), %rdi      # 0x3004
leaq 0x1bce(%rip), %rsi      # 0x3029
leaq 0x1be4(%rip), %rcx      # 0x3046
pushq $0x73
jmp 0x147d
leaq 0x1be4(%rip), %rdi      # 0x3051
leaq 0x1bb5(%rip), %rsi      # 0x3029
leaq 0x1bcb(%rip), %rcx      # 0x3046
pushq $0x7d
popq %rdx
callq 0x10e0
leaq 0x1c1b(%rip), %rdi      # 0x30a5
leaq 0x1b98(%rip), %rsi      # 0x3029
leaq 0x1bae(%rip), %rcx      # 0x3046
movl $0x80, %edx
callq 0x10e0
leaq 0x1c32(%rip), %rdi      # 0x30db
leaq 0x1b79(%rip), %rsi      # 0x3029
leaq 0x1b8f(%rip), %rcx      # 0x3046
movl $0x81, %edx
callq 0x10e0
leaq 0x1c49(%rip), %rdi      # 0x3111
leaq 0x1b5a(%rip), %rsi      # 0x3029
leaq 0x1b70(%rip), %rcx      # 0x3046
movl $0x84, %edx
callq 0x10e0
leaq 0x1c4a(%rip), %rdi      # 0x3131
leaq 0x1b3b(%rip), %rsi      # 0x3029
leaq 0x1b51(%rip), %rcx      # 0x3046
movl $0x85, %edx
callq 0x10e0
leaq 0x1c4b(%rip), %rdi      # 0x3151
leaq 0x1b1c(%rip), %rsi      # 0x3029
leaq 0x1b32(%rip), %rcx      # 0x3046
movl $0x88, %edx
callq 0x10e0
leaq 0x1c56(%rip), %rdi      # 0x317b
leaq 0x1afd(%rip), %rsi      # 0x3029
leaq 0x1b13(%rip), %rcx      # 0x3046
movl $0x89, %edx
callq 0x10e0
leaq 0x1c55(%rip), %rdi      # 0x3199
leaq 0x1ade(%rip), %rsi      # 0x3029
leaq 0x1af4(%rip), %rcx      # 0x3046
movl $0x90, %edx
callq 0x10e0
leaq 0x1c61(%rip), %rdi      # 0x31c4
leaq 0x1abf(%rip), %rsi      # 0x3029
leaq 0x1ad5(%rip), %rcx      # 0x3046
movl $0x93, %edx
callq 0x10e0