// File: /workspace/github_c_makefile_star2_O2/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 0x3fa08(%rip), %rax     # 0x45938
movq (%rax), %rbx
movq 0x10(%rsi), %rax
sarq %rax
cmpq $0x5, %rax
ja 0x60ff
movq %rsi, %r14
leaq 0x1716(%rip), %rcx      # 0x7664
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 0x51af
movq %r14, %rdi
callq 0x21c0
testq %rax, %rax
je 0x60ff
cmpl $0x2, 0x10(%rax)
jne 0x60ff
movq 0x18(%rax), %rax
movq (%rax), %rsi
pushq $0x2
popq %rdi
leaq 0x10(%rsp), %rbx
pushq $0x10
popq %rcx
movq %rbx, %rdx
callq 0x2030
movq %rbx, %rdi
callq 0x516a
jmp 0x60fc
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r15
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x51af
leaq 0xc(%rsp), %rsi
movq %r15, %rdi
callq 0x2210
cmpl $0x1, %eax
jne 0x60ff
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 0x60f4
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r14
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x51af
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x2210
cmpl $0x1, %eax
jne 0x60ff
leaq 0x10(%rsp), %rdi
pushq $0x4
jmp 0x60f6
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 0x60f4
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x51af
movq %r14, %rdi
callq 0x21c0
testq %rax, %rax
je 0x60ff
cmpl $0x2, 0x10(%rax)
jne 0x60ff
movq 0x18(%rax), %rcx
movq (%rcx), %rdi
movl 0x14(%rax), %esi
jmp 0x60f7
movq 0x18(%r14), %rdx
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0x51af
movq %r15, %rdi
callq 0x21c0
testq %rax, %rax
je 0x60ff
cmpl $0x2, 0x10(%rax)
jne 0x60ff
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
pushq $0x10
popq %rsi
callq 0x51f4
movq %rax, %rbx
movq %rbx, %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r14
popq %r15
retq