// File: /workspace/github_c_makefile_star2_O3/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_socketpair
int mg_socketpair(sock_t sp[2], int sock_type) {
  union socket_address sa;
  sock_t sock;
  socklen_t len = sizeof(sa.sin);
  int ret = 0;

  sock = sp[0] = sp[1] = INVALID_SOCKET;

  (void) memset(&sa, 0, sizeof(sa));
  sa.sin.sin_family = AF_INET;
  sa.sin.sin_port = htons(0);
  sa.sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */

  if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
  } else if (bind(sock, &sa.sa, len) != 0) {
  } else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) {
  } else if (getsockname(sock, &sa.sa, &len) != 0) {
  } else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
  } else if (connect(sp[0], &sa.sa, len) != 0) {
  } else if (sock_type == SOCK_DGRAM &&
             (getsockname(sp[0], &sa.sa, &len) != 0 ||
              connect(sock, &sa.sa, len) != 0)) {
  } else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept(
                                                            sock, &sa, len))) ==
             INVALID_SOCKET) {
  } else {
    mg_set_close_on_exec(sp[0]);
    mg_set_close_on_exec(sp[1]);
    if (sock_type == SOCK_STREAM) mg_socketpair_close(&sock);
    ret = 1;
  }

  if (!ret) {
    if (sp[0] != INVALID_SOCKET) mg_socketpair_close(&sp[0]);
    if (sp[1] != INVALID_SOCKET) mg_socketpair_close(&sp[1]);
    if (sock != INVALID_SOCKET) mg_socketpair_close(&sock);
  }

  return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebx
movq %rdi, %r14
movl $0x10, (%rsp)
leaq 0x4(%rdi), %r15
movq $-0x1, (%rdi)
movq $0x0, 0x10(%rsp)
movabsq $0x100007f00000002, %rax # imm = 0x100007F00000002
movq %rax, 0x8(%rsp)
movl $0x2, %edi
xorl %edx, %edx
callq 0x3670
movl %eax, %ebp
movl %eax, 0x4(%rsp)
cmpl $-0x1, %eax
je 0xa1e9
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
movl $0x10, %edx
callq 0x34c0
testl %eax, %eax
jne 0xa1e9
cmpl $0x1, %ebx
jne 0xa1d6
movl %ebp, %edi
movl $0x1, %esi
callq 0x3440
testl %eax, %eax
jne 0xa1e9
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
movl %ebp, %edi
callq 0x3560
testl %eax, %eax
je 0xa225
cmpl $-0x1, (%r14)
je 0xa1f7
movq %r14, %rdi
callq 0xa87c
cmpl $-0x1, (%r15)
je 0xa205
movq %r15, %rdi
callq 0xa87c
xorl %r15d, %r15d
cmpl $-0x1, %ebp
je 0xa217
leaq 0x4(%rsp), %rdi
callq 0xa87c
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, %edi
movl %ebx, %esi
xorl %edx, %edx
callq 0x3670
movl %eax, (%r14)
cmpl $-0x1, %eax
je 0xa1f7
movl (%rsp), %edx
leaq 0x8(%rsp), %rsi
movl %eax, %edi
callq 0x35c0
testl %eax, %eax
jne 0xa1e9
cmpl $0x2, %ebx
jne 0xa283
movl (%r14), %edi
leaq 0x8(%rsp), %rsi
movq %rsp, %rdx
callq 0x3560
testl %eax, %eax
jne 0xa1e9
movl (%rsp), %edx
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x35c0
testl %eax, %eax
jne 0xa1e9
movl %ebp, (%r15)
jmp 0xa29e
movl (%rsp), %edx
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0xa83b
movl %eax, (%r15)
cmpl $-0x1, %eax
je 0xa1e9
movl (%r14), %edi
movl $0x1, %r15d
movl $0x2, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x34f0
movl 0x4(%r14), %edi
movl $0x2, %esi
movl $0x1, %edx
xorl %eax, %eax
callq 0x34f0
cmpl $0x1, %ebx
je 0xa20d
jmp 0xa217