// File: /workspace/github_cpp_makefile_star2_O1/rachit31[P]CubeSatADCS/sendUdp.cpp
// Function: SendUdp::initialize(char const*, char const*, unsigned short)
const char* SendUdp::initialize(const char* localIp, const char* remoteIp, const uint16_t sendPort)
{
  std::stringstream error;

  // Create a datagram socket (TLPI pg 1153)
  m_socketFd = socket(AF_INET, SOCK_DGRAM, 0);
  if (m_socketFd == -1)
  {
    error << "socket(): " << std::strerror(errno);
    return error.str().c_str();
  }

  // IPv4 soccket address structure (TLPI pg 1202)
  struct in_addr in_addr;

  // Convert Presentation IP to Network (TLPI pg 1206)
  int retVal = inet_pton(AF_INET, localIp, &in_addr);
  if (retVal == -1)
  {
    error << "inet_pton(): " << std::strerror(errno);
    return error.str().c_str();
  }
  else if (retVal == 0)
  {
    error << "inet_pton(): " << localIp << " is not a valid network address";
    return error.str().c_str();;
  }

  // Setup sockaddr_in structure (TLPI pg 1202)
  memset(&m_sockaddr_in, 0, sizeof(struct sockaddr_in));
  m_sockaddr_in.sin_family = AF_INET;
  m_sockaddr_in.sin_addr = in_addr;
  m_sockaddr_in.sin_port = htons(sendPort);

  // Bind Socket to Address described by sockaddr_in (TLPI pg 1153)
  if (bind(m_socketFd, (struct sockaddr *) &m_sockaddr_in, sizeof(struct sockaddr_in)) == -1)
  {
    error << "bind(): " << std::strerror(errno);
    return error.str().c_str();
  }

  // Convert Presentation IP to Network (TLPI pg 1206)
  retVal = inet_pton(AF_INET, remoteIp, &in_addr);
  if (retVal == -1)
  {
    error << "inet_pton(): " << std::strerror(errno);
    return error.str().c_str();
  }
  else if (retVal == 0)
  {
    error << "inet_pton(): " << remoteIp << " is not a valid network address";
    return error.str().c_str();;
  }

  // Setup sockaddr_in structure (TLPI pg 1202)
  memset(&m_sockaddr_in, 0, sizeof(struct sockaddr_in));
  m_sockaddr_in.sin_family = AF_INET;
  m_sockaddr_in.sin_addr = in_addr;
  m_sockaddr_in.sin_port = htons(sendPort);

  m_len = sizeof(struct sockaddr_in);

  return error.str().c_str();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1b0, %rsp            # imm = 0x1B0
movl %ecx, %r14d
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r15
leaq -0x1d0(%rbp), %rdi
callq 0x2180
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x20a0
movl %eax, (%r15)
cmpl $-0x1, %eax
je 0x3e59
leaq -0x24(%rbp), %rdx
movl $0x2, %edi
movq %r12, %rsi
callq 0x23f0
testl %eax, %eax
je 0x3ea7
cmpl $-0x1, %eax
jne 0x3ee6
leaq -0x1c0(%rbp), %rbx
leaq 0x4432(%rip), %rsi      # 0x824b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x2290
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x4016
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x4036
leaq -0x1c0(%rbp), %rbx
leaq 0x43d9(%rip), %rsi      # 0x8240
movl $0xa, %edx
movq %rbx, %rdi
callq 0x2290
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x3f93
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x3fb3
leaq -0x1c0(%rbp), %rbx
leaq 0x4396(%rip), %rsi      # 0x824b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x2290
testq %r12, %r12
je 0x404b
movq %r12, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x4069
leaq 0x4(%r15), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%r15)
movw $0x2, 0x4(%r15)
movl -0x24(%rbp), %eax
movl %eax, 0x8(%r15)
rolw $0x8, %r14w
movw %r14w, 0x6(%r15)
movl (%r15), %edi
movq %r12, %rsi
movl $0x10, %edx
callq 0x2070
cmpl $-0x1, %eax
je 0x3fc8
leaq -0x24(%rbp), %rdx
movl $0x2, %edi
movq %rbx, %rsi
callq 0x23f0
testl %eax, %eax
je 0x4092
cmpl $-0x1, %eax
jne 0x40d1
leaq -0x1c0(%rbp), %rbx
leaq 0x42f8(%rip), %rsi      # 0x824b
movl $0xd, %edx
movq %rbx, %rdi
callq 0x2290
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x413b
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x415b
movq -0x1c0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1a0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2370
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x41af
leaq -0x1c0(%rbp), %rbx
leaq 0x42a3(%rip), %rsi      # 0x8279
movl $0x8, %edx
movq %rbx, %rdi
callq 0x2290
callq 0x2050
movl (%rax), %edi
callq 0x20d0
testq %rax, %rax
je 0x4109
movq %rax, %r14
movq %rax, %rdi
callq 0x2100
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x4129
movq -0x1c0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1a0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2370
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x41af
movq -0x1c0(%rbp), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl -0x1a0(%rbp,%rax), %esi
orl $0x1, %esi
callq 0x2370
leaq 0x41e9(%rip), %rsi      # 0x8259
movl $0x1f, %edx
movq %rbx, %rdi
callq 0x2290
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x41af
leaq -0x1c0(%rbp), %r14
leaq 0x41ab(%rip), %rsi      # 0x824b
movl $0xd, %edx
movq %r14, %rdi
callq 0x2290
testq %rbx, %rbx
je 0x416d
movq %rbx, %rdi
callq 0x2100
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2290
jmp 0x418b
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
movw $0x2, 0x4(%r15)
movl -0x24(%rbp), %eax
movl %eax, 0x8(%r15)
movw %r14w, 0x6(%r15)
movl $0x10, 0x14(%r15)
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x41af
movq -0x1c0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1a0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2370
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x41af
movq -0x1c0(%rbp), %rax
movq -0x18(%rax), %rax
addq %rax, %rbx
movl -0x1a0(%rbp,%rax), %esi
orl $0x1, %esi
movq %rbx, %rdi
callq 0x2370
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
jmp 0x41af
movq -0x1c0(%rbp), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl -0x1a0(%rbp,%rax), %esi
orl $0x1, %esi
callq 0x2370
leaq 0x40c7(%rip), %rsi      # 0x8259
movl $0x1f, %edx
movq %r14, %rdi
callq 0x2290
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x2330
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rbx
cmpq %rax, %rbx
je 0x41cb
movq -0x38(%rbp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x2270
movq 0x6de6(%rip), %rsi      # 0xafb8
leaq -0x1d0(%rbp), %rdi
callq 0x21c0
leaq -0x150(%rbp), %rdi
callq 0x20c0
movq %rbx, %rax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x420d
jmp 0x420d
jmp 0x420d
jmp 0x420d
jmp 0x420d
jmp 0x420d
jmp 0x420d
jmp 0x420d
movq %rax, %rbx
movq 0x6da1(%rip), %rsi      # 0xafb8
leaq -0x1d0(%rbp), %rdi
callq 0x21c0
leaq -0x150(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x23b0
nop