// File: /workspace/github_c_makefile_star2_O2/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 %rdx, %r12
movq %rdi, -0x38(%rbp)
movslq %esi, %r14
leaq 0x2b42(%rip), %rax      # 0x5160
movq %rax, -0x40(%rbp)
movw $0x1e, %ax
movq %rax, -0x50(%rbp)
movl $0x3e8, -0x30(%rbp)     # imm = 0x3E8
pushq $0x4
popq %rcx
xorl %eax, %eax
pushq $0x1
popq %r15
movl %ecx, -0x2c(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
cmpq %r14, %r15
jge 0x2833
movq (%r12,%r15,8), %rbx
xorl %r13d, %r13d
movl %eax, %eax
leaq 0x299e(%rip), %rcx      # 0x5004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
leaq 0x29a3(%rip), %rsi      # 0x501c
callq 0x22c0
testl %eax, %eax
je 0x287d
movq %rbx, %rdi
leaq 0x2993(%rip), %rsi      # 0x5023
callq 0x22c0
testl %eax, %eax
je 0x2881
movq %rbx, %rdi
leaq 0x2986(%rip), %rsi      # 0x502d
callq 0x22c0
testl %eax, %eax
je 0x27fe
movq %rbx, %rdi
leaq 0x2976(%rip), %rsi      # 0x5034
callq 0x22c0
testl %eax, %eax
je 0x2802
movq %rbx, %rdi
leaq 0x296c(%rip), %rsi      # 0x5041
callq 0x22c0
testl %eax, %eax
je 0x2806
movq %rbx, %rdi
leaq 0x2963(%rip), %rsi      # 0x504f
callq 0x22c0
testl %eax, %eax
je 0x2810
movq %rbx, %rdi
leaq 0x295d(%rip), %rsi      # 0x5060
callq 0x22c0
testl %eax, %eax
je 0x2814
movq %rbx, %rdi
leaq 0x295a(%rip), %rsi      # 0x5074
callq 0x22c0
testl %eax, %eax
je 0x2818
movq %rbx, %rdi
leaq 0x295c(%rip), %rsi      # 0x508d
callq 0x22c0
testl %eax, %eax
je 0x2827
movq %rbx, %rdi
leaq 0x294a(%rip), %rsi      # 0x5092
callq 0x22c0
testl %eax, %eax
je 0x282b
movq %rbx, %rdi
leaq 0x2937(%rip), %rsi      # 0x5096
callq 0x22c0
pushq $0x3
popq %rcx
movl %ecx, -0x2c(%rbp)
testl %eax, %eax
je 0x281c
jmp 0x28f8
movq %rbx, %rdi
callq 0x2420
movl %eax, -0x30(%rbp)
testl %eax, %eax
jne 0x281c
jmp 0x28b0
movq %rbx, %rdi
pushq $0x3a
popq %rsi
callq 0x20f0
xorl %ecx, %ecx
testq %rax, %rax
setne %cl
leal 0x2(,%rcx,8), %edi
movq %rbx, %rsi
leaq -0x70(%rbp), %rdx
callq 0x24c0
testl %eax, %eax
jle 0x28d6
movq %rbx, -0x40(%rbp)
jmp 0x281c
movq %rbx, -0x48(%rbp)
jmp 0x281c
movq %rbx, %rdi
callq 0x2420
decl %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jae 0x289d
movq %rbx, -0x58(%rbp)
jmp 0x281c
movq %rbx, %rdi
callq 0x2420
movq %rax, -0x50(%rbp)
decl %eax
cmpl $0xfffe, %eax           # imm = 0xFFFE
jbe 0x281c
jmp 0x28c3
pushq $0x1
jmp 0x281a
pushq $0x2
jmp 0x281a
movq -0x38(%rbp), %rax
movb $0x1, 0x2c(%rax)
jmp 0x281c
pushq $0x3
jmp 0x281a
pushq $0x4
jmp 0x281a
pushq $0x5
popq %r13
incq %r15
movl %r13d, %eax
jmp 0x264d
pushq $0x1
jmp 0x282d
pushq $0x2
popq %rax
movl %eax, -0x2c(%rbp)
jmp 0x281c
movq -0x58(%rbp), %rdx
testq %rdx, %rdx
je 0x2885
movq -0x38(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq %rdx, (%rax)
movq -0x48(%rbp), %rcx
movq %rcx, 0x18(%rax)
movl -0x30(%rbp), %ecx
movl %ecx, 0x20(%rax)
andl $0x0, 0x24(%rax)
movl -0x2c(%rbp), %ecx
movl %ecx, 0x28(%rax)
movq -0x50(%rbp), %rcx
movw %cx, 0x10(%rax)
pushq $0x1
popq %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
jmp 0x286d
pushq $0x2
jmp 0x286d
movq 0x6754(%rip), %rax      # 0x8fe0
movq (%rax), %rsi
leaq 0x28b5(%rip), %rdi      # 0x514b
callq 0x2310
jmp 0x28f1
movq 0x673c(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x2800(%rip), %rsi      # 0x50ae
jmp 0x28e7
movq 0x6729(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x282f(%rip), %rsi      # 0x50f0
jmp 0x28e7
movq 0x6716(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x2847(%rip), %rsi      # 0x511b
jmp 0x28e7
movq 0x6703(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x27e5(%rip), %rsi      # 0x50cc
movq %rbx, %rdx
xorl %eax, %eax
callq 0x23e0
pushq $0x4
jmp 0x286d
movq 0x66e1(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x2790(%rip), %rsi      # 0x5099
jmp 0x28e7