// File: /workspace/github_c_makefile_star2_O1/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
movl $0x0, 0x13d7c(%rip)     # 0x1966c
movq 0x13da9(%rip), %rdi     # 0x196a0
leaq 0x3ae8(%rip), %rsi      # 0x93e6
callq 0x2440
movq %rax, 0x13d56(%rip)     # 0x19660
testq %rax, %rax
je 0x5a6c
xorl %ebx, %ebx
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x23a0
movq 0x13d35(%rip), %rdi     # 0x19660
callq 0x22b0
movq %rax, %r14
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x13d15(%rip), %rdi     # 0x19660
callq 0x2180
movq 0x13d09(%rip), %rcx     # 0x19660
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x20a0
movq 0x13cf2(%rip), %rdi     # 0x19660
callq 0x2110
movq %r14, -0x30(%rbp)
testq %r14, %r14
jle 0x5a54
movq %r15, %r12
movl $0x10, %edi
callq 0x2320
movb $0x1, %r13b
testq %rax, %rax
je 0x5aab
movq %rax, %rbx
movq 0x13d35(%rip), %rax     # 0x196d8
movq %rax, 0x8(%rbx)
incl 0x13cbf(%rip)           # 0x1966c
movq %rbx, 0x13d24(%rip)     # 0x196d8
movzwl (%r12), %eax
movzwl 0x2(%r12,%rax), %ecx
leal (%rax,%rcx), %r14d
addl $0x4, %r14d
movq %r12, %rdi
movl %r14d, %esi
movq %rbx, %rdx
callq 0x407e
movq 0x13cfc(%rip), %rax     # 0x196d8
cmpq $0x0, (%rax)
je 0x5aab
movl %r14d, %eax
addq %rax, %r12
movq %r12, %rax
subq %r15, %rax
cmpq -0x30(%rbp), %rax
jl 0x5983
jmp 0x5a54
movq %rbx, %r12
movq 0x8(%r12), %r14
testq %r14, %r14
je 0x5a50
movq (%r14), %r15
movzwl (%r15), %edx
movq (%rbx), %rsi
cmpw (%rsi), %dx
jne 0x5a46
leaq 0x2(%r15), %rdi
addq $0x2, %rsi
callq 0x2380
testl %eax, %eax
jne 0x5a46
movq 0x8(%r14), %rax
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
decl 0x13c26(%rip)           # 0x1966c
movq 0x8(%r12), %r12
testq %r12, %r12
jne 0x59fd
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x59fa
movq 0x13c40(%rip), %rdi     # 0x196a0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x23f0
jmp 0x5aa8
movq 0x13c2d(%rip), %rdi     # 0x196a0
leaq 0x396f(%rip), %rsi      # 0x93e9
callq 0x2440
movq %rax, 0x13bda(%rip)     # 0x19660
testq %rax, %rax
je 0x5abd
movq 0x13c0e(%rip), %rdi     # 0x196a0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x23f0
movq 0x13bbd(%rip), %rdi     # 0x19660
callq 0x2110
xorl %r13d, %r13d
movl %r13d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %r13b
jmp 0x5aab