// File: /workspace/github_cpp_makefile_star2_O1/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 0x3722
movl (%r12), %edi
movzbl %r13b, %edx
movl $0x703, %esi            # imm = 0x703
xorl %eax, %eax
callq 0x22c0
cmpl $-0x1, %eax
je 0x3471
movl (%r12), %edi
leaq -0x58(%rbp), %rdx
movl $0x720, %esi            # imm = 0x720
xorl %eax, %eax
callq 0x22c0
cmpl $-0x1, %eax
je 0x34c7
movl (%r12), %edi
movslq %ebx, %rdx
movq %r15, %rsi
callq 0x22d0
movq %rax, %r15
movq %r14, %rdi
callq 0x3850
cmpl %ebx, %r15d
jne 0x351d
xorl %ebx, %ebx
jmp 0x3668
movq %r14, %rdi
callq 0x3850
leaq -0x1d0(%rbp), %rbx
leaq 0x4d02(%rip), %rsi      # 0x8189
movl $0x9, %edx
movq %rbx, %rdi
callq 0x2290
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x3575
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x3595
movq %r14, %rdi
callq 0x3850
leaq -0x1d0(%rbp), %rbx
leaq 0x4cac(%rip), %rsi      # 0x8189
movl $0x9, %edx
movq %rbx, %rdi
callq 0x2290
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x35aa
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x35ca
leaq -0x1d0(%rbp), %r14
cmpl $-0x1, %r15d
je 0x35dc
leaq 0x4c5e(%rip), %rsi      # 0x8193
movl $0x14, %edx
movq %r14, %rdi
callq 0x2290
movq %r14, %rdi
movl %ebx, %esi
callq 0x2390
leaq 0x4c55(%rip), %rsi      # 0x81a8
movl $0x6, %edx
movq %rax, %rdi
callq 0x2290
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x364c
movq -0x1d0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1b0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2370
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x364c
movq -0x1d0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1b0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2370
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x364c
leaq 0x4ba6(%rip), %rsi      # 0x8189
movl $0x9, %edx
movq %r14, %rdi
callq 0x2290
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x361c
movq %rax, %rbx
movq %rax, %rdi
callq 0x2100
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x363c
movq -0x1d0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %r14
movl -0x1b0(%rbp,%rax), %esi
orl $0x1, %esi
movq %r14, %rdi
callq 0x2370
leaq -0x1c8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rbx
cmpq %rax, %rbx
je 0x3668
movq -0x38(%rbp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x2270
movq 0x7949(%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 0x36a4
jmp 0x36a4
jmp 0x36a4
jmp 0x36a4
movq %rax, %rbx
movq 0x790a(%rip), %rsi      # 0xafb8
leaq -0x1e0(%rbp), %rdi
callq 0x21c0
leaq -0x160(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x23b0