// File: /workspace/github_c_makefile_star2_O0/niuyuling[P]CProxy/httpdns.c
// Function: read_cache_file
int8_t read_cache_file()
{
    char *buff, *cache_ptr;
    struct dns_cache *cache_temp;
    long file_size;
    int len, fl = 0;

    cache_temp = NULL;
    cache_using = 0;
    if ((cfp = fopen(httpdns.cachePath, "r+")) == NULL) {
        //创建文件并设置权限
        if ((cfp = fopen(httpdns.cachePath, "w")) != NULL) {
            chmod(httpdns.cachePath, S_IWOTH | S_IROTH | S_IWGRP | S_IRGRP | S_IWUSR | S_IRUSR);
            fclose(cfp);
            return 0;
        }
        return 1;
    }

    //读取文件内容
    fseek(cfp, 0, SEEK_END);
    file_size = ftell(cfp);
    if ((buff = (char *)alloca(file_size)) == NULL)
        return 1;
    rewind(cfp);
    if ((fl = fread(buff, file_size, 1, cfp)) != 1) ;
    fclose(cfp);

    for (cache_ptr = buff; cache_ptr - buff < file_size; cache_ptr += len) {
        cache_temp = (struct dns_cache *)malloc(sizeof(struct dns_cache));
        if (cache_temp == NULL)
            return 1;
        cache_temp->next = cache;
        cache = cache_temp;
        cache_using++;
        len = *(uint16_t *) cache_ptr + *(uint16_t *) (cache_ptr + *(uint16_t *) cache_ptr + 2) + 4;
        copy_new_mem(cache_ptr, len, &cache->dns_cache);
        if (cache->dns_cache == NULL)
            return 1;
    }
    /* 删除重复记录 */
    struct dns_cache *before, *after;
    for (; cache_temp; cache_temp = cache_temp->next) {
        for (before = cache_temp; before && (after = before->next) != NULL; before = before->next) {
            if (*(uint16_t *) after->dns_cache == *(uint16_t *) cache_temp->dns_cache && memcmp(after->dns_cache + 2, cache_temp->dns_cache + 2, *(uint16_t *) after->dns_cache) == 0) {
                before->next = after->next;
                free(after->dns_cache);
                free(after);
                cache_using--;
            }
        }
    }

    chmod(httpdns.cachePath, S_IWOTH | S_IROTH | S_IWGRP | S_IRGRP | S_IWUSR | S_IRUSR);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x30(%rbp)
movq $0x0, -0x20(%rbp)
movl $0x0, 0x154eb(%rip)     # 0x1c6cc
movq 0x15518(%rip), %rdi     # 0x1c700
leaq 0x52a5(%rip), %rsi      # 0xc494
callq 0x2470
movq %rax, 0x154c5(%rip)     # 0x1c6c0
cmpq $0x0, %rax
jne 0x7250
movq 0x154f8(%rip), %rdi     # 0x1c700
leaq 0x5015(%rip), %rsi      # 0xc224
callq 0x2470
movq %rax, 0x154a5(%rip)     # 0x1c6c0
cmpq $0x0, %rax
je 0x7247
movq 0x154d8(%rip), %rdi     # 0x1c700
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x2420
movq 0x15487(%rip), %rdi     # 0x1c6c0
callq 0x2100
movb $0x0, -0x1(%rbp)
jmp 0x7495
movb $0x1, -0x1(%rbp)
jmp 0x7495
movq 0x15469(%rip), %rdi     # 0x1c6c0
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x23c0
movq 0x15454(%rip), %rdi     # 0x1c6c0
callq 0x22e0
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rax
subq %rcx, %rax
movq %rax, %rsp
movq %rax, -0x10(%rbp)
cmpq $0x0, %rax
jne 0x729d
movb $0x1, -0x1(%rbp)
jmp 0x7495
movq 0x1541c(%rip), %rdi     # 0x1c6c0
callq 0x2190
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq 0x15408(%rip), %rcx     # 0x1c6c0
movl $0x1, %edx
callq 0x2090
movl %eax, -0x30(%rbp)
cmpl $0x1, %eax
je 0x72cc
jmp 0x72cc
movq 0x153ed(%rip), %rdi     # 0x1c6c0
callq 0x2100
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
cmpq -0x28(%rbp), %rax
jge 0x739f
movl $0x10, %edi
callq 0x2350
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x7313
movb $0x1, -0x1(%rbp)
jmp 0x7495
movq 0x1541e(%rip), %rcx     # 0x1c738
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq %rax, 0x1540b(%rip)     # 0x1c738
movl 0x15399(%rip), %eax     # 0x1c6cc
addl $0x1, %eax
movl %eax, 0x15390(%rip)     # 0x1c6cc
movq -0x18(%rbp), %rax
movzwl (%rax), %eax
movq -0x18(%rbp), %rcx
movq -0x18(%rbp), %rdx
movzwl (%rdx), %edx
movslq %edx, %rdx
movzwl 0x2(%rcx,%rdx), %ecx
addl %ecx, %eax
addl $0x4, %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq 0x153cc(%rip), %rdx     # 0x1c738
callq 0x4c90
movq 0x153c0(%rip), %rax     # 0x1c738
cmpq $0x0, (%rax)
jne 0x7387
movb $0x1, -0x1(%rbp)
jmp 0x7495
jmp 0x7389
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x18(%rbp)
jmp 0x72e0
jmp 0x73a1
cmpq $0x0, -0x20(%rbp)
je 0x7480
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x38(%rbp)
movb %al, -0x41(%rbp)
je 0x73d6
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, %rax
setne %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x73e2
jmp 0x746d
movq -0x40(%rbp), %rax
movq (%rax), %rax
movzwl (%rax), %eax
movq -0x20(%rbp), %rcx
movq (%rcx), %rcx
movzwl (%rcx), %ecx
cmpl %ecx, %eax
jne 0x745a
movq -0x40(%rbp), %rax
movq (%rax), %rdi
addq $0x2, %rdi
movq -0x20(%rbp), %rax
movq (%rax), %rsi
addq $0x2, %rsi
movq -0x40(%rbp), %rax
movq (%rax), %rax
movzwl (%rax), %eax
movl %eax, %edx
callq 0x2280
cmpl $0x0, %eax
jne 0x745a
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
callq 0x2030
movq -0x40(%rbp), %rdi
callq 0x2030
movl 0x1527b(%rip), %eax     # 0x1c6cc
addl $-0x1, %eax
movl %eax, 0x15272(%rip)     # 0x1c6cc
jmp 0x745c
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x73b4
jmp 0x746f
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x73a1
movq 0x15279(%rip), %rdi     # 0x1c700
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x2420
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
movq %rbp, %rsp
popq %rbp
retq
nopl (%rax)