// File: 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
subq $0x300, %rsp            # imm = 0x300
movw %cx, %ax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movw %ax, -0x22(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x2b8(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x3190
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x30b0
movl %eax, %ecx
movq -0x2b8(%rbp), %rax
movl %ecx, (%rax)
cmpl $-0x1, (%rax)
jne 0x52a3
leaq -0x1a0(%rbp), %rdi
leaq 0x803b(%rip), %rsi      # 0xd250
callq 0x3270
movq %rax, -0x2c0(%rbp)
jmp 0x5223
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq -0x2c0(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
jmp 0x5240
leaq -0x1e0(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x5255
leaq -0x1e0(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1e4(%rbp)
jmp 0x5651
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x31d0
jmp 0x566a
movq -0x18(%rbp), %rsi
movl $0x2, %edi
leaq -0x1e8(%rbp), %rdx
callq 0x3470
movl %eax, -0x1ec(%rbp)
cmpl $-0x1, -0x1ec(%rbp)
jne 0x5340
leaq -0x1a0(%rbp), %rdi
leaq 0x7f86(%rip), %rsi      # 0xd25b
callq 0x3270
movq %rax, -0x2c8(%rbp)
jmp 0x52e3
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq -0x2c8(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
jmp 0x5300
leaq -0x210(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x5315
leaq -0x210(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1e4(%rbp)
jmp 0x5651
cmpl $0x0, -0x1ec(%rbp)
jne 0x53d7
leaq -0x1a0(%rbp), %rdi
leaq 0x7f00(%rip), %rsi      # 0xd25b
callq 0x3270
movq %rax, -0x2d0(%rbp)
jmp 0x5369
movq -0x2d0(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3270
movq %rax, -0x2d8(%rbp)
jmp 0x5382
movq -0x2d8(%rbp), %rdi
leaq 0x7ed9(%rip), %rsi      # 0xd269
callq 0x3270
jmp 0x5397
leaq -0x230(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x53ac
leaq -0x230(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1e4(%rbp)
jmp 0x5651
jmp 0x53d9
movq -0x2b8(%rbp), %rdi
addq $0x4, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x3140
movq -0x2b8(%rbp), %rax
movw $0x2, 0x4(%rax)
movl -0x1e8(%rbp), %ecx
movl %ecx, 0x8(%rax)
movzwl -0x22(%rbp), %edi
callq 0x3050
movq -0x2b8(%rbp), %rsi
movw %ax, 0x6(%rsi)
movl (%rsi), %edi
addq $0x4, %rsi
movl $0x10, %edx
callq 0x3070
cmpl $-0x1, %eax
jne 0x54a8
leaq -0x1a0(%rbp), %rdi
leaq 0x7e4c(%rip), %rsi      # 0xd289
callq 0x3270
movq %rax, -0x2e0(%rbp)
jmp 0x544b
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq -0x2e0(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
jmp 0x5468
leaq -0x250(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x547d
leaq -0x250(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1e4(%rbp)
jmp 0x5651
movq -0x20(%rbp), %rsi
movl $0x2, %edi
leaq -0x1e8(%rbp), %rdx
callq 0x3470
movl %eax, -0x1ec(%rbp)
cmpl $-0x1, -0x1ec(%rbp)
jne 0x5545
leaq -0x1a0(%rbp), %rdi
leaq 0x7d81(%rip), %rsi      # 0xd25b
callq 0x3270
movq %rax, -0x2e8(%rbp)
jmp 0x54e8
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq -0x2e8(%rbp), %rdi
movq %rax, %rsi
callq 0x3270
jmp 0x5505
leaq -0x270(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x551a
leaq -0x270(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1e4(%rbp)
jmp 0x5651
cmpl $0x0, -0x1ec(%rbp)
jne 0x55d9
leaq -0x1a0(%rbp), %rdi
leaq 0x7cfb(%rip), %rsi      # 0xd25b
callq 0x3270
movq %rax, -0x2f0(%rbp)
jmp 0x556e
movq -0x2f0(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x3270
movq %rax, -0x2f8(%rbp)
jmp 0x5587
movq -0x2f8(%rbp), %rdi
leaq 0x7cd4(%rip), %rsi      # 0xd269
callq 0x3270
jmp 0x559c
leaq -0x290(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x55b1
leaq -0x290(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1e4(%rbp)
jmp 0x5651
jmp 0x55db
movq -0x2b8(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x4(%rax)
movw $0x2, 0x4(%rax)
movl -0x1e8(%rbp), %ecx
movl %ecx, 0x8(%rax)
movzwl -0x22(%rbp), %edi
callq 0x3050
movw %ax, %cx
movq -0x2b8(%rbp), %rax
movw %cx, 0x6(%rax)
movl $0x10, 0x14(%rax)
leaq -0x2b0(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x3260
jmp 0x562b
leaq -0x2b0(%rbp), %rdi
callq 0x3100
movq %rax, -0x8(%rbp)
leaq -0x2b0(%rbp), %rdi
callq 0x3160
movl $0x1, -0x1e4(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x31d0
movq -0x8(%rbp), %rax
addq $0x300, %rsp            # imm = 0x300
popq %rbp
retq
movq -0x1b8(%rbp), %rdi
callq 0x3420
nopw %cs:(%rax,%rax)