// File: /workspace/github_c_makefile_star2_O3/ttoobne[P]bohttpd/src/core/config.c
// Function: parse_configuration
config_t* parse_configuration(char* filename) {
    config_t* config;
    char buf[CONFBUF_SIZE] = { '\0' };
    FILE* fp;
    char* p;
    char* name_st;
    char* name_ed;
    char* value_st;
    char* value_ed;
    char ch;
    int have_name;
    int have_value;
    int line;

    /* 维护状态机来解析配置文件 */
    /* 按行读取，每行格式 [<name> = <value>] [#[...]] */
    enum {
        start,
        name,
        spaces_before_equal_sign,
        equal_sign,
        spaces_before_value,
        value,
        end,
    } state;

    do {
        if ((config = (config_t*)malloc(sizeof(config_t))) == NULL) {
            log_error("config_t malloc failed.");
            return NULL;
        }

        /* 设置配置默认值 */
        config->threadpool = THREADPOOL_DEF;
        config->taskqueue = TASKQUEUE_DEF;
        memset(config->root, 0, sizeof(config->root));
        memset(config->defile, 0, sizeof(config->defile));
        strncpy(config->root, ROOT_DEF, 2);
        strncpy(config->defile, DEFILE_DEF, 10);
        config->timeout = TIMEOUT_DEF;
        config->port = PORT_DEF;

        /* 只读打开配置文件 */
        if ((fp = fopen(filename, "r")) == NULL) {
            log_error("config file open failed.");
            break;
        }

        state = start;
        line = 0;
        
        while (fgets(buf, sizeof(buf), fp) != NULL) {
            line ++ ;

            for (p = buf; *p != '\0'; ++ p) {
                ch = *p;

                switch (state) {
                case start:
                    have_name = 0;
                    have_value = 0;

                    if (ch == ' ' || ch == '\t') {
                        break;
                    }

                    if (ch == '#' || ch == '\n') {
                        state = end;
                        p -- ;
                        break;
                    }

                    if (!isalpha(ch) && !isdigit(ch)) {
                        if (!iscntrl(ch)) {
                            log_warn("line %d in configuration file: unrecognized syntax or character '%c'.", line, ch);
                        } else {
                            log_warn("line %d in configuration file: unrecognized syntax or character '0x%x'.", line, ch);
                        }
                        state = end;
                        p -- ;
                        break;
                    }

                    name_st = p;
                    state = name;

                    break;
                
                case name:
                    if (ch == ' ' || ch == '\t') {
                        name_ed = p - 1;
                        have_name = 1;
                        state = spaces_before_equal_sign;
                        break;
                    }

                    if (ch == '=') {
                        name_ed = p - 1;
                        have_name = 1;
                        state = spaces_before_value;
                        break;
                    }

                    if (!isalpha(ch) && !isdigit(ch)) {
                        if (!iscntrl(ch)) {
                            log_warn("line %d in configuration file: unrecognized syntax or character '%c'.", line, ch);
                        } else {
                            log_warn("line %d in configuration file: unrecognized syntax or character '0x%x'.", line, ch);
                        }
                        state = end;
                        p -- ;
                        break;
                    }

                    break;

                case spaces_before_equal_sign:
                    if (ch == ' ' || ch == '\t') {
                        break;
                    }

                    if (ch == '=') {
                        state = spaces_before_value;
                        break;
                    }
                    
                    if (!iscntrl(ch)) {
                        log_warn("line %d in configuration file: unrecognized syntax or character '%c'.", line, ch);
                    } else {
                        log_warn("line %d in configuration file: unrecognized syntax or character '0x%x'.", line, ch);
                    }
                    state = end;
                    p -- ;
                    break;

                case spaces_before_value:
                    if (ch == ' ' || ch == '\t') {
                        break;
                    }

                    value_st = p;
                    state = value;

                    break;

                case value:
                    if (ch == ' ' || ch == '\t' || ch == '#' || ch == '\n') {
                        value_ed = p - 1;
                        have_value = 1;
                        state = end;
                        p -- ;
                        break;
                    }

                    break;

                case end:
                    *(p + 1) = '\0';
                    state = start;

                    if (have_name != have_value) {
                        log_warn("line %d in configuration file: unrecognized syntax .", line);
                        break;
                    }

                    if (have_name) {
                        if (check_name_value(config, name_st, name_ed, value_st, value_ed) != 0) {
                            log_warn("line %d in configuration file: unrecognized identifier or value.", line);
                            break;
                        }
                    }

                    break;

                default:
                    break;
                }
            }
        }

        fclose(fp);
        fp = NULL;

        return config;

    } while(0);
    
    if (fp != NULL) {
        fclose(fp);
        fp = NULL;
    }

    config_destroy(config);

    return NULL;
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x458, %rsp            # imm = 0x458
movq %rdi, %rbx
leaq -0x470(%rbp), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %esi, %esi
callq 0x21b0
movl $0x218, %edi            # imm = 0x218
callq 0x22d0
testq %rax, %rax
je 0x32a2
movq %rax, %r14
movabsq $0x2000000040, %rax     # imm = 0x2000000040
movq %rax, (%r14)
leaq 0x8(%r14), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x21b0
movw $0x2f2e, 0x8(%r14)      # imm = 0x2F2E
movabsq $0x74682e7865646e69, %rax # imm = 0x74682E7865646E69
movq %rax, 0x108(%r14)
movw $0x6c6d, 0x110(%r14)    # imm = 0x6C6D
movq $0x3e8, 0x208(%r14) # imm = 0x3E8
movw $0x50, 0x210(%r14)
leaq 0x4ffd(%rip), %rsi      # 0x7caf
movq %rbx, %rdi
callq 0x2390
testq %rax, %rax
je 0x32cc
movq %rax, %rbx
movq %r15, -0x60(%rbp)
movq %r14, -0x40(%rbp)
leaq -0x470(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x2210
testq %rax, %rax
je 0x3294
movq -0x40(%rbp), %rax
addq $0x108, %rax            # imm = 0x108
movq %rax, -0x58(%rbp)
xorl %r15d, %r15d
movl $0x0, -0x30(%rbp)
movq %rbx, -0x68(%rbp)
incl -0x30(%rbp)
movb -0x470(%rbp), %al
testb %al, %al
je 0x326d
leaq -0x470(%rbp), %r13
movl %r14d, %edx
leaq 0x4597(%rip), %r14      # 0x72c0
cmpl $0x6, %r15d
ja 0x3155
movsbl %al, %ebx
movl %r15d, %ecx
movslq (%r14,%rcx,4), %rcx
addq %r14, %rcx
jmpq *%rcx
movzbl %al, %eax
xorl %r12d, %r12d
cmpl $0x1f, %eax
jg 0x2edf
cmpl $0x9, %eax
je 0x2f16
cmpl $0xa, %eax
je 0x314a
jmp 0x2eed
movl $0x2, %r15d
cmpb $0x9, %al
je 0x3155
movzbl %al, %eax
cmpl $0x20, %eax
je 0x3155
cmpl $0x3d, %eax
jne 0x2f20
movl $0x4, %r15d
jmp 0x3155
movb $0x0, 0x1(%r13)
xorl %r15d, %r15d
cmpl %edx, %r12d
jne 0x2eac
testl %edx, %edx
je 0x2fd1
movl %edx, -0x2c(%rbp)
movq -0x70(%rbp), %rbx
testq %rbx, %rbx
sete %al
cmpq $0x0, -0x50(%rbp)
sete %cl
orb %al, %cl
cmpq $0x0, -0x38(%rbp)
sete %al
cmpq $0x0, -0x48(%rbp)
sete %dl
orb %al, %dl
orb %cl, %dl
je 0x2fd9
xorl %r15d, %r15d
movl $0x3, %edi
leaq 0x4509(%rip), %rsi      # 0x72f8
leaq 0x4514(%rip), %rdx      # 0x730a
movl $0xbc, %ecx
leaq 0x461b(%rip), %r8       # 0x741d
movl -0x30(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
movl $0x1, %r12d
movl -0x2c(%rbp), %edx
jmp 0x3155
cmpb $0x9, %al
je 0x2e34
movzbl %al, %eax
cmpl $0x3d, %eax
je 0x2f62
cmpl $0x20, %eax
jne 0x2f7b
leaq -0x1(%r13), %rax
movq %rax, -0x50(%rbp)
movl $0x2, %r15d
jmp 0x2f70
movl $0x4, %r15d
cmpb $0x9, %al
je 0x3155
movzbl %al, %eax
cmpl $0x20, %eax
je 0x3155
movl $0x5, %r15d
movq %r13, -0x38(%rbp)
jmp 0x3155
movl $0x5, %r15d
cmpb $0x23, %al
ja 0x3155
movzbl %al, %eax
movabsq $0x900000600, %rcx      # imm = 0x900000600
btq %rax, %rcx
jae 0x3155
decq %r13
movl $0x6, %r15d
movl $0x1, %edx
movq %r13, -0x48(%rbp)
jmp 0x3155
movl $0x3, %edi
leaq 0x4440(%rip), %rsi      # 0x72f8
movl %edx, %ebx
leaq 0x4449(%rip), %rdx      # 0x730a
movl $0xb6, %ecx
leaq 0x451b(%rip), %r8       # 0x73e8
movl -0x30(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
movl %ebx, %edx
jmp 0x3155
cmpl $0x20, %eax
je 0x2f16
cmpl $0x23, %eax
je 0x314a
callq 0x2490
movq (%rax), %rax
movslq %ebx, %rcx
movzwl (%rax,%rcx,2), %eax
movl $0x1, %r15d
testl $0xc00, %eax            # imm = 0xC00
je 0x30b6
movq %r13, -0x70(%rbp)
jmp 0x3153
xorl %edx, %edx
xorl %r15d, %r15d
jmp 0x3155
movl %r12d, %r14d
movl %edx, %r12d
callq 0x2490
movq (%rax), %rax
movslq %ebx, %rcx
testb $0x2, (%rax,%rcx,2)
jne 0x306e
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x43ae(%rip), %rsi      # 0x72f8
leaq 0x43b9(%rip), %rdx      # 0x730a
movl $0x94, %ecx
leaq 0x43fd(%rip), %r8       # 0x735a
jmp 0x3090
leaq -0x1(%r13), %rax
movq %rax, -0x50(%rbp)
movl $0x4, %r15d
movl $0x1, %r12d
jmp 0x3155
movl %r12d, -0x2c(%rbp)
movl %edx, %r12d
callq 0x2490
movq (%rax), %rax
movslq %ebx, %rcx
movzwl (%rax,%rcx,2), %eax
movl $0x1, %r15d
testl $0xc00, %eax            # imm = 0xC00
jne 0x3114
testb $0x2, %al
jne 0x30de
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x433f(%rip), %rsi      # 0x72f8
leaq 0x434a(%rip), %rdx      # 0x730a
movl $0x7e, %ecx
leaq 0x438e(%rip), %r8       # 0x735a
jmp 0x3100
xorl %r12d, %r12d
jmp 0x3153
xorl %eax, %eax
movq -0x50(%rbp), %rcx
movb %al, 0x1(%rcx)
movq -0x48(%rbp), %rdx
movb %al, 0x1(%rdx)
movq %rcx, %rax
subq %rbx, %rax
addq $-0x3, %rax
cmpq $0x6, %rax
ja 0x2de0
leaq 0x42d8(%rip), %rcx      # 0x72dc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x4, %edx
leaq 0x4445(%rip), %rdi      # 0x745e
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
je 0x3256
movl $0x4, %edx
leaq 0x442e(%rip), %rdi      # 0x7463
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2de0
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3323
cmpq $0xffff, %rax           # imm = 0xFFFF
ja 0x2de0
movq -0x40(%rbp), %rcx
movw %ax, 0x210(%rcx)
jmp 0x2e0d
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x427b(%rip), %rsi      # 0x72f8
leaq 0x4286(%rip), %rdx      # 0x730a
movl $0x96, %ecx
leaq 0x4310(%rip), %r8       # 0x73a0
movl -0x30(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
decq %r13
movl $0x6, %r15d
movl %r12d, %edx
movl %r14d, %r12d
leaq 0x420f(%rip), %r14      # 0x72c0
jmp 0x3155
testb $0x2, %al
jne 0x311d
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x422f(%rip), %rsi      # 0x72f8
leaq 0x423a(%rip), %rdx      # 0x730a
movl $0x5f, %ecx
leaq 0x427e(%rip), %r8       # 0x735a
jmp 0x313f
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x420b(%rip), %rsi      # 0x72f8
leaq 0x4216(%rip), %rdx      # 0x730a
movl $0x80, %ecx
leaq 0x42a0(%rip), %r8       # 0x73a0
movl -0x30(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
decq %r13
movl $0x6, %r15d
movl %r12d, %edx
movl -0x2c(%rbp), %r12d
jmp 0x3155
movl %ebx, (%rsp)
movl $0x3, %edi
leaq 0x41cc(%rip), %rsi      # 0x72f8
leaq 0x41d7(%rip), %rdx      # 0x730a
movl $0x61, %ecx
leaq 0x4261(%rip), %r8       # 0x73a0
movl -0x30(%rbp), %r9d
xorl %eax, %eax
callq 0x252a
decq %r13
movl $0x6, %r15d
xorl %edx, %edx
movb 0x1(%r13), %al
incq %r13
testb %al, %al
jne 0x2d29
jmp 0x3270
movl $0x7, %edx
leaq 0x42fa(%rip), %rdi      # 0x746f
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2de0
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3323
testq %rax, %rax
js 0x2de0
movq -0x40(%rbp), %rcx
movq %rax, 0x208(%rcx)
jmp 0x2e0d
movl $0x6, %edx
leaq 0x42b1(%rip), %rdi      # 0x7468
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2de0
movl $0x100, %edx            # imm = 0x100
movq -0x58(%rbp), %rdi
jmp 0x325f
movl $0x9, %edx
leaq 0x4296(%rip), %rdi      # 0x7477
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2de0
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3323
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
ja 0x2de0
movq -0x40(%rbp), %rcx
movl %eax, 0x4(%rcx)
jmp 0x2e0d
movl $0xa, %edx
leaq 0x425f(%rip), %rdi      # 0x7481
movq %rbx, %rsi
callq 0x2070
testl %eax, %eax
jne 0x2de0
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x3323
cmpq $0x7fffffff, %rax       # imm = 0x7FFFFFFF
ja 0x2de0
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x2e0d
movl $0x100, %edx            # imm = 0x100
movq -0x60(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x2060
jmp 0x2e0d
movl %r14d, %edx
movl %edx, %r14d
leaq -0x470(%rbp), %rdi
movl $0x400, %esi            # imm = 0x400
movq -0x68(%rbp), %rbx
movq %rbx, %rdx
callq 0x2210
testq %rax, %rax
jne 0x2d07
movq %rbx, %rdi
callq 0x2120
movq -0x40(%rbp), %rbx
jmp 0x32fc
leaq 0x404f(%rip), %rsi      # 0x72f8
leaq 0x405a(%rip), %rdx      # 0x730a
leaq 0x4067(%rip), %r8       # 0x731e
xorl %ebx, %ebx
movl $0x4, %edi
movl $0x31, %ecx
xorl %eax, %eax
callq 0x252a
jmp 0x32fc
leaq 0x4025(%rip), %rsi      # 0x72f8
leaq 0x4030(%rip), %rdx      # 0x730a
leaq 0x4060(%rip), %r8       # 0x7341
movl $0x4, %edi
movl $0x41, %ecx
xorl %eax, %eax
callq 0x252a
movq %r14, %rdi
callq 0x2030
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x458, %rsp            # imm = 0x458
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq