// File: NovaGL[P]HarmonyHubControl/HarmonyHubControl.cpp
// Function: startCommunication(csocket*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int startCommunication(csocket* communicationcsocket, std::string strUserName, std::string strPassword)
{
    if(communicationcsocket == NULL || strUserName.length() == 0 || strPassword.length() == 0)
    {
        errorString = "startCommunication : Invalid communication parameter(s) provided";
        return 1;
    } 

    // Start communication
    std::string data = "<stream:stream to='connect.logitech.com' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0'>";
     communicationcsocket->write(data.c_str(), static_cast<unsigned int>(data.length()));
     memset(databuffer, 0, 1000000);
    memset(databuffer, 0, 1000000);
    communicationcsocket->read(databuffer, 1000000, false);
    
    std::string strData = databuffer;/* <- Expect: <?xml version='1.0' encoding='iso-8859-1'?><stream:stream from='' id='XXXXXXXX' version='1.0' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'><stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism></mechanisms></stream:features> */

    data = "<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" mechanism=\"PLAIN\">";
    std::string tmp = "\0";
    tmp.append(strUserName);
    tmp.append("\0");
    tmp.append(strPassword);
    data.append(base64_encode(tmp.c_str(), static_cast<unsigned int>(tmp.length())));
    data.append("</auth>");
    communicationcsocket->write(data.c_str(), static_cast<unsigned int>(data.length())); 
   
    memset(databuffer, 0, 1000000);
    communicationcsocket->read(databuffer, 1000000, false);
    
    strData = databuffer; /* <- Expect: <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/> */
    if(strData != "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>")
    {
        errorString = "startCommunication : connection error";
        return 1;
    } 

    data = "<stream:stream to='connect.logitech.com' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0'>";
    communicationcsocket->write(data.c_str(), static_cast<unsigned int>(data.length()));
    
    memset(databuffer, 0, 1000000);
    communicationcsocket->read(databuffer, 1000000, false);

    strData = databuffer; /* <- Expect: <?xml version='1.0' encoding='iso-8859-1'?><stream:stream from='' id='057a30bd' version='1.0' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'><stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism></mechanisms></stream:features> */

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rsi, -0xe0(%rbp)
movq %rdx, -0xd8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x45b0
movq -0xe0(%rbp), %rdi
callq 0x3540
cmpq $0x0, %rax
je 0x45b0
movq -0xd8(%rbp), %rdi
callq 0x3540
cmpq $0x0, %rax
jne 0x45cf
leaq 0x1ddc1(%rip), %rdi     # 0x22378
leaq 0x11d9b(%rip), %rsi     # 0x16359
callq 0x34c0
movl $0x1, -0x4(%rbp)
jmp 0x4a61
leaq -0x41(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x35f0
movq -0xe8(%rbp), %rdx
leaq 0x11dad(%rip), %rsi     # 0x1639a
leaq -0x40(%rbp), %rdi
callq 0x8b50
jmp 0x45f8
leaq -0x41(%rbp), %rdi
callq 0x33f0
movq -0x10(%rbp), %rax
movq %rax, -0xf8(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x31c0
movq -0x100(%rbp), %rdi
movq %rax, -0xf0(%rbp)
callq 0x3540
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rsi
movl %eax, %edx
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
jmp 0x464a
leaq 0x1ddaf(%rip), %rdi     # 0x22400
movq %rdi, -0x110(%rbp)
xorl %esi, %esi
movl %esi, -0x104(%rbp)
movl $0xf4240, %edx          # imm = 0xF4240
movq %rdx, -0x118(%rbp)
callq 0x3210
movq -0x118(%rbp), %rdx
movq -0x110(%rbp), %rdi
movl -0x104(%rbp), %esi
callq 0x3210
movq -0x110(%rbp), %rsi
movl -0x104(%rbp), %ecx
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movl $0xf4240, %edx          # imm = 0xF4240
callq *%rax
jmp 0x46aa
leaq -0x79(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x35f0
movq -0x120(%rbp), %rdx
leaq 0x1dd38(%rip), %rsi     # 0x22400
leaq -0x78(%rbp), %rdi
callq 0x8b50
jmp 0x46d3
leaq -0x79(%rbp), %rdi
callq 0x33f0
leaq 0x11d43(%rip), %rsi     # 0x16426
leaq -0x40(%rbp), %rdi
callq 0x34c0
jmp 0x46ee
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x35f0
movq -0x128(%rbp), %rdx
leaq 0x14db6(%rip), %rsi     # 0x194c5
leaq -0xa0(%rbp), %rdi
callq 0x8b50
jmp 0x471d
leaq -0xa1(%rbp), %rdi
callq 0x33f0
movq -0xe0(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3640
jmp 0x473e
leaq 0x14d80(%rip), %rsi     # 0x194c5
leaq -0xa0(%rbp), %rdi
callq 0x3630
jmp 0x4753
movq -0xd8(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3640
jmp 0x4768
leaq -0xa0(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x31c0
movq -0x138(%rbp), %rdi
movq %rax, -0x130(%rbp)
callq 0x3540
movq -0x130(%rbp), %rsi
movl %eax, %edx
leaq -0xc8(%rbp), %rdi
callq 0x3930
jmp 0x47a5
leaq -0x40(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x3640
jmp 0x47b7
leaq -0xc8(%rbp), %rdi
callq 0x3698
leaq 0x11c9e(%rip), %rsi     # 0x16468
leaq -0x40(%rbp), %rdi
callq 0x3630
jmp 0x47d5
movq -0x10(%rbp), %rax
movq %rax, -0x148(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x31c0
movq -0x150(%rbp), %rdi
movq %rax, -0x140(%rbp)
callq 0x3540
movq -0x148(%rbp), %rdi
movq -0x140(%rbp), %rsi
movl %eax, %edx
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
jmp 0x481e
leaq 0x1dbdb(%rip), %rdi     # 0x22400
movq %rdi, -0x160(%rbp)
xorl %esi, %esi
movl %esi, -0x154(%rbp)
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x3210
movq -0x160(%rbp), %rsi
movl -0x154(%rbp), %ecx
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movl $0xf4240, %edx          # imm = 0xF4240
callq *%rax
jmp 0x485e
leaq 0x1db9b(%rip), %rsi     # 0x22400
leaq -0x78(%rbp), %rdi
callq 0x34c0
jmp 0x4870
leaq 0x11bf9(%rip), %rsi     # 0x16470
leaq -0x78(%rbp), %rdi
callq 0x8c90
movb %al, -0x161(%rbp)
jmp 0x4888
movb -0x161(%rbp), %al
testb $0x1, %al
jne 0x4897
jmp 0x4963
leaq 0x1dada(%rip), %rdi     # 0x22378
leaq 0x11bff(%rip), %rsi     # 0x164a4
callq 0x34c0
jmp 0x48ac
movl $0x1, -0x4(%rbp)
movl $0x1, -0xcc(%rbp)
jmp 0x4a21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x41(%rbp), %rdi
callq 0x33f0
jmp 0x4a6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4a56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x79(%rbp), %rdi
callq 0x33f0
jmp 0x4a56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4a4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xa1(%rbp), %rdi
callq 0x33f0
jmp 0x4a4d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x4a41
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x3698
jmp 0x4a41
leaq 0x11a30(%rip), %rsi     # 0x1639a
leaq -0x40(%rbp), %rdi
callq 0x34c0
jmp 0x4975
movq -0x10(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0x180(%rbp)
callq 0x31c0
movq -0x180(%rbp), %rdi
movq %rax, -0x170(%rbp)
callq 0x3540
movq -0x178(%rbp), %rdi
movq -0x170(%rbp), %rsi
movl %eax, %edx
movq (%rdi), %rax
movq 0x8(%rax), %rax
callq *%rax
jmp 0x49be
leaq 0x1da3b(%rip), %rdi     # 0x22400
movq %rdi, -0x190(%rbp)
xorl %esi, %esi
movl %esi, -0x184(%rbp)
movl $0xf4240, %edx          # imm = 0xF4240
callq 0x3210
movq -0x190(%rbp), %rsi
movl -0x184(%rbp), %ecx
movq -0x10(%rbp), %rdi
movq (%rdi), %rax
movq (%rax), %rax
movl $0xf4240, %edx          # imm = 0xF4240
callq *%rax
jmp 0x49fe
leaq 0x1d9fb(%rip), %rsi     # 0x22400
leaq -0x78(%rbp), %rdi
callq 0x34c0
jmp 0x4a10
movl $0x0, -0x4(%rbp)
movl $0x1, -0xcc(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x3698
leaq -0x78(%rbp), %rdi
callq 0x3698
leaq -0x40(%rbp), %rdi
callq 0x3698
jmp 0x4a61
leaq -0xa0(%rbp), %rdi
callq 0x3698
leaq -0x78(%rbp), %rdi
callq 0x3698
leaq -0x40(%rbp), %rdi
callq 0x3698
jmp 0x4a6d
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x35e0
nopw %cs:(%rax,%rax)