// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rdx, %r14
movq %rsi, %r8
movl %edi, %ecx
leaq 0x11ded(%rip), %rdx     # 0x20d82
xorl %ebp, %ebp
leaq 0x90(%rsp), %rdi
movl $0x40, %esi
xorl %eax, %eax
callq 0x51c0
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
leaq 0x11d91(%rip), %rdi     # 0x20d71
leaq 0x13177(%rip), %rsi     # 0x2215e
callq 0x5470
testq %rax, %rax
je 0xf08c
movq %rax, %r15
movq %rbx, 0x8(%rsp)
movb $0x0, 0x1cf(%rsp)
leaq 0xd0(%rsp), %r12
leaq 0x90(%rsp), %r13
xorl %ebx, %ebx
movslq %ebx, %rbx
movq %r12, %rdi
movl $0xff, %esi
movq %r15, %rdx
callq 0x5270
testq %rax, %rax
je 0xf078
movq %r12, %rdi
callq 0x5160
cmpq $0x2, %rax
jb 0xf04d
cmpb $0xa, -0x1(%rax,%r12)
jne 0xf04d
movb $0x0, 0xcf(%rsp,%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x5550
testq %rax, %rax
jne 0xf074
movq %r12, %rdi
callq 0x5520
movq %rax, 0x10(%rsp,%rbx,8)
incq %rbx
cmpl $0x10, %ebx
jne 0xf01a
jmp 0xf078
incl %ebp
jmp 0xf017
movq %r15, %rdi
callq 0x5130
testl %ebp, %ebp
sete %al
movq 0x8(%rsp), %rbx
jmp 0xf0a0
callq 0x5070
movq %rax, %r15
movb $0x1, %al
cmpl $0x2, (%r15)
jne 0xf146
testq %r14, %r14
sete %cl
testb %al, %cl
jne 0xf277
leaq 0x11cbc(%rip), %rdi     # 0x20d71
leaq 0x1255d(%rip), %rsi     # 0x21619
callq 0x5470
testq %rax, %rax
je 0xf1d9
movq %rax, %r15
testq %r14, %r14
je 0xf0ee
leaq 0x11cd7(%rip), %rsi     # 0x20db0
leaq 0x90(%rsp), %rcx
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x52c0
testq %rbx, %rbx
je 0xf10f
leaq 0x11cb6(%rip), %rsi     # 0x20db0
leaq 0x90(%rsp), %rcx
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xfe1b(%rip), %rbx      # 0x1ef31
xorl %r14d, %r14d
movq 0x10(%rsp,%r14,8), %rdx
testq %rdx, %rdx
je 0xf139
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x52c0
incq %r14
cmpq $0x10, %r14
jne 0xf119
movq %r15, %rdi
callq 0x5130
jmp 0xf25d
movq 0x1ae73(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xeb2f
movl (%r15), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11c27(%rip), %rsi     # 0x20d8d
leaq 0x1bb43(%rip), %rdx     # 0x2acb0
leaq 0x11bfd(%rip), %rcx     # 0x20d71
movq %rbx, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1bb1d(%rip), %rbx     # 0x2aca8
testq %rbx, %rbx
je 0xf277
callq 0xeb2f
movl (%r15), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11be3(%rip), %rsi     # 0x20d8d
leaq 0x1baff(%rip), %rdx     # 0x2acb0
leaq 0x11bb9(%rip), %rcx     # 0x20d71
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 0x1ade0(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11b8d(%rip), %rsi     # 0x20d8d
leaq 0x1baa9(%rip), %rdx     # 0x2acb0
leaq 0x11b63(%rip), %rcx     # 0x20d71
movq %r14, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1ba83(%rip), %r14     # 0x2aca8
testq %r14, %r14
je 0xf25d
callq 0xeb2f
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x11b4e(%rip), %rsi     # 0x20d8d
leaq 0x1ba6a(%rip), %rdx     # 0x2acb0
leaq 0x11b24(%rip), %rcx     # 0x20d71
movq %r14, %rdi
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
xorl %ebx, %ebx
movq 0x10(%rsp,%rbx,8), %rdi
testq %rdi, %rdi
je 0xf277
callq 0x5040
incq %rbx
cmpq $0x10, %rbx
jne 0xf25f
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq