// File: /workspace/github_c_makefile_star2_O3/TimeToogo[P]ff-proxy/src/config.c
// Function: ff_parse_arguments
enum ff_action ff_parse_arguments(struct ff_config *config, int argc, char **argv)
{
    // Default values
    char *port = NULL;
    char *listen_address = default_listen_address;
    enum ff_action action = FF_ACTION_START_PROXY;
    int state = FF_PARSE_ARG_STATE_DEFAULT;
    enum ff_log_type logging_level = FF_ERROR;
    struct ff_encryption_config encryption_config = {
        .key = NULL,
        .pbkdf2_iterations = 1000};
    uint16_t timestamp_fudge_factor = 30;

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

        switch (state)
        {
        case FF_PARSE_ARG_STATE_DEFAULT:
            if (strcasecmp(arg, "--help") == 0)
            {
                action = FF_ACTION_PRINT_USAGE;
                goto done;
            }
            else if (strcasecmp(arg, "--version") == 0)
            {
                action = FF_ACTION_PRINT_VERSION;
                goto done;
            }
            else if (strcasecmp(arg, "--port") == 0)
            {
                state = FF_PARSE_ARG_PARSE_PORT;
            }
            else if (strcasecmp(arg, "--ip-address") == 0)
            {
                state = FF_PARSE_ARG_PARSE_IP;
            }
            else if (strcasecmp(arg, "--ipv6-v6only") == 0)
            {
                config->ipv6_v6only = true;
            }
            else if (strcasecmp(arg, "--pre-shared-key") == 0)
            {
                state = FF_PARSE_ARG_PARSE_PSK;
            }
            else if (strcasecmp(arg, "--pbkdf2-iterations") == 0)
            {
                state = FF_PARSE_ARG_PARSE_PBKDF2_ITERATIONS;
            }
            else if (strcasecmp(arg, "--timestamp-fudge-factor") == 0)
            {
                state = FF_PARSE_ARG_PARSE_TIMESTAMP_FUDGE_FACTOR;
            }
            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_ACTION_INVALID_ARGS;
                goto done;
            }
            break;

        case FF_PARSE_ARG_PARSE_PORT:
        {
            int parsed_port = atoi(arg);

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

            port = arg;
            state = FF_PARSE_ARG_STATE_DEFAULT;
            break;
        }

        case FF_PARSE_ARG_PARSE_IP:
        {
            int ret;
            unsigned char buf[sizeof(struct in6_addr)];

            if (strchr(arg, ':'))
            {
                ret = inet_pton(AF_INET6, arg, buf);
            }
            else
            {
                ret = inet_pton(AF_INET, arg, buf);
            }

            if (ret < 1)
            {
                fprintf(stderr, "Invalid --ip-address argument: %s\n\n", arg);
                action = FF_ACTION_INVALID_ARGS;
                goto done;
            }

            listen_address = arg;
            state = FF_PARSE_ARG_STATE_DEFAULT;
            break;
        }

        case FF_PARSE_ARG_PARSE_PSK:
            encryption_config.key = (uint8_t *)arg;
            state = FF_PARSE_ARG_STATE_DEFAULT;
            break;

        case FF_PARSE_ARG_PARSE_PBKDF2_ITERATIONS:
            encryption_config.pbkdf2_iterations = atoi(arg);

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

            state = FF_PARSE_ARG_STATE_DEFAULT;
            break;

        case FF_PARSE_ARG_PARSE_TIMESTAMP_FUDGE_FACTOR:
        {
            int parsed = atoi(arg);

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

            timestamp_fudge_factor = (uint16_t)parsed;

            state = FF_PARSE_ARG_STATE_DEFAULT;
            break;
        }

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

    if (action == FF_ACTION_START_PROXY)
    {
        if (!port)
        {
            fputs("--port is required\n\n", stderr);
            action = FF_ACTION_INVALID_ARGS;
            goto done;
        }

        config->ip_address = listen_address;
        config->port = port;
        config->encryption = encryption_config;
        config->logging_level = logging_level;
        config->timestamp_fudge_factor = timestamp_fudge_factor;
    }

done:
    return action;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, -0x38(%rbp)
cmpl $0x2, %esi
jl 0x28dd
movq %rdx, %r14
movl %esi, %ebx
movq $0x0, -0x58(%rbp)
movl $0x4, -0x2c(%rbp)
movl $0x3e8, -0x30(%rbp)     # imm = 0x3E8
movw $0x1e, %ax
movq %rax, -0x48(%rbp)
leaq 0x3afc(%rip), %rax      # 0x6160
movq %rax, -0x40(%rbp)
movl $0x1, %r15d
leaq 0x398f(%rip), %r12      # 0x6004
movq $0x0, -0x50(%rbp)
xorl %ecx, %ecx
movq (%r14,%r15,8), %r13
movl %ecx, %eax
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %r13, %rdi
leaq 0x3984(%rip), %rsi      # 0x601c
callq 0x22c0
testl %eax, %eax
je 0x28cf
movq %r13, %rdi
leaq 0x3974(%rip), %rsi      # 0x6023
callq 0x22c0
testl %eax, %eax
je 0x28d6
movq %r13, %rdi
leaq 0x3967(%rip), %rsi      # 0x602d
callq 0x22c0
testl %eax, %eax
je 0x2835
movq %r13, %rdi
leaq 0x3957(%rip), %rsi      # 0x6034
callq 0x22c0
testl %eax, %eax
je 0x283c
movq %r13, %rdi
leaq 0x394d(%rip), %rsi      # 0x6041
callq 0x22c0
testl %eax, %eax
je 0x2843
movq %r13, %rdi
leaq 0x3944(%rip), %rsi      # 0x604f
callq 0x22c0
testl %eax, %eax
je 0x284d
movq %r13, %rdi
leaq 0x393e(%rip), %rsi      # 0x6060
callq 0x22c0
testl %eax, %eax
je 0x2854
movq %r13, %rdi
leaq 0x393b(%rip), %rsi      # 0x6074
callq 0x22c0
testl %eax, %eax
je 0x285b
movq %r13, %rdi
leaq 0x393d(%rip), %rsi      # 0x608d
callq 0x22c0
testl %eax, %eax
je 0x286e
movq %r13, %rdi
leaq 0x392b(%rip), %rsi      # 0x6092
callq 0x22c0
testl %eax, %eax
je 0x2877
movq %r13, %rdi
leaq 0x3918(%rip), %rsi      # 0x6096
callq 0x22c0
movl $0x3, -0x2c(%rbp)
movl $0x0, %ecx
testl %eax, %eax
je 0x2860
jmp 0x293f
movq %r13, %rdi
callq 0x2420
movl $0x0, %ecx
movl %eax, -0x30(%rbp)
testl %eax, %eax
jne 0x2860
jmp 0x28f7
movq %r13, %rdi
movl $0x3a, %esi
callq 0x20f0
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
leal 0x2(,%rcx,8), %edi
movq %r13, %rsi
leaq -0x70(%rbp), %rdx
callq 0x24c0
testl %eax, %eax
jle 0x28e4
xorl %ecx, %ecx
movq %r13, -0x40(%rbp)
jmp 0x2860
xorl %ecx, %ecx
movq %r13, -0x50(%rbp)
jmp 0x2860
movq %r13, %rdi
callq 0x2420
decl %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jae 0x291d
xorl %ecx, %ecx
movq %r13, -0x58(%rbp)
jmp 0x2860
movq %r13, %rdi
callq 0x2420
leal -0x1(%rax), %ecx
cmpl $0xfffe, %ecx           # imm = 0xFFFE
ja 0x290a
movq %rax, -0x48(%rbp)
xorl %ecx, %ecx
jmp 0x2860
movl $0x1, %ecx
jmp 0x2860
movl $0x2, %ecx
jmp 0x2860
movq -0x38(%rbp), %rax
movb $0x1, 0x2c(%rax)
jmp 0x2831
movl $0x3, %ecx
jmp 0x2860
movl $0x4, %ecx
jmp 0x2860
movl $0x5, %ecx
incq %r15
cmpq %r15, %rbx
jne 0x267f
jmp 0x2880
movl $0x1, -0x2c(%rbp)
jmp 0x2831
movl $0x2, -0x2c(%rbp)
jmp 0x2831
movq -0x58(%rbp), %rcx
testq %rcx, %rcx
je 0x28dd
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rdx
movq %rdx, 0x8(%rax)
movq %rcx, (%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x18(%rax)
movl -0x30(%rbp), %ecx
movl %ecx, 0x20(%rax)
movl $0x0, 0x24(%rax)
movl -0x2c(%rbp), %ecx
movl %ecx, 0x28(%rax)
movq -0x48(%rbp), %rcx
movw %cx, 0x10(%rax)
movl $0x1, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x3, %eax
jmp 0x28c0
movl $0x2, %eax
jmp 0x28c0
callq 0x2510
jmp 0x2938
movq 0x76f5(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x37d7(%rip), %rsi      # 0x60cc
jmp 0x292e
movq 0x76e2(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x37e8(%rip), %rsi      # 0x60f0
jmp 0x292e
movq 0x76cf(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x3800(%rip), %rsi      # 0x611b
jmp 0x292e
movq 0x76bc(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x3780(%rip), %rsi      # 0x60ae
movq %r13, %rdx
xorl %eax, %eax
callq 0x23e0
movl $0x4, %eax
jmp 0x28c0
movq 0x769a(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
leaq 0x3749(%rip), %rsi      # 0x6099
jmp 0x292e