// File: /workspace/github_cpp_makefile_star2_O2/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 0x2170
movb $0x0, -0x38(%rbp)
movb %r14b, -0x37(%rbp)
movl $0x1, -0x34(%rbp)
andq $0x0, -0x30(%rbp)
leaq 0x8(%r12), %r14
movq %r14, %rdi
callq 0x3228
movl (%r12), %edi
movzbl %r13b, %edx
movl $0x703, %esi            # imm = 0x703
xorl %eax, %eax
callq 0x2280
cmpl $-0x1, %eax
je 0x307e
movl (%r12), %edi
leaq -0x38(%rbp), %rdx
movl $0x720, %esi            # imm = 0x720
xorl %eax, %eax
callq 0x2280
cmpl $-0x1, %eax
je 0x30c8
movl (%r12), %edi
movslq %ebx, %rdx
movq %r15, %rsi
callq 0x2290
movq %rax, %r15
movq %r14, %rdi
callq 0x32e4
cmpl %ebx, %r15d
jne 0x310f
xorl %ebx, %ebx
jmp 0x3195
movq %r14, %rdi
callq 0x32e4
leaq -0x1d0(%rbp), %rdi
leaq 0x40f5(%rip), %rsi      # 0x7189
callq 0x2220
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2220
leaq -0x1c8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x22e0
jmp 0x3189
movq %r14, %rdi
callq 0x32e4
leaq -0x1d0(%rbp), %rdi
leaq 0x40ab(%rip), %rsi      # 0x7189
callq 0x2220
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2220
leaq -0x1c8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x22e0
jmp 0x3189
leaq -0x1d0(%rbp), %rdi
cmpl $-0x1, %r15d
je 0x3153
leaq 0x4070(%rip), %rsi      # 0x7193
callq 0x2220
movq %rax, %rdi
movl %ebx, %esi
callq 0x2330
leaq 0x406f(%rip), %rsi      # 0x71a8
movq %rax, %rdi
callq 0x2220
leaq -0x1c8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x22e0
jmp 0x3189
leaq 0x402f(%rip), %rsi      # 0x7189
callq 0x2220
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2220
leaq -0x1c8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x22e0
leaq -0x58(%rbp), %rdi
movq (%rdi), %rbx
callq 0x20f0
leaq -0x1e0(%rbp), %rdi
callq 0x21b0
movq %rbx, %rax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31be
jmp 0x31be
jmp 0x31be
jmp 0x31be
movq %rax, %rbx
leaq -0x1e0(%rbp), %rdi
callq 0x21b0
movq %rbx, %rdi
callq 0x2350