// File: /workspace/github_c_makefile_star2_O3/GregorR[P]lstg/source/lst_socket_primitives.c
// Function: lstInetPrimitive
struct object *
lstInetPrimitive(int primitiveNumber, struct object *args, int *failed)
{
    struct object *returnedValue = nilObject;
    switch (integerValue(args->data[0])) {

        case 0: {   /* Inet firstIpStringFromHostname: hostName -> IPv4 address string */
            char hostname[256];
            lstGetString(hostname, sizeof(hostname), args->data[1]);
            struct hostent *h = gethostbyname(hostname);
            if (h && h->h_addrtype == AF_INET) {
                char addr[16];
                inet_ntop(h->h_addrtype, h->h_addr_list[0], addr, sizeof(addr));
                returnedValue = lstNewString(addr);
            }
            break;
        }

        case 1: {   /* Inet firstIpAddressFromHostname: hostName -> IPv4 in_addr binary */
            char hostname[256];
            lstGetString(hostname, sizeof(hostname), args->data[1]);
            struct hostent *h = gethostbyname(hostname);
            if (h && h->h_addrtype == AF_INET)
                returnedValue = lstNewBinary(h->h_addr_list[0], h->h_length);
            break;
        }

        case 2: {   /* Inet newIpAddressFromIpString: ipStr -> IPv4 in_addr binary */
            char ipStr[16];
            lstGetString(ipStr, sizeof(ipStr), args->data[1]);
            struct in_addr addr;
            int rc = inet_aton(ipStr, &addr);
            if (1 == rc)
                returnedValue = lstNewBinary(&addr, sizeof(addr));
            break;
        }

        case 3: {   /* Inet newSocketAddressFromIpAddress: in_addr port: port -> socketaddr_in binary */
            struct byteObject *addrObj = (struct byteObject *) args->data[1];
            struct sockaddr_in in;
            in.sin_family = AF_INET;
            in.sin_port = htons(integerValue(args->data[2]));
            memcpy(&in.sin_addr, addrObj->bytes, SIZE(addrObj));
            returnedValue = lstNewBinary(&in, sizeof(in));
            break;
        }

        case 4: {   /* Inet newSocketAddressFromIpString: ipStr port: port -> socketaddr_in binary */
            char ipStr[16];
            lstGetString(ipStr, sizeof(ipStr), args->data[1]);
            struct in_addr addr;
            int rc = inet_aton(ipStr, &addr);
            if (1 == rc) {
                struct sockaddr_in in;
                in.sin_family = AF_INET;
                in.sin_port = htons(integerValue(args->data[2]));
                memcpy(&in.sin_addr, &addr, sizeof(addr));
                returnedValue = lstNewBinary(&in, sizeof(in));
            }
            break;
        }

        case 5: {   /* Inet newSocketAddressFromHostname: host port: port -> socketaddr_in binary */
            char hostname[256];
            lstGetString(hostname, sizeof(hostname), args->data[1]);
            struct hostent *h = gethostbyname(hostname);
            if (h && h->h_addrtype == AF_INET) {
                struct sockaddr_in in;
                in.sin_family = AF_INET;
                in.sin_port = htons(integerValue(args->data[2]));
                memcpy(&in.sin_addr, h->h_addr_list[0], h->h_length);
                returnedValue = lstNewBinary(&in, sizeof(in));
            }
            break;
        }

    }
    return returnedValue;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
leaq 0x3f3d6(%rip), %rax     # 0x45938
movq (%rax), %rbx
movq 0x10(%rsi), %rax
sarq %rax
cmpq $0x5, %rax
ja 0x673e
movq %rsi, %r14
leaq 0x10f0(%rip), %rcx      # 0x7670
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x56e3
movq %r14, %rdi
callq 0x21c0
testq %rax, %rax
je 0x673e
cmpl $0x2, 0x10(%rax)
jne 0x673e
movq 0x18(%rax), %rax
movq (%rax), %rsi
leaq 0x10(%rsp), %rbx
movl $0x2, %edi
movq %rbx, %rdx
movl $0x10, %ecx
callq 0x2030
movq %rbx, %rdi
callq 0x569b
jmp 0x673b
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movl $0x10, %esi
callq 0x56e3
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
callq 0x2210
cmpl $0x1, %eax
jne 0x673e
leaq 0x10(%rsp), %rdi
movw $0x2, (%rdi)
movl 0x20(%r14), %eax
shrl %eax
rolw $0x8, %ax
movw %ax, 0x2(%rdi)
movl 0xc(%rsp), %eax
movl %eax, 0x4(%rdi)
jmp 0x6731
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movl $0x10, %esi
callq 0x56e3
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x2210
cmpl $0x1, %eax
jne 0x673e
leaq 0x10(%rsp), %rdi
movl $0x4, %esi
jmp 0x6736
movq 0x18(%r14), %rsi
leaq 0x24(%rsp), %rdi
movw $0x2, -0x4(%rdi)
movl 0x20(%r14), %eax
shrl %eax
rolw $0x8, %ax
movw %ax, -0x2(%rdi)
movl (%rsi), %edx
addq $0x10, %rsi
shrl $0x2, %edx
callq 0x21f0
leaq 0x20(%rsp), %rdi
jmp 0x6731
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x56e3
movq %r14, %rdi
callq 0x21c0
testq %rax, %rax
je 0x673e
cmpl $0x2, 0x10(%rax)
jne 0x673e
movq 0x18(%rax), %rcx
movq (%rcx), %rdi
movl 0x14(%rax), %esi
jmp 0x6736
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x56e3
movq %r15, %rdi
callq 0x21c0
testq %rax, %rax
je 0x673e
cmpl $0x2, 0x10(%rax)
jne 0x673e
leaq 0x14(%rsp), %rdi
movw $0x2, -0x4(%rdi)
movl 0x20(%r14), %ecx
shrl %ecx
rolw $0x8, %cx
movw %cx, -0x2(%rdi)
movq 0x18(%rax), %rcx
movq (%rcx), %rsi
movslq 0x14(%rax), %rdx
callq 0x21f0
leaq 0x10(%rsp), %rdi
movl $0x10, %esi
callq 0x573a
movq %rax, %rbx
movq %rbx, %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r14
popq %r15
retq