// File: /workspace/github_c_makefile_star2_O3/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 0x11ed7(%rip), %rdx     # 0x20d7e
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 0x11e7b(%rip), %rdi     # 0x20d6d
leaq 0x1328d(%rip), %rsi     # 0x22186
callq 0x5470
testq %rax, %rax
je 0xef9e
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 0xef8a
movq %r12, %rdi
callq 0x5160
cmpq $0x2, %rax
jb 0xef5f
cmpb $0xa, -0x1(%rax,%r12)
jne 0xef5f
movb $0x0, 0xcf(%rsp,%rax)
movq %r12, %rdi
movq %r13, %rsi
callq 0x5550
testq %rax, %rax
jne 0xef86
movq %r12, %rdi
callq 0x5520
movq %rax, 0x10(%rsp,%rbx,8)
incq %rbx
cmpl $0x10, %ebx
jne 0xef2c
jmp 0xef8a
incl %ebp
jmp 0xef29
movq %r15, %rdi
callq 0x5130
testl %ebp, %ebp
sete %al
movq 0x8(%rsp), %rbx
jmp 0xefb2
callq 0x5070
movq %rax, %r15
movb $0x1, %al
cmpl $0x2, (%r15)
jne 0xf058
testq %r14, %r14
sete %cl
testb %al, %cl
jne 0xf189
leaq 0x11da6(%rip), %rdi     # 0x20d6d
leaq 0x12673(%rip), %rsi     # 0x21641
callq 0x5470
testq %rax, %rax
je 0xf0eb
movq %rax, %r15
testq %r14, %r14
je 0xf000
leaq 0x11dc1(%rip), %rsi     # 0x20dac
leaq 0x90(%rsp), %rcx
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x52c0
testq %rbx, %rbx
je 0xf021
leaq 0x11da0(%rip), %rsi     # 0x20dac
leaq 0x90(%rsp), %rcx
movq %r15, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x52c0
leaq 0xff05(%rip), %rbx      # 0x1ef2d
xorl %r14d, %r14d
movq 0x10(%rsp,%r14,8), %rdx
testq %rdx, %rdx
je 0xf04b
movq %r15, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x52c0
incq %r14
cmpq $0x10, %r14
jne 0xf02b
movq %r15, %rdi
callq 0x5130
jmp 0xf16f
movq 0x1af61(%rip), %rax     # 0x29fc0
movq (%rax), %rbx
callq 0xea3b
movl (%r15), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11d11(%rip), %rsi     # 0x20d89
leaq 0x1bc31(%rip), %rdx     # 0x2acb0
leaq 0x11ce7(%rip), %rcx     # 0x20d6d
movq %rbx, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1bc0b(%rip), %rbx     # 0x2aca8
testq %rbx, %rbx
je 0xf189
callq 0xea3b
movl (%r15), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11ccd(%rip), %rsi     # 0x20d89
leaq 0x1bbed(%rip), %rdx     # 0x2acb0
leaq 0x11ca3(%rip), %rcx     # 0x20d6d
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 0x1aece(%rip), %rax     # 0x29fc0
movq (%rax), %r14
callq 0xea3b
callq 0x5070
movq %rax, %rbx
movl (%rax), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x11c77(%rip), %rsi     # 0x20d89
leaq 0x1bb97(%rip), %rdx     # 0x2acb0
leaq 0x11c4d(%rip), %rcx     # 0x20d6d
movq %r14, %rdi
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
movq 0x1bb71(%rip), %r14     # 0x2aca8
testq %r14, %r14
je 0xf16f
callq 0xea3b
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x11c38(%rip), %rsi     # 0x20d89
leaq 0x1bb58(%rip), %rdx     # 0x2acb0
leaq 0x11c0e(%rip), %rcx     # 0x20d6d
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 0xf189
callq 0x5040
incq %rbx
cmpq $0x10, %rbx
jne 0xf171
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq