// File: /workspace/github_c_makefile_star2_O2/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
movq %rdi, %rbx
movl $0x10, 0xc(%rsp)
orq $-0x1, (%rdi)
andq $0x0, 0x1c(%rsp)
movl %esi, %ebp
leaq 0x4(%rdi), %r14
movabsq $0x100007f00000002, %rax # imm = 0x100007F00000002
movq %rax, 0x14(%rsp)
pushq $0x2
popq %rdi
xorl %edx, %edx
callq 0x3670
movl %eax, %r15d
movl %eax, 0x10(%rsp)
cmpl $-0x1, %eax
je 0x9b95
leaq 0x14(%rsp), %rsi
pushq $0x10
popq %rdx
movl %r15d, %edi
callq 0x34c0
testl %eax, %eax
jne 0x9b95
cmpl $0x1, %ebp
jne 0x9b7f
pushq $0x1
popq %rsi
movl %r15d, %edi
callq 0x3440
testl %eax, %eax
jne 0x9b95
leaq 0x14(%rsp), %rsi
leaq 0xc(%rsp), %rdx
movl %r15d, %edi
callq 0x3560
testl %eax, %eax
je 0x9bd3
cmpl $-0x1, (%rbx)
je 0x9ba2
movq %rbx, %rdi
callq 0xa0ab
cmpl $-0x1, (%r14)
je 0x9bb0
movq %r14, %rdi
callq 0xa0ab
xorl %ebx, %ebx
cmpl $-0x1, %r15d
je 0x9bc2
leaq 0x10(%rsp), %rdi
callq 0xa0ab
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x2
popq %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x3670
movl %eax, (%rbx)
cmpl $-0x1, %eax
je 0x9ba2
movl 0xc(%rsp), %edx
leaq 0x14(%rsp), %rsi
movl %eax, %edi
callq 0x35c0
testl %eax, %eax
jne 0x9b95
cmpl $0x2, %ebp
jne 0x9c2f
movl (%rbx), %edi
leaq 0x14(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x3560
testl %eax, %eax
jne 0x9b95
movl 0xc(%rsp), %edx
leaq 0x14(%rsp), %rsi
movl %r15d, %edi
callq 0x35c0
testl %eax, %eax
jne 0x9b95
jmp 0x9c6a
movl 0xc(%rsp), %eax
leaq 0x24(%rsp), %r12
movl %eax, (%r12)
leaq 0x14(%rsp), %r13
movl %r15d, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3550
cmpl $-0x1, %eax
jne 0x9c67
callq 0x30a0
cmpl $0x4, (%rax)
je 0x9c41
orl $-0x1, (%r14)
jmp 0x9b95
movl %eax, %r15d
movl %r15d, 0x4(%rbx)
movl (%rbx), %edi
callq 0x9abd
movl 0x4(%rbx), %edi
callq 0x9abd
pushq $0x1
popq %rbx
cmpl $0x1, %ebp
je 0x9bb8
jmp 0x9bc2