// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
andl $0x0, 0x140c3(%rip)     # 0x1967c
movq 0x140f0(%rip), %rdi     # 0x196b0
leaq 0x3e17(%rip), %rsi      # 0x93de
callq 0x2450
movq %rax, 0x1409d(%rip)     # 0x19670
testq %rax, %rax
je 0x572e
xorl %ebx, %ebx
pushq $0x2
popq %rdx
movq %rax, %rdi
xorl %esi, %esi
callq 0x23b0
movq 0x1407e(%rip), %rdi     # 0x19670
callq 0x22c0
movq %rax, %r14
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x1405e(%rip), %rdi     # 0x19670
callq 0x2180
movq 0x14052(%rip), %rcx     # 0x19670
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r14, -0x30(%rbp)
movq %r14, %rsi
callq 0x20a0
movq 0x14039(%rip), %rdi     # 0x19670
callq 0x2110
movq %r15, %r13
movq %r13, %rax
subq %r15, %rax
cmpq -0x30(%rbp), %rax
jge 0x5716
pushq $0x10
popq %rdi
callq 0x2330
movb $0x1, %r12b
testq %rax, %rax
je 0x5772
movq %rax, %rbx
movq 0x1407b(%rip), %rax     # 0x196e8
movq %rax, 0x8(%rbx)
incl 0x14005(%rip)           # 0x1967c
movq %rbx, 0x1406a(%rip)     # 0x196e8
movzwl (%r13), %eax
movzwl 0x2(%r13,%rax), %ecx
leal (%rax,%rcx), %r14d
addl $0x4, %r14d
movq %r13, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0x3ee8
movq 0x14042(%rip), %rax     # 0x196e8
cmpq $0x0, (%rax)
je 0x5772
movl %r14d, %eax
addq %rax, %r13
jmp 0x563f
movq %rbx, %r14
testq %r14, %r14
je 0x5712
movq %r14, %r12
movq 0x8(%r14), %r14
testq %r14, %r14
je 0x5712
movq (%r14), %r15
movzwl (%r15), %edx
movq (%rbx), %rsi
cmpw (%rsi), %dx
jne 0x56bb
leaq 0x2(%r15), %rdi
addq $0x2, %rsi
callq 0x2390
testl %eax, %eax
jne 0x56bb
movq 0x8(%r14), %rax
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
decl 0x13f71(%rip)           # 0x1967c
movq 0x8(%r12), %r14
jmp 0x56bb
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x56b8
movq 0x13f8e(%rip), %rdi     # 0x196b0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x2400
jmp 0x576a
movq 0x13f7b(%rip), %rdi     # 0x196b0
leaq 0x3ca5(%rip), %rsi      # 0x93e1
callq 0x2450
movq %rax, 0x13f28(%rip)     # 0x19670
testq %rax, %rax
je 0x576f
movq 0x13f5c(%rip), %rdi     # 0x196b0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x2400
movq 0x13f0b(%rip), %rdi     # 0x19670
callq 0x2110
xorl %r12d, %r12d
jmp 0x5772
movb $0x1, %r12b
movl %r12d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq