// File: /workspace/github_cpp_makefile_star2_O2/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 0x2170
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x2090
movl %eax, (%r15)
cmpl $-0x1, %eax
je 0x3744
pushq $0x2
popq %rdi
leaq -0x24(%rbp), %rdx
movq %r12, %rsi
callq 0x2390
testl %eax, %eax
je 0x3786
cmpl $-0x1, %eax
jne 0x37c8
leaq -0x1c0(%rbp), %rdi
leaq 0x3b3b(%rip), %rsi      # 0x724b
callq 0x2220
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2220
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x22e0
jmp 0x3909
leaq -0x1c0(%rbp), %rdi
leaq 0x3aee(%rip), %rsi      # 0x7240
callq 0x2220
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2220
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x22e0
jmp 0x3909
leaq -0x1c0(%rbp), %rdi
leaq 0x3ab7(%rip), %rsi      # 0x724b
callq 0x2220
movq %rax, %rdi
movq %r12, %rsi
callq 0x2220
leaq 0x3aae(%rip), %rsi      # 0x7259
movq %rax, %rdi
callq 0x2220
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x22e0
jmp 0x3909
andq $0x0, 0xc(%r15)
leaq 0x4(%r15), %rsi
movw $0x2, 0x4(%r15)
movl -0x24(%rbp), %eax
movl %eax, 0x8(%r15)
rolw $0x8, %r14w
movw %r14w, 0x6(%r15)
movl (%r15), %edi
pushq $0x10
popq %rdx
callq 0x2060
cmpl $-0x1, %eax
je 0x385b
pushq $0x2
popq %rdi
leaq -0x24(%rbp), %rdx
movq %rbx, %rsi
callq 0x2390
testl %eax, %eax
je 0x389a
cmpl $-0x1, %eax
jne 0x38d9
leaq -0x1c0(%rbp), %rdi
leaq 0x3a24(%rip), %rsi      # 0x724b
callq 0x2220
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2220
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x22e0
jmp 0x3909
leaq -0x1c0(%rbp), %rdi
leaq 0x3a10(%rip), %rsi      # 0x7279
callq 0x2220
movq %rax, %rbx
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2220
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x22e0
jmp 0x3909
leaq -0x1c0(%rbp), %rdi
leaq 0x39a3(%rip), %rsi      # 0x724b
callq 0x2220
movq %rax, %rdi
movq %rbx, %rsi
callq 0x2220
leaq 0x399a(%rip), %rsi      # 0x7259
movq %rax, %rdi
callq 0x2220
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x22e0
jmp 0x3909
andq $0x0, 0xc(%r15)
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 0x22e0
leaq -0x48(%rbp), %rdi
movq (%rdi), %rbx
callq 0x20f0
leaq -0x1d0(%rbp), %rdi
callq 0x21b0
movq %rbx, %rax
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3944
jmp 0x3944
jmp 0x3944
jmp 0x3944
jmp 0x3944
jmp 0x3944
jmp 0x3944
jmp 0x3944
movq %rax, %rbx
leaq -0x1d0(%rbp), %rdi
callq 0x21b0
movq %rbx, %rdi
callq 0x2350
nop