// File: /workspace/github_cpp_makefile_star2_O3/rachit31[P]CubeSatADCS/i2c.cpp
// Function: I2C::readBuffer(unsigned char, unsigned char, unsigned char*, int)
const char* I2C::readBuffer(uint8_t busAddr, uint8_t dataAddr, uint8_t* buffer, int number)
{
  std::stringstream error;
  int retVal;

  struct i2c_smbus_ioctl_data args;

  args.read_write = 0;
  args.command = dataAddr;
  args.size = 1;
  args.data = NULL;

  m_mutex.lock();
  if (ioctl(m_busFd, I2C_SLAVE, busAddr) == -1)
  {
    m_mutex.unlock();
    error << "ioctl(): " << std::strerror(errno);
    return error.str().c_str();
  }

  if (ioctl(m_busFd,I2C_SMBUS,&args) == -1)
  {
    m_mutex.unlock();
    error << "ioctl(): " << std::strerror(errno);
    return error.str().c_str();
  }

  retVal = read(m_busFd, buffer, number);
  m_mutex.unlock();

  if (retVal != number)
  {
    if (retVal == -1)
    { 
      error << "ioctl(): " << std::strerror(errno);
      return error.str().c_str();
    }
    else
    {
      error << "read() did not read " << number << " bytes";
      return error.str().c_str();
    }
  }

  return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movl %r8d, %ebx
movq %rcx, %r15
movl %edx, %r14d
movl %esi, %r13d
movq %rdi, %r12
leaq -0x1e0(%rbp), %rdi
callq 0x2180
movb $0x0, -0x58(%rbp)
movb %r14b, -0x57(%rbp)
movl $0x1, -0x54(%rbp)
movq $0x0, -0x50(%rbp)
leaq 0x8(%r12), %r14
movq %r14, %rdi
callq 0x3712
movl (%r12), %edi
movzbl %r13b, %edx
movl $0x703, %esi            # imm = 0x703
xorl %eax, %eax
callq 0x22b0
cmpl $-0x1, %eax
je 0x3461
movl (%r12), %edi
leaq -0x58(%rbp), %rdx
movl $0x720, %esi            # imm = 0x720
xorl %eax, %eax
callq 0x22b0
cmpl $-0x1, %eax
je 0x34b7
movl (%r12), %edi
movslq %ebx, %rdx
movq %r15, %rsi
callq 0x22c0
movq %rax, %r15
movq %r14, %rdi
callq 0x3840
cmpl %ebx, %r15d
jne 0x350d
xorl %ebx, %ebx
jmp 0x3658
movq %r14, %rdi
callq 0x3840
leaq -0x1d0(%rbp), %rbx
leaq 0x4d12(%rip), %rsi      # 0x8189
movl $0x9, %edx
movq %rbx, %rdi
callq 0x2280
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x3565
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2280
jmp 0x3585
movq %r14, %rdi
callq 0x3840
leaq -0x1d0(%rbp), %rbx
leaq 0x4cbc(%rip), %rsi      # 0x8189
movl $0x9, %edx
movq %rbx, %rdi
callq 0x2280
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x359a
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2280
jmp 0x35ba
leaq -0x1d0(%rbp), %r14
cmpl $-0x1, %r15d
je 0x35cc
leaq 0x4c6e(%rip), %rsi      # 0x8193
movl $0x14, %edx
movq %r14, %rdi
callq 0x2280
movq %r14, %rdi
movl %ebx, %esi
callq 0x2380
leaq 0x4c65(%rip), %rsi      # 0x81a8
movl $0x6, %edx
movq %rax, %rdi
callq 0x2280
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2320
jmp 0x363c
movq -0x1d0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1b0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2360
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2320
jmp 0x363c
movq -0x1d0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1b0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2360
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2320
jmp 0x363c
leaq 0x4bb6(%rip), %rsi      # 0x8189
movl $0x9, %edx
movq %r14, %rdi
callq 0x2280
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x360c
movq %rax, %rbx
movq %rax, %rdi
callq 0x2100
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2280
jmp 0x362c
movq -0x1d0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %r14
movl -0x1b0(%rbp,%rax), %esi
orl $0x1, %esi
movq %r14, %rdi
callq 0x2360
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2320
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rbx
cmpq %rax, %rbx
je 0x3658
movq -0x38(%rbp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x2260
movq 0x7959(%rip), %rsi      # 0xafb8
leaq -0x1e0(%rbp), %rdi
callq 0x21c0
leaq -0x160(%rbp), %rdi
callq 0x20c0
movq %rbx, %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3694
jmp 0x3694
jmp 0x3694
jmp 0x3694
movq %rax, %rbx
movq 0x791a(%rip), %rsi      # 0xafb8
leaq -0x1e0(%rbp), %rdi
callq 0x21c0
leaq -0x160(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x23a0