// File: /workspace/github_c_makefile_star2_O3/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, 0x13e20(%rip)     # 0x1967c
movq 0x13e4d(%rip), %rdi     # 0x196b0
leaq 0x3b74(%rip), %rsi      # 0x93de
callq 0x2450
movq %rax, 0x13dfa(%rip)     # 0x19670
testq %rax, %rax
je 0x59d7
movq %rax, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x23b0
movq 0x13ddb(%rip), %rdi     # 0x19670
callq 0x22c0
movq %rax, %rbx
movq %rsp, %r15
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r15
movq %r15, %rsp
movq 0x13dbb(%rip), %rdi     # 0x19670
callq 0x2180
movq 0x13daf(%rip), %rcx     # 0x19670
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x20a0
movq 0x13d98(%rip), %rdi     # 0x19670
callq 0x2110
movq %rbx, -0x30(%rbp)
testq %rbx, %rbx
jle 0x59c4
movq %r15, %r12
movl $0x10, %edi
callq 0x2330
movb $0x1, %r13b
testq %rax, %rax
je 0x5a16
movq %rax, %rbx
movq 0x13ddb(%rip), %rax     # 0x196e8
movq %rax, 0x8(%rbx)
incl 0x13d65(%rip)           # 0x1967c
movq %rbx, 0x13dca(%rip)     # 0x196e8
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 0x4072
movq 0x13da2(%rip), %rax     # 0x196e8
cmpq $0x0, (%rax)
je 0x5a16
movl %r14d, %eax
addq %rax, %r12
movq %r12, %rax
subq %r15, %rax
cmpq -0x30(%rbp), %rax
jl 0x58ed
movq %rbx, %r12
movq 0x8(%r12), %r14
testq %r14, %r14
je 0x59bb
movq (%r14), %r15
movzwl (%r15), %edx
movq (%rbx), %rsi
cmpw (%rsi), %dx
jne 0x59b3
leaq 0x2(%r15), %rdi
addq $0x2, %rsi
callq 0x2390
testl %eax, %eax
jne 0x59b3
movq 0x8(%r14), %rax
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0x2030
movq %r14, %rdi
callq 0x2030
decl 0x13cce(%rip)           # 0x1967c
movq 0x8(%r12), %r14
movq %r14, %r12
testq %r14, %r14
jne 0x5965
movq 0x8(%rbx), %rbx
testq %rbx, %rbx
jne 0x5962
movq 0x13ce5(%rip), %rdi     # 0x196b0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x2400
jmp 0x5a13
movq 0x13cd2(%rip), %rdi     # 0x196b0
leaq 0x39fc(%rip), %rsi      # 0x93e1
callq 0x2450
movq %rax, 0x13c7f(%rip)     # 0x19670
testq %rax, %rax
je 0x5a28
movq 0x13cb3(%rip), %rdi     # 0x196b0
movl $0x1b6, %esi            # imm = 0x1B6
callq 0x2400
movq 0x13c62(%rip), %rdi     # 0x19670
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 0x5a16