// File: /workspace/github_c_makefile_star2_O0/motet-a[P]libforks/libforks.c
// Function: serv_handle_fork_request
static void serv_handle_fork_request(
    const ClientMessage *req,
    serv_Client **first_client_ptr, // in/out
    size_t *client_count_ptr, // in/out
    serv_Client *parent) {
  serv_DEBUG("FORK_REQUEST received\n");

  if (*client_count_ptr == serv_MAX_CLIENTS) {
    ServerMessage res = {
      .type = ServerMessageType_FORK_FAILURE,
      .u = {
        .fork_failure = {
          .error_code = libforks_TOO_MANY_CLIENTS_ERROR,
          .saved_errno = 0,
        },
      },
    };
    serv_send(parent->socket, res);
    return;
  }

  pid_t server_pid = getpid();

  int sockets[2]; // for private child-server communication
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
    ServerMessage res = {
      .type = ServerMessageType_FORK_FAILURE,
      .u = {
        .fork_failure = {
          .error_code = libforks_SOCKET_CREATION_ERROR,
          .saved_errno = errno,
        },
      },
    };
    serv_send(parent->socket, res);
    return;
  }
  int server_socket = sockets[0]; // server end
  int child_socket = sockets[1]; // child end

  int user_sockets[2] = {-1, -1}; // for public parent-child communication
  if (req->u.fork_request.create_user_socket) {
    if (socketpair(AF_UNIX, SOCK_STREAM, 0, user_sockets) == -1) {
      ServerMessage res = {
        .type = ServerMessageType_FORK_FAILURE,
        .u = {
          .fork_failure = {
            .error_code = libforks_SOCKET_CREATION_ERROR,
            .saved_errno = errno,
          },
        },
      };
      serv_send(parent->socket, res);
      goto destroy_socket;
    }
  }
  int parent_user_socket = user_sockets[0]; // parent end
  int child_user_socket = user_sockets[1]; // child end

  pid_t child_pid = fork();
  if (child_pid == -1) {
    ServerMessage res = {
      .type = ServerMessageType_FORK_FAILURE,
      .u = {
        .fork_failure = {
          .error_code = libforks_FORK_ERROR,
          .saved_errno = errno,
        },
      },
    };
    serv_send(parent->socket, res);
    goto destroy_user_socket;
  }

  if (child_pid == 0) {
    // We are in the new child process

    close(server_socket);
    if (parent_user_socket != -1) {
      close(parent_user_socket);
    }

    // Release server resources
    serv_Client *client = *first_client_ptr;
    while (client) {
      serv_Client *next = client->next;
      if (client->exit_fd != -1) {
        close(client->exit_fd);
      }
      if (client->socket != -1) {
        close(client->socket);
      }
      free(client);
      client = next;
    }
    close(serv_exit_pipe[READ_END]);
    close(serv_exit_pipe[WRITE_END]);

    serv_uninstall_signal_handler(SIGTERM);
    serv_uninstall_signal_handler(SIGCHLD);

    ServerConn *child_conn = malloc(sizeof(ServerConn));
    if (!child_conn) {
      // XXX using `serv_*` functions here is not very correct
      // because we are in the child
      serv_print_errno("malloc");
      serv_panic();
    }
    child_conn->server_pid = server_pid;
    child_conn->socket = child_socket;
    libforks_ServerConn conn_p = {.private = child_conn};

    // Inform the parent that we are ready.
    if (write_exact(child_socket, "r", 1) == -1) {
      // XXX not very correct as well
      serv_print_errno("write_exact");
      serv_panic();
    }

    req->u.fork_request.entrypoint(conn_p, child_user_socket);
    exit(0);
  }

  serv_DEBUG("created child process %d\n", (int)child_pid);

  close(child_socket);
  if (child_user_socket != -1) {
    close(child_user_socket);
  }

  serv_Client *client = malloc(sizeof(serv_Client));
  if (!client) {
    serv_print_errno("malloc");
    serv_panic();
  }

  client->pid = child_pid;
  client->main = false;
  client->socket = server_socket;
  client->exit_fd = -1;
  client->next = *first_client_ptr;

  int exit_pipe[2] = {-1, -1};
  if (req->u.fork_request.create_exit_pipe) {
    if (pipe(exit_pipe) == -1) {
      serv_print_errno("pipe");
      serv_panic();
    }
    client->exit_fd = exit_pipe[WRITE_END];
  }
  serv_DEBUG("created exit pipe {%d, %d}\n", exit_pipe[0], exit_pipe[1]);

  *first_client_ptr = client;
  (*client_count_ptr)++;

  int fds_to_send[2];
  size_t fd_count = 0;

  if (parent_user_socket != -1) {
    fds_to_send[fd_count++] = parent_user_socket;
  }
  if (exit_pipe[READ_END] != -1) {
    fds_to_send[fd_count++] = exit_pipe[READ_END];
  }

  // Wait until the child is ready. This is necessary because if
  // libforks_stop() is called just after libforks_fork(), the child
  // process may not have uninstalled its SIGTERM handler yet and
  // may ignore the signal sent by libforks_stop().
  char ready_char;
  if (read_exact(server_socket, &ready_char, 1) == -1 || ready_char != 'r') {
    serv_print_errno("read_exact");
    serv_panic();
  }

  serv_DEBUG("sending %d file descriptor(s)\n", (int)fd_count);
  ServerMessage res = {
    .type = ServerMessageType_FORK_SUCCESS,
    .u = {
      .fork_success = {
        .pid = child_pid,
      },
    },
  };
  serv_send_fds(parent->socket, res, fds_to_send, fd_count);
  if (exit_pipe[READ_END] != -1) {
    close(exit_pipe[READ_END]);
  }
  if (parent_user_socket != -1) {
    close(parent_user_socket);
  }
  return;

destroy_user_socket:
  close(parent_user_socket);
  close(child_user_socket);
destroy_socket:
  close(server_socket);
  close(child_socket);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x14, (%rax)
jne 0x2e3f
movq 0x1370(%rip), %rax      # 0x417c
movq %rax, -0x2c(%rbp)
movl 0x136e(%rip), %eax      # 0x4184
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x2c(%rbp), %rax
movq %rax, -0x3c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x3c(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x3410
jmp 0x333d
callq 0x1090
movl %eax, -0x40(%rbp)
leaq -0x48(%rbp), %rcx
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1150
cmpl $-0x1, %eax
jne 0x2e9c
movl $0xfffffffd, -0x54(%rbp) # imm = 0xFFFFFFFD
callq 0x1050
movl (%rax), %eax
movl %eax, -0x50(%rbp)
movl $0x1, -0x4c(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x54(%rbp), %rax
movq %rax, -0x64(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x64(%rbp), %rsi
movl -0x5c(%rbp), %edx
callq 0x3410
jmp 0x333d
movl -0x48(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x44(%rbp), %eax
movl %eax, -0x6c(%rbp)
movq 0x1945(%rip), %rax      # 0x47f4
movq %rax, -0x74(%rbp)
movq -0x8(%rbp), %rax
testb $0x1, (%rax)
je 0x2f1f
leaq -0x74(%rbp), %rcx
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1150
cmpl $-0x1, %eax
jne 0x2f1d
movl $0xfffffffd, -0x80(%rbp) # imm = 0xFFFFFFFD
callq 0x1050
movl (%rax), %eax
movl %eax, -0x7c(%rbp)
movl $0x1, -0x78(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edi
movq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movl -0x78(%rbp), %eax
movl %eax, -0x88(%rbp)
movq -0x90(%rbp), %rsi
movl -0x88(%rbp), %edx
callq 0x3410
jmp 0x332d
jmp 0x2f1f
movl -0x74(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x98(%rbp)
callq 0x1200
movl %eax, -0x9c(%rbp)
cmpl $-0x1, -0x9c(%rbp)
jne 0x2f9e
movl $0xfffffffb, -0xa8(%rbp) # imm = 0xFFFFFFFB
callq 0x1050
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
movl $0x1, -0xa0(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edi
movq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0xb0(%rbp)
movq -0xb8(%rbp), %rsi
movl -0xb0(%rbp), %edx
callq 0x3410
jmp 0x3317
cmpl $0x0, -0x9c(%rbp)
jne 0x310a
movl -0x68(%rbp), %edi
callq 0x1c50
cmpl $-0x1, -0x94(%rbp)
je 0x2fc7
movl -0x94(%rbp), %edi
callq 0x1c50
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x3045
movq -0xc0(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
cmpl $-0x1, 0xc(%rax)
je 0x300d
movq -0xc0(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x1c50
movq -0xc0(%rbp), %rax
cmpl $-0x1, 0x8(%rax)
je 0x3029
movq -0xc0(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x1c50
movq -0xc0(%rbp), %rdi
callq 0x1030
movq -0xc8(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x2fd5
movl 0x40d1(%rip), %edi      # 0x711c
callq 0x1c50
movl 0x40ca(%rip), %edi      # 0x7120
callq 0x1c50
movl $0xf, %edi
callq 0x33b0
movl $0x11, %edi
callq 0x33b0
movl $0x8, %edi
callq 0x1170
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xd0(%rbp)
jne 0x309b
leaq 0x11bb(%rip), %rdi      # 0x424c
callq 0x1c90
callq 0x2830
movl -0x40(%rbp), %ecx
movq -0xd0(%rbp), %rax
movl %ecx, (%rax)
movl -0x6c(%rbp), %ecx
movq -0xd0(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0xd0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0x6c(%rbp), %edi
leaq 0x159f(%rip), %rsi      # 0x466b
movl $0x1, %edx
callq 0x1aa0
cmpl $-0x1, %eax
jne 0x30ec
leaq 0x16cf(%rip), %rdi      # 0x47b1
callq 0x1c90
callq 0x2830
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x98(%rbp), %esi
movq -0xd8(%rbp), %rdi
callq *%rax
xorl %edi, %edi
callq 0x11d0
movl -0x6c(%rbp), %edi
callq 0x1c50
cmpl $-0x1, -0x98(%rbp)
je 0x3126
movl -0x98(%rbp), %edi
callq 0x1c50
movl $0x18, %edi
callq 0x1170
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xe0(%rbp)
jne 0x3152
leaq 0x1104(%rip), %rdi      # 0x424c
callq 0x1c90
callq 0x2830
movl -0x9c(%rbp), %ecx
movq -0xe0(%rbp), %rax
movl %ecx, (%rax)
movq -0xe0(%rbp), %rax
movb $0x0, 0x4(%rax)
movl -0x68(%rbp), %ecx
movq -0xe0(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xe0(%rbp), %rax
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0xe0(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x1654(%rip), %rax      # 0x47f4
movq %rax, -0xe8(%rbp)
movq -0x8(%rbp), %rax
testb $0x1, 0x1(%rax)
je 0x31e3
leaq -0xe8(%rbp), %rdi
callq 0x10f0
cmpl $-0x1, %eax
jne 0x31d3
leaq 0x131e(%rip), %rdi      # 0x44e7
callq 0x1c90
callq 0x2830
movl -0xe4(%rbp), %ecx
movq -0xe0(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0xe0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movq $0x0, -0xf8(%rbp)
cmpl $-0x1, -0x94(%rbp)
je 0x3235
movl -0x94(%rbp), %ecx
movq -0xf8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xf8(%rbp)
movl %ecx, -0xf0(%rbp,%rax,4)
cmpl $-0x1, -0xe8(%rbp)
je 0x3260
movl -0xe8(%rbp), %ecx
movq -0xf8(%rbp), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, -0xf8(%rbp)
movl %ecx, -0xf0(%rbp,%rax,4)
movl -0x68(%rbp), %edi
leaq -0xf9(%rbp), %rsi
movl $0x1, %edx
callq 0x1830
cmpl $-0x1, %eax
je 0x3285
movsbl -0xf9(%rbp), %eax
cmpl $0x72, %eax
je 0x3296
leaq 0x1306(%rip), %rdi      # 0x4592
callq 0x1c90
callq 0x2830
movl -0x9c(%rbp), %eax
movl %eax, -0x108(%rbp)
movl $0x0, -0x100(%rbp)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %edi
leaq -0xf0(%rbp), %rcx
movq -0xf8(%rbp), %r8
movq -0x108(%rbp), %rax
movq %rax, -0x118(%rbp)
movl -0x100(%rbp), %eax
movl %eax, -0x110(%rbp)
movq -0x118(%rbp), %rsi
movl -0x110(%rbp), %edx
callq 0x3460
cmpl $-0x1, -0xe8(%rbp)
je 0x3301
movl -0xe8(%rbp), %edi
callq 0x1c50
cmpl $-0x1, -0x94(%rbp)
je 0x3315
movl -0x94(%rbp), %edi
callq 0x1c50
jmp 0x333d
movl -0x94(%rbp), %edi
callq 0x1c50
movl -0x98(%rbp), %edi
callq 0x1c50
movl -0x68(%rbp), %edi
callq 0x1c50
movl -0x6c(%rbp), %edi
callq 0x1c50
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)