// File: 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
subq $0x2a0, %rsp            # imm = 0x2A0
movb %dl, %al
movb %sil, %dl
movq %rdi, -0x10(%rbp)
movb %dl, -0x11(%rbp)
movb %al, -0x12(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x268(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x3190
movq -0x268(%rbp), %rdi
movb $0x0, -0x1c8(%rbp)
movb -0x12(%rbp), %al
movb %al, -0x1c7(%rbp)
movl $0x1, -0x1c4(%rbp)
movq $0x0, -0x1c0(%rbp)
addq $0x8, %rdi
callq 0x49e0
jmp 0x4620
movq -0x268(%rbp), %rax
movl (%rax), %edi
movzbl -0x11(%rbp), %edx
movl $0x703, %esi            # imm = 0x703
movb $0x0, %al
callq 0x3360
cmpl $-0x1, %eax
jne 0x46f0
movq -0x268(%rbp), %rdi
addq $0x8, %rdi
callq 0x4b30
jmp 0x4654
leaq -0x1a0(%rbp), %rdi
leaq 0x8b2f(%rip), %rsi      # 0xd191
callq 0x3270
movq %rax, -0x270(%rbp)
jmp 0x4670
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq -0x270(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
jmp 0x468d
leaq -0x1f8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x46a2
leaq -0x1f8(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1fc(%rbp)
jmp 0x4907
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1d0(%rbp)
movl %eax, -0x1d4(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x31d0
jmp 0x4920
movq -0x268(%rbp), %rax
movl (%rax), %edi
movl $0x720, %esi            # imm = 0x720
leaq -0x1c8(%rbp), %rdx
movb $0x0, %al
callq 0x3360
cmpl $-0x1, %eax
jne 0x47a0
movq -0x268(%rbp), %rdi
addq $0x8, %rdi
callq 0x4b30
jmp 0x4727
leaq -0x1a0(%rbp), %rdi
leaq 0x8a5c(%rip), %rsi      # 0xd191
callq 0x3270
movq %rax, -0x278(%rbp)
jmp 0x4743
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq -0x278(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
jmp 0x4760
leaq -0x220(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x4775
leaq -0x220(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1fc(%rbp)
jmp 0x4907
movq -0x268(%rbp), %rax
movl (%rax), %edi
movq -0x20(%rbp), %rsi
movslq -0x24(%rbp), %rdx
callq 0x3370
movq %rax, -0x280(%rbp)
jmp 0x47bf
movq -0x268(%rbp), %rdi
movq -0x280(%rbp), %rax
movl %eax, -0x1b4(%rbp)
addq $0x8, %rdi
callq 0x4b30
jmp 0x47de
movl -0x1b4(%rbp), %eax
cmpl -0x24(%rbp), %eax
je 0x48f5
cmpl $-0x1, -0x1b4(%rbp)
jne 0x486f
leaq -0x1a0(%rbp), %rdi
leaq 0x898d(%rip), %rsi      # 0xd191
callq 0x3270
movq %rax, -0x288(%rbp)
jmp 0x4812
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq -0x288(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
jmp 0x482f
leaq -0x240(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x4844
leaq -0x240(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1fc(%rbp)
jmp 0x4907
leaq -0x1a0(%rbp), %rdi
leaq 0x891e(%rip), %rsi      # 0xd19b
callq 0x3270
movq %rax, -0x290(%rbp)
jmp 0x488b
movq -0x290(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x3400
movq %rax, -0x298(%rbp)
jmp 0x48a3
movq -0x298(%rbp), %rdi
leaq 0x88ff(%rip), %rsi      # 0xd1b0
callq 0x3270
jmp 0x48b8
leaq -0x260(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x48cd
leaq -0x260(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1fc(%rbp)
jmp 0x4907
movq $0x0, -0x8(%rbp)
movl $0x1, -0x1fc(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x31d0
movq -0x8(%rbp), %rax
addq $0x2a0, %rsp            # imm = 0x2A0
popq %rbp
retq
movq -0x1d0(%rbp), %rdi
callq 0x3420
nopl (%rax)