// File: /workspace/github_c_makefile_star2_O1/TimeToogo[P]ff-proxy/client/c/config.c
// Function: ff_client_parse_arguments
enum ff_client_action ff_client_parse_arguments(struct ff_client_config *config, int argc, char **argv)
{
    enum ff_client_action action = FF_CLIENT_ACTION_MAKE_REQUEST;
    int state = FF_CLIENT_PARSE_ARG_STATE_DEFAULT;
    enum ff_log_type logging_level = FF_ERROR;
    struct ff_encryption_config encryption_config = {.key = NULL, .pbkdf2_iterations = 1000};

    /*
     * Not strictly necessary as config is declared global static,
     * but the tests repeatedly call this function, so ensure it's
     * cleared each time for them.
     */
    memset(config, 0, sizeof(struct ff_config));

    for (int i = 1; i < argc; i++)
    {
        char *arg = argv[i];

        switch (state)
        {
        case FF_CLIENT_PARSE_ARG_STATE_DEFAULT:
            if (strcasecmp(arg, "--help") == 0)
            {
                action = FF_CLIENT_ACTION_PRINT_USAGE;
                goto done;
            }

            else if (strcasecmp(arg, "--version") == 0)
            {
                action = FF_CLIENT_ACTION_PRINT_VERSION;
                goto done;
            }
            else if (strcasecmp(arg, "--port") == 0)
            {
                state = FF_CLIENT_PARSE_ARG_PARSE_PORT;
            }
            else if (strcasecmp(arg, "--ip-address") == 0)
            {
                state = FF_CLIENT_PARSE_ARG_PARSE_IP;
            }
            else if (strcasecmp(arg, "--pre-shared-key") == 0)
            {
                state = FF_CLIENT_PARSE_ARG_PARSE_PSK;
            }
            else if (strcasecmp(arg, "--pbkdf2-iterations") == 0)
            {
                state = FF_CLIENT_PARSE_ARG_PARSE_PBKDF2_ITERATIONS;
            }
            else if (strcasecmp(arg, "--https") == 0)
            {
                config->https = true;
            }
            else if (strcasecmp(arg, "-vvv") == 0)
            {
                logging_level = FF_DEBUG;
            }
            else if (strcasecmp(arg, "-vv") == 0)
            {
                logging_level = FF_INFO;
            }
            else if (strcasecmp(arg, "-v") == 0)
            {
                logging_level = FF_WARNING;
            }
            else
            {
                fprintf(stderr, "Unkown argument %s\n\n", arg);
                action = FF_CLIENT_ACTION_INVALID_ARGS;
                goto done;
            }
            break;

        case FF_CLIENT_PARSE_ARG_PARSE_PORT:
        {
            int port = atoi(arg);

            if (port <= 0 || port > UINT16_MAX)
            {
                fprintf(stderr, "Invalid --port argument: %s\n\n", arg);
                action = FF_CLIENT_ACTION_INVALID_ARGS;
                goto done;
            }

            config->port = arg;
            state = FF_CLIENT_PARSE_ARG_STATE_DEFAULT;
            break;
       }

        case FF_CLIENT_PARSE_ARG_PARSE_IP:
            config->ip_address = arg;
            state = FF_CLIENT_PARSE_ARG_STATE_DEFAULT;
            break;

        case FF_CLIENT_PARSE_ARG_PARSE_PSK:
            encryption_config.key = (uint8_t *)arg;
            state = FF_CLIENT_PARSE_ARG_STATE_DEFAULT;
            break;

        case FF_CLIENT_PARSE_ARG_PARSE_PBKDF2_ITERATIONS:
            sscanf(arg, "%u", &encryption_config.pbkdf2_iterations);

            if (encryption_config.pbkdf2_iterations == 0)
            {
                fprintf(stderr, "Invalid --pbkdf2-iterations argument: %s\n\n", arg);
                action = FF_CLIENT_ACTION_INVALID_ARGS;
                goto done;
            }

            state = FF_CLIENT_PARSE_ARG_STATE_DEFAULT;
            break;

        default:
            fputs("Unkown parse arg state\n\n", stderr);
            action = FF_CLIENT_ACTION_INVALID_ARGS;
            goto done;
        }
    }

    if (action == FF_CLIENT_ACTION_MAKE_REQUEST)
    {
        if (!config->port)
        {
            fputs("--port is required\n\n", stderr);
            action = FF_CLIENT_ACTION_INVALID_ARGS;
            goto done;
        }

        if (!config->ip_address)
        {
            config->ip_address = default_ip_address;
        }

        config->encryption = encryption_config;
        config->logging_level = logging_level;
    }

done:
    return action;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movups 0x23f9(%rip), %xmm0     # 0x5250
movaps %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, 0x20(%rdi)
cmpl $0x2, %esi
setl %cl
jge 0x2e80
movl $0x1, %eax
movl $0x4, %edx
jmp 0x30ac
movl %esi, %eax
movl $0x1, %esi
movq %rax, -0x68(%rbp)
subq %rax, %rsi
movq %rsi, -0x60(%rbp)
xorl %eax, %eax
movl $0x4, -0x34(%rbp)
movl $0x1, -0x30(%rbp)
movl $0x2, %r15d
movq -0x8(%rdx,%r15,8), %r13
movl %eax, %eax
leaq 0x2382(%rip), %rsi      # 0x5238
movslq (%rsi,%rax,4), %rax
addq %rsi, %rax
movq %rdx, -0x58(%rbp)
jmpq *%rax
cmpq -0x68(%rbp), %r15
setae %cl
movq -0x60(%rbp), %rax
addq %r15, %rax
incq %rax
cmpq $0x2, %rax
je 0x30a6
incq %r15
movq -0x58(%rbp), %rax
movq -0x8(%rax,%r15,8), %r13
movl %r12d, %eax
leaq 0x2344(%rip), %rdx      # 0x5238
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
movb %cl, -0x29(%rbp)
leaq 0x2359(%rip), %rsi      # 0x5260
movq %r13, %rdi
callq 0x21a0
xorl %r12d, %r12d
testl %eax, %eax
je 0x2ffb
leaq 0x2346(%rip), %rsi      # 0x5267
movq %r13, %rdi
callq 0x21a0
xorl %r12d, %r12d
testl %eax, %eax
je 0x300a
leaq 0x2336(%rip), %rsi      # 0x5271
movq %r13, %rdi
callq 0x21a0
movb $0x1, %r14b
testl %eax, %eax
je 0x3121
leaq 0x2323(%rip), %rsi      # 0x5278
movq %r13, %rdi
callq 0x21a0
testl %eax, %eax
je 0x30f7
leaq 0x2319(%rip), %rsi      # 0x5285
movq %r13, %rdi
callq 0x21a0
testl %eax, %eax
je 0x312c
leaq 0x2313(%rip), %rsi      # 0x5296
movq %r13, %rdi
callq 0x21a0
testl %eax, %eax
je 0x3137
leaq 0x2310(%rip), %rsi      # 0x52aa
movq %r13, %rdi
callq 0x21a0
testl %eax, %eax
je 0x3142
leaq 0x2301(%rip), %rsi      # 0x52b2
movq %r13, %rdi
callq 0x21a0
testl %eax, %eax
je 0x314b
leaq 0x22ef(%rip), %rsi      # 0x52b7
movq %r13, %rdi
callq 0x21a0
testl %eax, %eax
je 0x3157
leaq 0x22dc(%rip), %rsi      # 0x52bb
movq %r13, %rdi
callq 0x21a0
testl %eax, %eax
jne 0x3172
movl $0x3, -0x34(%rbp)
jmp 0x309a
xorl %r14d, %r14d
movl $0x3, -0x30(%rbp)
jmp 0x309a
xorl %r14d, %r14d
movl $0x2, -0x30(%rbp)
jmp 0x309a
movb %cl, -0x29(%rbp)
movq %r13, -0x50(%rbp)
jmp 0x3094
movb %cl, -0x29(%rbp)
leaq 0x2b88(%rip), %rsi      # 0x5bb4
movq %r13, %rdi
leaq -0x48(%rbp), %rdx
xorl %eax, %eax
callq 0x2180
movb $0x1, %r14b
cmpl $0x0, -0x48(%rbp)
jne 0x3097
movq 0x4f96(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x229d(%rip), %rsi      # 0x52f1
xorl %r14d, %r14d
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
movl $0x4, %r12d
movl $0x4, -0x30(%rbp)
jmp 0x309a
movb %cl, -0x29(%rbp)
movq %r13, %rdi
callq 0x2270
decl %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
setb %r14b
jae 0x30ff
movq %r13, (%rbx)
jmp 0x3097
movb %cl, -0x29(%rbp)
movq %r13, 0x8(%rbx)
movb $0x1, %r14b
xorl %r12d, %r12d
testb %r14b, %r14b
jne 0x2ec3
movb -0x29(%rbp), %cl
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %eax
cmpl $0x1, %eax
sete %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x30e8
cmpq $0x0, (%rbx)
je 0x3163
cmpq $0x0, 0x8(%rbx)
jne 0x30d8
leaq 0x225d(%rip), %rax      # 0x5331
movq %rax, 0x8(%rbx)
movaps -0x50(%rbp), %xmm0
movups %xmm0, 0x18(%rbx)
movl %edx, 0x28(%rbx)
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x2, %r12d
jmp 0x309a
movq 0x4eda(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x21c3(%rip), %rsi      # 0x52d3
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
movl $0x4, -0x30(%rbp)
movl $0x1, %r12d
jmp 0x309a
movl $0x3, %r12d
jmp 0x309a
movl $0x4, %r12d
jmp 0x309a
movb $0x1, 0x10(%rbx)
jmp 0x309a
movl $0x1, -0x34(%rbp)
jmp 0x309a
movl $0x2, -0x34(%rbp)
jmp 0x309a
callq 0x2320
movl $0x4, %eax
jmp 0x30e8
movq 0x4e67(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x213b(%rip), %rsi      # 0x52be
xorl %r12d, %r12d
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
movl $0x4, -0x30(%rbp)
xorl %r14d, %r14d
jmp 0x309a