// File: /workspace/github_c_makefile_star2_O2/Carton32[P]quectel-CM/udhcpc.c
// Function: ql_set_driver_link_state
void ql_set_driver_link_state(PROFILE_T *profile, int link_state) {
    char link_file[128];
    int fd;
    int new_state = 0;

    snprintf(link_file, sizeof(link_file), "/sys/class/net/%s/link_state", profile->usbnet_adapter);
    fd = open(link_file, O_RDWR | O_NONBLOCK | O_NOCTTY);
    if (fd == -1) {
        if (errno != ENOENT)
            dbg_time("Fail to access %s, errno: %d (%s)", link_file, errno, strerror(errno));
        return;
    }

    if (profile->qmap_mode <= 1)
        new_state = !!link_state;
    else {
        //0x80 means link off this pdp
        new_state = (link_state ? 0x00 : 0x80) + (profile->muxid & 0x7F);
    }

    snprintf(link_file, sizeof(link_file), "%d\n", new_state);
    if (write(fd, link_file, sizeof(link_file)) == -1) {};

    if (link_state == 0 && profile->qmapnet_adapter[0]
        && strcmp(profile->qmapnet_adapter, profile->usbnet_adapter)) {
        size_t rc;

        lseek(fd, 0, SEEK_SET);
        rc = read(fd, link_file, sizeof(link_file));
        if (rc > 1 && (!strncasecmp(link_file, "0\n", 2) || !strncasecmp(link_file, "0x0\n", 4))) {
            snprintf(link_file, sizeof(link_file), "ifconfig %s down", profile->usbnet_adapter);
            ql_system(link_file);
        }
    }

    close(fd);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
movl %esi, %r15d
movq %rdi, %r14
leaq 0xaa(%rdi), %rbx
leaq 0x6c18(%rip), %rdx      # 0x2161f
movq %rsp, %r12
movl $0x80, %esi
movq %r12, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x51c0
movq %r12, %rdi
movl $0x902, %esi            # imm = 0x902
xorl %eax, %eax
callq 0x5450
cmpl $-0x1, %eax
je 0x1aa4d
movl %eax, %ebp
cmpl $0x1, 0x10a(%r14)
jg 0x1aad5
xorl %ecx, %ecx
testl %r15d, %r15d
setne %cl
jmp 0x1aaec
callq 0x5070
cmpl $0x2, (%rax)
je 0x1ab20
movq %rax, %rbx
movq 0xe553(%rip), %rax      # 0x28fb8
movq (%rax), %r14
callq 0xe343
movq %rax, %r15
movl (%rbx), %ebp
movl %ebp, %edi
callq 0x5540
leaq 0x6bbc(%rip), %rsi      # 0x2163c
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0xf20b(%rip), %rax      # 0x29ca8
movq (%rax), %r14
testq %r14, %r14
je 0x1ab20
callq 0xe343
movq %rax, %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5540
leaq 0x6b7f(%rip), %rsi      # 0x2163c
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %rdx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
jmp 0x1ab20
xorl %eax, %eax
testl %r15d, %r15d
sete %al
shll $0x7, %eax
movl 0x11e(%r14), %ecx
andl $0x7f, %ecx
orl %eax, %ecx
leaq 0x58c7(%rip), %rdx      # 0x203ba
movq %rsp, %r12
movl $0x80, %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x51c0
movl $0x80, %edx
movl %ebp, %edi
movq %r12, %rsi
callq 0x5100
testl %r15d, %r15d
je 0x1ab30
movl %ebp, %edi
callq 0x5230
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0xca(%r14)
je 0x1ab19
addq $0xca, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x52a0
testl %eax, %eax
je 0x1ab19
movl %ebp, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x51e0
movq %rsp, %rsi
movl $0x80, %edx
movl %ebp, %edi
callq 0x5250
cmpq $0x2, %rax
jb 0x1ab19
leaq 0x6aef(%rip), %rsi      # 0x21666
movq %rsp, %rdi
pushq $0x2
popq %rdx
callq 0x5370
testl %eax, %eax
je 0x1aba0
leaq 0x6ad7(%rip), %rsi      # 0x21664
movq %rsp, %rdi
pushq $0x4
popq %rdx
callq 0x5370
testl %eax, %eax
jne 0x1ab19
leaq 0x6ac2(%rip), %rdx      # 0x21669
movq %rsp, %r14
movl $0x80, %esi
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x51c0
movq %r14, %rdi
callq 0x1abc9
jmp 0x1ab19