// File: /workspace/github_c_makefile_star2_O2/ZombiesYard[P]MPU6050-DMP-raspberry-pi/libs/I2Cdev/I2Cdev.c
// Function: writeWords(unsigned char, unsigned char, unsigned char, unsigned short*)
int writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t* data) {
    int8_t count = 0;
    uint8_t buf[128];
    int i, fd;

    // Should do potential byteswap and call writeBytes() really, but that
    // messes with the callers buffer

    if (length > 63) {
        fprintf(stderr, "Word write count (%d) > 63\n", length);
        return -1;
    }

    fd = open("/dev/i2c-1", O_RDWR);
    if (fd < 0) {
        fprintf(stderr, "Failed to open device: %s\n", strerror(errno));
        return -1;
    }
    if (ioctl(fd, I2C_SLAVE, devAddr) < 0) {
        fprintf(stderr, "Failed to select device: %s\n", strerror(errno));
        close(fd);
        return -1;
    }
    buf[0] = regAddr;
    for (i = 0; i < length; i++) {
        buf[i*2+1] = data[i] >> 8;
        buf[i*2+2] = data[i];
    }
    count = write(fd, buf, length*2+1);
    if (count < 0) {
        fprintf(stderr, "Failed to write device(%d): %s\n", count, strerror(errno));
        close(fd);
        return -1;
    } else if (count != length*2+1) {
        fprintf(stderr, "Short write to device, expected %d, got %d\n", length+1, count);
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
movl %edx, %r14d
cmpb $0x40, %r14b
jae 0x4d3c
movq %rcx, %r15
movl %esi, %ebp
movl %edi, %r12d
leaq 0x1563(%rip), %rdi      # 0x6218
pushq $0x2
popq %rsi
xorl %eax, %eax
callq 0x1070
testl %eax, %eax
js 0x4d5c
movl %eax, %ebx
movzbl %r12b, %edx
movl $0x703, %esi            # imm = 0x703
movl %eax, %edi
xorl %eax, %eax
callq 0x10d0
testl %eax, %eax
js 0x4d8b
movb %bpl, (%rsp)
movzbl %r14b, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x4d05
movzwl (%r15,%rcx,2), %edx
rolw $0x8, %dx
movw %dx, 0x1(%rsp,%rcx,2)
incq %rcx
jmp 0x4ced
leal 0x1(,%r14,2), %r15d
movq %rsp, %rsi
movl %ebx, %edi
movq %r15, %rdx
callq 0x1060
movsbl %al, %ebp
testl %ebp, %ebp
js 0x4db7
cmpl %r15d, %ebp
jne 0x4de5
movl %ebx, %edi
callq 0x1120
xorl %eax, %eax
jmp 0x4e0f
movq 0x429d(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x15dd(%rip), %rsi      # 0x632a
movl %r14d, %edx
xorl %eax, %eax
callq 0x10f0
jmp 0x4e0c
movq 0x427d(%rip), %rax      # 0x8fe0
movq (%rax), %rbx
callq 0x1030
movl (%rax), %edi
callq 0x1050
leaq 0x14aa(%rip), %rsi      # 0x6223
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x4e0c
movq 0x424e(%rip), %rax      # 0x8fe0
movq (%rax), %r14
callq 0x1030
movl (%rax), %edi
callq 0x1050
leaq 0x1496(%rip), %rsi      # 0x623e
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x4e05
movq 0x4222(%rip), %rax      # 0x8fe0
movq (%rax), %r14
callq 0x1030
movl (%rax), %edi
callq 0x1050
leaq 0x150a(%rip), %rsi      # 0x62de
movq %r14, %rdi
movl %ebp, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x10f0
jmp 0x4e05
movq 0x41f4(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
incl %r14d
leaq 0x1505(%rip), %rsi      # 0x62fe
movl %r14d, %edx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x10f0
movl %ebx, %edi
callq 0x1120
pushq $-0x1
popq %rax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq