// File: /workspace/github_c_makefile_star2_O1/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 0x74cb(%rip), %rdx      # 0x235aa
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 0x1c125
movl %eax, %ebp
cmpl $0x1, 0x10a(%r14)
jg 0x1c1ad
xorl %ecx, %ecx
testl %r15d, %r15d
setne %cl
jmp 0x1c1c4
callq 0x5070
cmpl $0x2, (%rax)
je 0x1c1f8
movq %rax, %rbx
movq 0xde83(%rip), %rax      # 0x29fc0
movq (%rax), %r14
callq 0xeb2f
movq %rax, %r15
movl (%rbx), %ebp
movl %ebp, %edi
callq 0x5530
leaq 0x746f(%rip), %rsi      # 0x235c7
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %rdx
movl %ebp, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
leaq 0xeb33(%rip), %rax      # 0x2aca8
movq (%rax), %r14
testq %r14, %r14
je 0x1c1f8
callq 0xeb2f
movq %rax, %r15
movl (%rbx), %ebx
movl %ebx, %edi
callq 0x5530
leaq 0x7432(%rip), %rsi      # 0x235c7
movq %rsp, %rcx
movq %r14, %rdi
movq %r15, %rdx
movl %ebx, %r8d
movq %rax, %r9
xorl %eax, %eax
callq 0x52c0
jmp 0x1c1f8
xorl %eax, %eax
testl %r15d, %r15d
sete %al
shll $0x7, %eax
movl 0x11e(%r14), %ecx
andl $0x7f, %ecx
orl %eax, %ecx
leaq 0x61b2(%rip), %rdx      # 0x2237d
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 0x1c208
movl %ebp, %edi
callq 0x5230
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0xca(%r14)
je 0x1c1f1
addq $0xca, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x52a0
testl %eax, %eax
je 0x1c1f1
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 0x1c1f1
leaq 0x73a2(%rip), %rsi      # 0x235f1
movq %rsp, %rdi
movl $0x2, %edx
callq 0x5370
testl %eax, %eax
je 0x1c27c
leaq 0x7388(%rip), %rsi      # 0x235ef
movq %rsp, %rdi
movl $0x4, %edx
callq 0x5370
testl %eax, %eax
jne 0x1c1f1
leaq 0x7371(%rip), %rdx      # 0x235f4
movq %rsp, %r14
movl $0x80, %esi
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x51c0
movq %r14, %rdi
callq 0x1c2a5
jmp 0x1c1f1