// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movq %rdx, -0x58(%rbp)
movups 0x2418(%rip), %xmm0     # 0x5250
movaps %xmm0, -0x50(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, 0x20(%rdi)
cmpl $0x2, %esi
jl 0x305e
movq %rdi, -0x38(%rbp)
movl %esi, %r12d
xorl %r14d, %r14d
movl $0x4, -0x2c(%rbp)
movl $0x1, %r15d
leaq 0x23c7(%rip), %rbx      # 0x5238
movq -0x58(%rbp), %rax
movq (%rax,%r15,8), %r13
movl %r14d, %eax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
movq %r13, %rdi
leaq 0x23d1(%rip), %rsi      # 0x5260
callq 0x21a0
testl %eax, %eax
je 0x3050
movq %r13, %rdi
leaq 0x23c1(%rip), %rsi      # 0x5267
callq 0x21a0
testl %eax, %eax
je 0x3057
movq %r13, %rdi
leaq 0x23b4(%rip), %rsi      # 0x5271
callq 0x21a0
testl %eax, %eax
je 0x2fd6
movq %r13, %rdi
leaq 0x23a4(%rip), %rsi      # 0x5278
callq 0x21a0
testl %eax, %eax
je 0x2fde
movq %r13, %rdi
leaq 0x239a(%rip), %rsi      # 0x5285
callq 0x21a0
testl %eax, %eax
je 0x2fe6
movq %r13, %rdi
leaq 0x2394(%rip), %rsi      # 0x5296
callq 0x21a0
testl %eax, %eax
je 0x2fee
movq %r13, %rdi
leaq 0x2391(%rip), %rsi      # 0x52aa
callq 0x21a0
testl %eax, %eax
je 0x2ff6
movq %r13, %rdi
leaq 0x2382(%rip), %rsi      # 0x52b2
callq 0x21a0
xorl %r14d, %r14d
testl %eax, %eax
je 0x3000
movq %r13, %rdi
leaq 0x236d(%rip), %rsi      # 0x52b7
callq 0x21a0
testl %eax, %eax
je 0x3009
movq %r13, %rdi
leaq 0x235a(%rip), %rsi      # 0x52bb
callq 0x21a0
movl $0x3, -0x2c(%rbp)
testl %eax, %eax
je 0x2fc8
jmp 0x309a
xorl %r14d, %r14d
movq %r13, %rdi
leaq 0x2c31(%rip), %rsi      # 0x5bb4
leaq -0x48(%rbp), %rdx
xorl %eax, %eax
callq 0x2180
cmpl $0x0, -0x48(%rbp)
jne 0x2fc8
jmp 0x306a
movq -0x38(%rbp), %rax
movq %r13, 0x8(%rax)
jmp 0x2fc5
movq %r13, -0x50(%rbp)
jmp 0x2fc5
movq %r13, %rdi
callq 0x2270
decl %eax
cmpl $0xfffe, %eax           # imm = 0xFFFE
ja 0x307d
movq -0x38(%rbp), %rax
movq %r13, (%rax)
xorl %r14d, %r14d
incq %r15
cmpq %r15, %r12
jne 0x2e71
jmp 0x3012
movl $0x1, %r14d
jmp 0x2fc8
movl $0x2, %r14d
jmp 0x2fc8
movl $0x3, %r14d
jmp 0x2fc8
movl $0x4, %r14d
jmp 0x2fc8
movq -0x38(%rbp), %rax
movb $0x1, 0x10(%rax)
jmp 0x2fc5
movl $0x1, -0x2c(%rbp)
jmp 0x2fc8
movl $0x2, -0x2c(%rbp)
jmp 0x2fc8
movq -0x38(%rbp), %rcx
cmpq $0x0, (%rcx)
je 0x305e
cmpq $0x0, 0x8(%rcx)
jne 0x302e
leaq 0x2307(%rip), %rax      # 0x5331
movq %rax, 0x8(%rcx)
movaps -0x50(%rbp), %xmm0
movups %xmm0, 0x18(%rcx)
movl -0x2c(%rbp), %eax
movl %eax, 0x28(%rcx)
movl $0x1, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x3, %eax
jmp 0x3041
movl $0x2, %eax
jmp 0x3041
callq 0x2320
movl $0x4, %eax
jmp 0x3041
movq 0x4f6f(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2276(%rip), %rsi      # 0x52f1
jmp 0x308e
movq 0x4f5c(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2245(%rip), %rsi      # 0x52d3
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
jmp 0x3063
movq 0x4f3f(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2213(%rip), %rsi      # 0x52be
jmp 0x308e