// File: /workspace/github_cpp_makefile_star2_O3/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 0x4dd1
movq %rsi, %rbx
cmpq $0x0, 0x8(%rsi)
je 0x4dd1
movq %rdi, %r14
leaq 0xb8(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x77be(%rip), %rsi      # 0xc4de
leaq 0x77bc(%rip), %rdx      # 0xc4e3
leaq 0xa8(%rsp), %rdi
callq 0x8dc8
leaq 0x98(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x779c(%rip), %rsi      # 0xc4e4
leaq 0x779e(%rip), %rdx      # 0xc4ed
leaq 0x88(%rsp), %rdi
callq 0x8dc8
leaq 0xa8(%rsp), %rsi
leaq 0x88(%rsp), %rdx
movq %r14, %rdi
callq 0x4928
movl %eax, %ebp
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x4d93
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0x4db0
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x3290
testl %ebp, %ebp
je 0x4e0c
movq 0xf505(%rip), %rdx      # 0x142c0
leaq 0xf4f6(%rip), %rdi      # 0x142b8
leaq 0x7725(%rip), %rcx      # 0xc4ee
movl $0x2e, %r8d
jmp 0x4dec
movq 0xf4e8(%rip), %rdx      # 0x142c0
leaq 0xf4d9(%rip), %rdi      # 0x142b8
leaq 0x76ac(%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 0x76e9(%rip), %rcx      # 0xc51d
leaq 0x28(%rsp), %rdi
movl $0x13, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3420
leaq 0x76e2(%rip), %rsi      # 0xc531
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x76f8(%rip), %rsi      # 0xc558
leaq 0x28(%rsp), %rdi
callq 0x34f0
movq (%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x7732(%rip), %rsi      # 0xc5b0
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x773c(%rip), %rsi      # 0xc5d4
leaq 0x773e(%rip), %rdx      # 0xc5dd
leaq 0x48(%rsp), %rdi
callq 0x8dc8
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %edx
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0xf47e(%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 0xf44d(%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 0x76c9(%rip), %rsi      # 0xc5de
leaq 0x8(%rsp), %rdi
movl $0x5, %ecx
xorl %edx, %edx
callq 0x3490
testq %rax, %rax
je 0x4f57
movl $0x1, %ebx
movq 0xf389(%rip), %rdx      # 0x142c0
leaq 0xf37a(%rip), %rdi      # 0x142b8
leaq 0x769f(%rip), %rcx      # 0xc5e4
movl $0x31, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x50fe
leaq 0x7(%rsp), %rsi
movb $0x0, (%rsi)
movss 0x709d(%rip), %xmm0     # 0xc004
movq %r14, %rdi
callq 0xba60
cmpb $0x1, 0x7(%rsp)
je 0x4f90
leaq 0x7661(%rip), %rsi      # 0xc5de
leaq 0x8(%rsp), %rdi
callq 0x30f0
testl %eax, %eax
jne 0x4f90
movb $0x1, 0x7(%rsp)
cmpb $0x1, 0x7(%rsp)
jne 0x4fe7
leaq 0xf3a2(%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 0xf37a(%rip), %rsi      # 0x14340
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x7(%rsp), %rsi
movss 0x7027(%rip), %xmm0     # 0xc004
movq %r14, %rdi
callq 0xba60
jmp 0x4f90
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 0x503c
movl $0x1, %ebx
movq 0xf2a4(%rip), %rdx      # 0x142c0
leaq 0xf295(%rip), %rdi      # 0x142b8
leaq 0x75ec(%rip), %rcx      # 0xc616
movl $0x61, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x50fe
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 0x5088
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x75e9(%rip), %rsi      # 0xc678
movl $0x11, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3490
shlq $0x20, %rax
cmpq %r15, %rax
jne 0x50c1
movl $0x1, %ebx
leaq 0xf205(%rip), %rdi      # 0x142b8
leaq 0x75d0(%rip), %rsi      # 0xc68a
callq 0x3370
jmp 0x50fe
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 0x50fc
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 0x511f
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x5136
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4df8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x4df8
jmp 0x51a1
jmp 0x51a1
jmp 0x51a1
jmp 0x519c
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r12, %rdi
je 0x5183
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x5183
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r15, %rdi
je 0x51f3
movq 0xb8(%rsp), %rsi
jmp 0x51eb
jmp 0x51a1
movq %rax, %rbx
jmp 0x51c0
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x51c0
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x51d7
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x51f3
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0