// File: /workspace/github_c_makefile_star2_O2/Carton32[P]quectel-CM/util.c
// Function: update_resolv_conf
void update_resolv_conf(int iptype, const char *ifname, const char *dns1, const char *dns2) {
    const char *dns_file = "/etc/resolv.conf";
    FILE *dns_fp;
    char dns_line[256];
    #define MAX_DNS 16
    char *dns_info[MAX_DNS];
    char dns_tag[64];
    int dns_match = 0;
    int i;

    snprintf(dns_tag, sizeof(dns_tag), "# IPV%d %s", iptype, ifname);

    for (i = 0; i < MAX_DNS; i++)
        dns_info[i] = NULL;
    
    dns_fp = fopen(dns_file, "r");
    if (dns_fp) {
        i = 0;    
        dns_line[sizeof(dns_line)-1] = '\0';
        
        while((fgets(dns_line, sizeof(dns_line)-1, dns_fp)) != NULL) {
            if ((strlen(dns_line) > 1) && (dns_line[strlen(dns_line) - 1] == '\n'))
                dns_line[strlen(dns_line) - 1] = '\0';
            //dbg_time("%s", dns_line);
            if (strstr(dns_line, dns_tag)) {
                dns_match++;
                continue;
            }
            dns_info[i++] = strdup(dns_line);
            if (i == MAX_DNS)
                break;
        }

        fclose(dns_fp);
    }
    else if (errno != ENOENT) {
        dbg_time("fopen %s fail, errno:%d (%s)", dns_file, errno, strerror(errno));
        return;
    }
    
    if (dns1 == NULL && dns_match == 0)
        return;

    dns_fp = fopen(dns_file, "w");
    if (dns_fp) {
        if (dns1)
            fprintf(dns_fp, "nameserver %s %s\n", dns1, dns_tag);
        if (dns2)
            fprintf(dns_fp, "nameserver %s %s\n", dns2, dns_tag);
        
        for (i = 0; i < MAX_DNS && dns_info[i]; i++)
            fprintf(dns_fp, "%s\n", dns_info[i]);
        fclose(dns_fp);
    }
    else {
        dbg_time("fopen %s fail, errno:%d (%s)", dns_file, errno, strerror(errno));
    }

    for (i = 0; i < MAX_DNS && dns_info[i]; i++)
        free(dns_info[i]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r8
movl %edi, %ecx
leaq 0x10622(%rip), %rdx     # 0x1ed94
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
pushq $0x40
popq %rsi
xorl %eax, %eax
callq 0x51c0
cmpq $0x10, %rbx
je 0xe794
andq $0x0, 0x50(%rsp,%rbx,8)
incq %rbx
jmp 0xe783
leaq 0x105e8(%rip), %rdi     # 0x1ed83
leaq 0x119f9(%rip), %rsi     # 0x2019b
callq 0x5480
testq %rax, %rax
je 0xe84c
movq %rax, %r15
movq %r14, 0x8(%rsp)
movq %r12, %r14
movb $0x0, 0x1cf(%rsp)
xorl %ebp, %ebp
leaq 0xd0(%rsp), %r12
leaq 0x10(%rsp), %r13
xorl %ebx, %ebx
movslq %ebx, %rbx
movq %r12, %rdi
movl $0xff, %esi
movq %r15, %rdx
callq 0x5270
testq %rax, %rax
je 0xe835
movq %r12, %rdi
callq 0x5160
cmpq $0x2, %rax
jb 0xe80a
cmpb $0xa, -0x1(%rax,%r12)
jne 0xe80a
movb $0x0, 0xcf(%rsp,%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x5560
testq %rax, %rax
jne 0xe831
movq %r12, %rdi
callq 0x5530
movq %rax, 0x50(%rsp,%rbx,8)
incq %rbx
cmpl $0x10, %ebx
jne 0xe7d7
jmp 0xe835
incl %ebp
jmp 0xe7d4
movq %r15, %rdi
callq 0x5130
testl %ebp, %ebp
sete %al
movq %r14, %r12
movq 0x8(%rsp), %r14
jmp 0xe860
callq 0x5070
movq %rax, %r15
movb $0x1, %al
cmpl $0x2, (%r15)
jne 0xe910
testq %r14, %r14
sete %cl
testb %al, %cl
je 0xe87c
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x10500(%rip), %rdi     # 0x1ed83
leaq 0x10dd3(%rip), %rsi     # 0x1f65d
callq 0x5480
testq %rax, %rax
je 0xe9a3
movq %rax, %r15
testq %r14, %r14
je 0xe8b9
leaq 0x1051b(%rip), %rsi     # 0x1edc2
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x52c0
testq %r12, %r12
je 0xe8d7
leaq 0x104fd(%rip), %rsi     # 0x1edc2
leaq 0x10(%rsp), %rcx
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xe64f(%rip), %rbx      # 0x1cf2d
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0xe903
movq 0x50(%rsp,%r14,8), %rdx
testq %rdx, %rdx
je 0xe903
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x52c0
incq %r14
jmp 0xe8e1
movq %r15, %rdi
callq 0x5130
jmp 0xea27
movq 0x1a6a1(%rip), %rax     # 0x28fb8
movq (%rax), %rbx
callq 0xe343
movl (%r15), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x1046f(%rip), %rsi     # 0x1ed9f
leaq 0x1b379(%rip), %rdx     # 0x29cb0
leaq 0x10445(%rip), %rcx     # 0x1ed83
movq %rbx, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1b353(%rip), %rbx     # 0x29ca8
testq %rbx, %rbx
je 0xe86a
callq 0xe343
movl (%r15), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x1042b(%rip), %rsi     # 0x1ed9f
leaq 0x1b335(%rip), %rdx     # 0x29cb0
leaq 0x10401(%rip), %rcx     # 0x1ed83
movq %rbx, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x52c0
movq 0x1a60e(%rip), %rax     # 0x28fb8
movq (%rax), %r14
callq 0xe343
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x103d5(%rip), %rsi     # 0x1ed9f
leaq 0x1b2df(%rip), %rdx     # 0x29cb0
leaq 0x103ab(%rip), %rcx     # 0x1ed83
movq %r14, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1b2b9(%rip), %r14     # 0x29ca8
testq %r14, %r14
je 0xea27
callq 0xe343
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5540
leaq 0x10396(%rip), %rsi     # 0x1ed9f
leaq 0x1b2a0(%rip), %rdx     # 0x29cb0
leaq 0x1036c(%rip), %rcx     # 0x1ed83
movq %r14, %rdi
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
xorl %ebx, %ebx
cmpq $0x10, %rbx
je 0xe86a
movq 0x50(%rsp,%rbx,8), %rdi
testq %rdi, %rdi
je 0xe86a
callq 0x5040
incq %rbx
jmp 0xea29