// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
testq %rdi, %rdi
je 0x3fff
movq %rsi, %r15
cmpq $0x0, 0x8(%rsi)
je 0x3fff
movq %rdx, %r14
cmpq $0x0, 0x8(%rdx)
je 0x3fff
movq %rdi, %rbx
leaq 0x74c3(%rip), %rsi      # 0xb362
movq %rsp, %r12
leaq 0x40(%rsp), %rdx
movq %r12, %rdi
callq 0x68b8
movq (%r12), %rsi
movl 0x8(%r12), %edx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
leaq 0xf478(%rip), %r12      # 0x13340
movl $0xf4240, %edx          # imm = 0xF4240
movq %r12, %rdi
xorl %esi, %esi
callq 0x31b0
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
leaq 0xf450(%rip), %rsi      # 0x13340
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
callq 0x68b8
leaq 0x74e8(%rip), %rsi      # 0xb3ee
movq %rsp, %rdi
callq 0x34c0
leaq 0xa2a7(%rip), %rsi      # 0xe1bc
leaq 0x20(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x68b8
leaq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x34d0
leaq 0xa284(%rip), %rsi      # 0xe1bc
leaq 0x20(%rsp), %rdi
callq 0x34b0
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x34d0
movq 0x20(%rsp), %rsi
movl 0x28(%rsp), %edx
leaq 0x60(%rsp), %rdi
callq 0x3715
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x34d0
leaq 0x60(%rsp), %rdi
callq 0x3518
leaq 0x74b0(%rip), %rsi      # 0xb430
movq %rsp, %rdi
callq 0x34b0
movq (%rsp), %rsi
movl 0x8(%rsp), %edx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
leaq 0xf3a0(%rip), %r14      # 0x13340
movl $0xf4240, %edx          # imm = 0xF4240
movq %r14, %rdi
xorl %esi, %esi
callq 0x31b0
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
leaq 0xf378(%rip), %rsi      # 0x13340
leaq 0x40(%rsp), %rdi
callq 0x34c0
leaq 0x745f(%rip), %rsi      # 0xb438
leaq 0x40(%rsp), %rdi
callq 0x68f3
testb %al, %al
je 0x4017
leaq 0xf2ca(%rip), %rdi      # 0x132b8
leaq 0x7477(%rip), %rsi      # 0xb46c
callq 0x34c0
pushq $0x1
popq %rbp
jmp 0x4072
leaq 0xf2b2(%rip), %rdi      # 0x132b8
leaq 0x7314(%rip), %rsi      # 0xb321
callq 0x34c0
pushq $0x1
popq %rbp
jmp 0x408e
leaq 0x7344(%rip), %rsi      # 0xb362
movq %rsp, %rdi
callq 0x34c0
movq (%rsp), %rsi
movl 0x8(%rsp), %edx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
leaq 0xf302(%rip), %r14      # 0x13340
xorl %ebp, %ebp
movl $0xf4240, %edx          # imm = 0xF4240
movq %r14, %rdi
xorl %esi, %esi
callq 0x31b0
movq (%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
leaq 0xf2d8(%rip), %rsi      # 0x13340
leaq 0x40(%rsp), %rdi
callq 0x34c0
leaq 0x20(%rsp), %rdi
callq 0x3518
leaq 0x40(%rsp), %rdi
callq 0x3518
movq %rsp, %rdi
callq 0x3518
movl %ebp, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3518
jmp 0x40c2
jmp 0x40bf
jmp 0x40b3
movq %rax, %rbx
jmp 0x40cc
jmp 0x40ba
movq %rax, %rbx
jmp 0x40d6
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3518
leaq 0x40(%rsp), %rdi
callq 0x3518
movq %rsp, %rdi
callq 0x3518
movq %rbx, %rdi
callq 0x3470