// File: /workspace/github_c_makefile_star2_O3/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 0x7889(%rip), %rdx      # 0x23600
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 0x5440
cmpl $-0x1, %eax
je 0x1bdbd
movl %eax, %ebp
cmpl $0x1, 0x10a(%r14)
jg 0x1be45
xorl %ecx, %ecx
testl %r15d, %r15d
setne %cl
jmp 0x1be5c
callq 0x5070
cmpl $0x2, (%rax)
je 0x1be90
movq %rax, %rbx
movq 0xe1eb(%rip), %rax      # 0x29fc0
movq (%rax), %r14
callq 0xea3b
movq %rax, %r15
movl (%rbx), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x782d(%rip), %rsi      # 0x2361d
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0xee9b(%rip), %rax      # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x1be90
callq 0xea3b
movq %rax, %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x77f0(%rip), %rsi      # 0x2361d
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %rdx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
jmp 0x1be90
xorl %eax, %eax
testl %r15d, %r15d
sete %al
shll $0x7, %eax
movl 0x11e(%r14), %ecx
andl $0x7f, %ecx
orl %eax, %ecx
leaq 0x6542(%rip), %rdx      # 0x223a5
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 0x1bea0
movl %ebp, %edi
callq 0x5230
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0xca(%r14)
je 0x1be89
addq $0xca, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x52a0
testl %eax, %eax
je 0x1be89
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 0x1be89
leaq 0x7760(%rip), %rsi      # 0x23647
movq %rsp, %rdi
movl $0x2, %edx
callq 0x5370
testl %eax, %eax
je 0x1bf14
leaq 0x7746(%rip), %rsi      # 0x23645
movq %rsp, %rdi
movl $0x4, %edx
callq 0x5370
testl %eax, %eax
jne 0x1be89
leaq 0x772f(%rip), %rdx      # 0x2364a
movq %rsp, %r14
movl $0x80, %esi
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x51c0
movq %r14, %rdi
callq 0x1bf3d
jmp 0x1be89