// File: /workspace/github_c_makefile_star2_O2/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 0x2620(%rip), %xmm0     # 0x5250
leaq -0x48(%rbp), %rax
movaps %xmm0, -0x8(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rdi)
movups %xmm0, 0x10(%rdi)
movq %rdi, -0x38(%rbp)
movups %xmm0, 0x20(%rdi)
movslq %esi, %r12
pushq $0x4
popq %rax
xorl %r15d, %r15d
pushq $0x1
popq %r14
leaq 0x25da(%rip), %rbx      # 0x5238
movl %eax, -0x2c(%rbp)
cmpq %r12, %r14
jge 0x2df0
movq -0x58(%rbp), %rax
movq (%rax,%r14,8), %r13
movl %r15d, %eax
movslq (%rbx,%rax,4), %rax
addq %rbx, %rax
jmpq *%rax
movq %r13, %rdi
leaq 0x25d8(%rip), %rsi      # 0x5260
callq 0x21a0
testl %eax, %eax
je 0x2e2c
movq %r13, %rdi
leaq 0x25c8(%rip), %rsi      # 0x5267
callq 0x21a0
testl %eax, %eax
je 0x2e30
movq %r13, %rdi
leaq 0x25bb(%rip), %rsi      # 0x5271
callq 0x21a0
testl %eax, %eax
je 0x2dc2
movq %r13, %rdi
leaq 0x25ab(%rip), %rsi      # 0x5278
callq 0x21a0
testl %eax, %eax
je 0x2dc6
movq %r13, %rdi
leaq 0x25a1(%rip), %rsi      # 0x5285
callq 0x21a0
testl %eax, %eax
je 0x2dca
movq %r13, %rdi
leaq 0x259b(%rip), %rsi      # 0x5296
callq 0x21a0
testl %eax, %eax
je 0x2dce
movq %r13, %rdi
leaq 0x2598(%rip), %rsi      # 0x52aa
callq 0x21a0
testl %eax, %eax
je 0x2dda
movq %r13, %rdi
leaq 0x2589(%rip), %rsi      # 0x52b2
callq 0x21a0
xorl %r15d, %r15d
testl %eax, %eax
je 0x2de4
movq %r13, %rdi
leaq 0x2574(%rip), %rsi      # 0x52b7
callq 0x21a0
testl %eax, %eax
je 0x2de8
movq %r13, %rdi
leaq 0x2561(%rip), %rsi      # 0x52bb
callq 0x21a0
pushq $0x3
popq %rcx
movl %ecx, -0x2c(%rbp)
testl %eax, %eax
je 0x2dd2
jmp 0x2e7e
xorl %r15d, %r15d
movq %r13, %rdi
leaq 0x2e39(%rip), %rsi      # 0x5bb4
leaq -0x48(%rbp), %rdx
xorl %eax, %eax
callq 0x2180
cmpl $0x0, -0x48(%rbp)
jne 0x2dd2
jmp 0x2e4c
movq -0x38(%rbp), %rax
movq %r13, 0x8(%rax)
jmp 0x2dbd
movq %r13, -0x50(%rbp)
jmp 0x2dbd
movq %r13, %rdi
callq 0x2270
decl %eax
cmpl $0xfffe, %eax           # imm = 0xFFFE
ja 0x2e5f
movq -0x38(%rbp), %rax
movq %r13, (%rax)
xorl %r15d, %r15d
jmp 0x2dd2
pushq $0x1
jmp 0x2dd0
pushq $0x2
jmp 0x2dd0
pushq $0x3
jmp 0x2dd0
pushq $0x4
popq %r15
incq %r14
jmp 0x2c61
movq -0x38(%rbp), %rax
movb $0x1, 0x10(%rax)
jmp 0x2dbd
pushq $0x1
jmp 0x2dea
pushq $0x2
popq %rax
movl %eax, -0x2c(%rbp)
jmp 0x2dd2
movq -0x38(%rbp), %rcx
cmpq $0x0, (%rcx)
je 0x2e34
cmpq $0x0, 0x8(%rcx)
jne 0x2e0c
leaq 0x2529(%rip), %rax      # 0x5331
movq %rax, 0x8(%rcx)
movaps -0x50(%rbp), %xmm0
movups %xmm0, 0x18(%rcx)
movl -0x2c(%rbp), %eax
movl %eax, 0x28(%rcx)
pushq $0x1
popq %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
jmp 0x2e1c
pushq $0x2
jmp 0x2e1c
movq 0x51a5(%rip), %rax      # 0x7fe0
movq (%rax), %rsi
leaq 0x24d7(%rip), %rdi      # 0x531c
callq 0x21c0
jmp 0x2e7a
movq 0x518d(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2494(%rip), %rsi      # 0x52f1
jmp 0x2e70
movq 0x517a(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2463(%rip), %rsi      # 0x52d3
movq %r13, %rdx
xorl %eax, %eax
callq 0x2230
pushq $0x4
jmp 0x2e1c
movq 0x515b(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x242f(%rip), %rsi      # 0x52be
jmp 0x2e70