// File: /workspace/github_cpp_makefile_star2_O1/NovaGL[P]HarmonyHubControl/HarmonyHubControl.cpp
// Function: swapAuthorizationToken(csocket*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
int swapAuthorizationToken(csocket* authorizationcsocket, std::string& strAuthorizationToken)
{
    if(authorizationcsocket == NULL || strAuthorizationToken.length() == 0)
    {
        errorString = "swapAuthorizationToken : NULL csocket or empty authorization token provided";
        return 1;
    }

    if(startCommunication(authorizationcsocket, "guest", "gatorade.") != 0)
    {
        errorString = "swapAuthorizationToken : Communication failure";
        return 1;
    }

    std::string strData;
    std::string sendData;
    
    // GENERATE A LOGIN ID REQUEST USING THE HARMONY ID AND LOGIN AUTHORIZATION TOKEN 
    sendData = "<iq type=\"get\" id=\"";
    sendData.append(CONNECTION_ID);
    sendData.append("\"><oa xmlns=\"connect.logitech.com\" mime=\"vnd.logitech.connect/vnd.logitech.pair\">token=");
    sendData.append(strAuthorizationToken.c_str());
    sendData.append(":name=foo#iOS6.0.1#iPhone</oa></iq>");

    std::string strIdentityTokenTag = "identity=";
    int pos = std::string::npos;
    
    authorizationcsocket->write(sendData.c_str(), static_cast<unsigned int>(sendData.length()));
    
    memset(databuffer, 0, 1000000);
    authorizationcsocket->read(databuffer, 1000000, false);
        
    strData = databuffer; /* <- Expect: <iq/> ... <success xmlns= ... identity=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:status=succeeded ... */

    if(strData.find("<iq/>") != 0)
    {
         errorString = "swapAuthorizationToken : Invalid Harmony response";
         return 1;  
    }

    bool bIsDataReadable = false;
    authorizationcsocket->canRead(&bIsDataReadable, 0.3f);
    if(!bIsDataReadable && strData == "<iq/>")
    {
        bIsDataReadable = true;
    }

    while(bIsDataReadable)
    {
        memset(databuffer, 0, 1000000);
        authorizationcsocket->read(databuffer, 1000000, false);
        strData.append(databuffer);
        authorizationcsocket->canRead(&bIsDataReadable, 0.3f);
    };

    // Parse the session authorization token from the response
    pos = (int)strData.find(strIdentityTokenTag);
    if(pos == std::string::npos)
    {
        errorString = "swapAuthorizationToken : Logitech Harmony response does not contain a session authorization token";
        return 1;  
    }
    
    strAuthorizationToken = strData.substr(pos + strIdentityTokenTag.length());

    pos = (int)strAuthorizationToken.find(":status=succeeded");
    if(pos == std::string::npos)
    {
        errorString = "swapAuthorizationToken : Logitech Harmony response does not contain a valid session authorization token";
        return 1;  
    }
    strAuthorizationToken = strAuthorizationToken.substr(0, pos);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
testq %rdi, %rdi
je 0x47db
movq %rsi, %rbx
cmpq $0x0, 0x8(%rsi)
je 0x47db
movq %rdi, %r14
leaq 0xb8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x7db4(%rip), %rsi      # 0xc4de
leaq 0x7db2(%rip), %rdx      # 0xc4e3
leaq 0xa8(%rsp), %rdi
callq 0x8846
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x7d92(%rip), %rsi      # 0xc4e4
leaq 0x7d94(%rip), %rdx      # 0xc4ed
leaq 0x88(%rsp), %rdi
callq 0x8846
leaq 0xa8(%rsp), %rsi
leaq 0x88(%rsp), %rdx
movq %r14, %rdi
callq 0x4332
movl %eax, %ebp
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x479d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0x47ba
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x3290
testl %ebp, %ebp
je 0x4816
movq 0xfafb(%rip), %rdx      # 0x142c0
leaq 0xfaec(%rip), %rdi      # 0x142b8
leaq 0x7d1b(%rip), %rcx      # 0xc4ee
movl $0x2e, %r8d
jmp 0x47f6
movq 0xfade(%rip), %rdx      # 0x142c0
leaq 0xfacf(%rip), %rdi      # 0x142b8
leaq 0x7ca2(%rip), %rcx      # 0xc492
movl $0x4b, %r8d
xorl %esi, %esi
callq 0x3420
movl $0x1, %ebx
movl %ebx, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18(%rsp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq 0x38(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq %rax, -0x8(%rbp)
movb %al, (%rbp)
leaq 0x7cdf(%rip), %rcx      # 0xc51d
leaq 0x28(%rsp), %rdi
movl $0x13, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3420
leaq 0x7cd8(%rip), %rsi      # 0xc531
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x7cee(%rip), %rsi      # 0xc558
leaq 0x28(%rsp), %rdi
callq 0x34f0
movq (%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x7d28(%rip), %rsi      # 0xc5b0
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x7d32(%rip), %rsi      # 0xc5d4
leaq 0x7d34(%rip), %rdx      # 0xc5dd
leaq 0x48(%rsp), %rdi
callq 0x8846
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %edx
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0xfa74(%rip), %r15      # 0x14340
movl $0xf4240, %edx          # imm = 0xF4240
movq %r15, %rdi
xorl %esi, %esi
callq 0x3190
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
leaq 0x8(%rsp), %r15
movq 0x8(%r15), %r12
leaq 0xfa43(%rip), %r13      # 0x14340
movq %r13, %rdi
callq 0x3150
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r13, %rcx
movq %rax, %r8
callq 0x3420
leaq 0x7cbf(%rip), %rsi      # 0xc5de
leaq 0x8(%rsp), %rdi
movl $0x5, %ecx
xorl %edx, %edx
callq 0x3490
testq %rax, %rax
je 0x4961
movl $0x1, %ebx
movq 0xf97f(%rip), %rdx      # 0x142c0
leaq 0xf970(%rip), %rdi      # 0x142b8
leaq 0x7c95(%rip), %rcx      # 0xc5e4
movl $0x31, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x4b08
leaq 0x7(%rsp), %rsi
movb $0x0, (%rsi)
movss 0x7693(%rip), %xmm0     # 0xc004
movq %r14, %rdi
callq 0xbce0
cmpb $0x1, 0x7(%rsp)
je 0x499a
leaq 0x7c57(%rip), %rsi      # 0xc5de
leaq 0x8(%rsp), %rdi
callq 0x30f0
testl %eax, %eax
jne 0x499a
movb $0x1, 0x7(%rsp)
cmpb $0x1, 0x7(%rsp)
jne 0x49f1
leaq 0xf998(%rip), %r15      # 0x14340
movl $0xf4240, %edx          # imm = 0xF4240
movq %r15, %rdi
xorl %esi, %esi
callq 0x3190
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq *(%rax)
leaq 0xf970(%rip), %rsi      # 0x14340
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x7(%rsp), %rsi
movss 0x761d(%rip), %xmm0     # 0xc004
movq %r14, %rdi
callq 0xbce0
jmp 0x499a
movabsq $-0x100000000, %r15     # imm = 0xFFFFFFFF00000000
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rcx
leaq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x3490
shlq $0x20, %rax
cmpq %r15, %rax
jne 0x4a46
movl $0x1, %ebx
movq 0xf89a(%rip), %rdx      # 0x142c0
leaq 0xf88b(%rip), %rdi      # 0x142b8
leaq 0x7be2(%rip), %rcx      # 0xc616
movl $0x61, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x4b08
sarq $0x20, %rax
addq 0x50(%rsp), %rax
leaq 0x68(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movq %rax, %rdx
movq $-0x1, %rcx
callq 0x32c0
leaq 0x68(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3330
movq (%r14), %rdi
leaq 0x78(%rsp), %r12
cmpq %r12, %rdi
je 0x4a92
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x7bdf(%rip), %rsi      # 0xc678
movl $0x11, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3490
shlq $0x20, %rax
cmpq %r15, %rax
jne 0x4acb
movl $0x1, %ebx
leaq 0xf7fb(%rip), %rdi      # 0x142b8
leaq 0x7bc6(%rip), %rsi      # 0xc68a
callq 0x3370
jmp 0x4b08
sarq $0x20, %rax
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x32c0
leaq 0x68(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3330
movq (%r14), %rdi
cmpq %r12, %rdi
je 0x4b06
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3290
xorl %ebx, %ebx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
leaq 0x18(%rsp), %r14
je 0x4b29
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4b40
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4802
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x4802
jmp 0x4bab
jmp 0x4bab
jmp 0x4bab
jmp 0x4ba6
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x4b8d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x4b8d
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0x4bfd
movq 0xb8(%rsp), %rsi
jmp 0x4bf5
jmp 0x4bab
movq %rax, %rbx
jmp 0x4bca
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x4bca
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4be1
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x4bfd
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0