// File: /workspace/github_c_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r15d
movq %rdi, %r14
movl $0x10, 0x8(%rsp)
leaq 0x4(%rdi), %rbx
movq $-0x1, (%rdi)
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movabsq $0x100007f00000002, %rax # imm = 0x100007F00000002
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
movl $0x2, %edi
xorl %edx, %edx
callq 0x3670
movl %eax, 0xc(%rsp)
movb $0x1, %r13b
cmpl $-0x1, %eax
je 0xa2b0
movl %eax, %r12d
movl 0x8(%rsp), %edx
leaq 0x10(%rsp), %rsi
movl %eax, %edi
callq 0x34c0
testl %eax, %eax
jne 0xa2b0
cmpl $0x1, %r15d
jne 0xa29a
movl %r12d, %edi
movl $0x1, %esi
callq 0x3440
testl %eax, %eax
jne 0xa2b0
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movl %r12d, %edi
callq 0x3560
testl %eax, %eax
je 0xa2f2
testb %r13b, %r13b
je 0xa2e1
cmpl $-0x1, (%r14)
je 0xa2c3
movq %r14, %rdi
callq 0xa9c1
cmpl $-0x1, (%rbx)
je 0xa2d0
movq %rbx, %rdi
callq 0xa9c1
cmpl $-0x1, 0xc(%rsp)
je 0xa2e1
leaq 0xc(%rsp), %rdi
callq 0xa9c1
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
movl $0x2, %edi
movl %r15d, %esi
xorl %edx, %edx
callq 0x3670
movl %eax, (%r14)
cmpl $-0x1, %eax
je 0xa2b0
movl 0x8(%rsp), %edx
leaq 0x10(%rsp), %rsi
movl %eax, %edi
callq 0x35c0
testl %eax, %eax
jne 0xa2b0
cmpl $0x2, %r15d
jne 0xa35a
movl (%r14), %edi
leaq 0x10(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x3560
testl %eax, %eax
jne 0xa2b0
movl 0x8(%rsp), %edx
leaq 0x10(%rsp), %rsi
movl %r12d, %edi
callq 0x35c0
testl %eax, %eax
jne 0xa2b0
jmp 0xa36e
movl 0x8(%rsp), %edx
leaq 0x10(%rsp), %rsi
movl %r12d, %edi
callq 0xa980
movl %eax, %r12d
movl %r12d, (%rbx)
cmpl $-0x1, %r12d
je 0xa2b0
movl (%r14), %edi
xorl %r13d, %r13d
movl $0x1, %ebp
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, %r15d
jne 0xa2b0
leaq 0xc(%rsp), %rdi
callq 0xa9c1
xorl %r13d, %r13d
jmp 0xa2b0