// File: /workspace/github_cpp_makefile_star2_O1/NovaGL[P]HarmonyHubControl/HarmonyHubControl.cpp
// Function: harmonyWebServiceLogin(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, 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 harmonyWebServiceLogin(std::string strUserEmail, std::string strPassword, std::string& strAuthorizationToken )
{   
    if(strUserEmail.length() == 0 || strPassword.length() == 0)
    {
        errorString = "harmonyWebServiceLogin : Empty email or password provided";
        return 1;
    } 


    // Build JSON request
    std::string strJSONText = "{\"email\":\"";
    strJSONText.append(strUserEmail.c_str());
    strJSONText.append("\",\"password\":\"");
    strJSONText.append(strPassword.c_str());
    strJSONText.append("\"}");

    std::string strHttpPayloadText;

    csocket authcsocket;
    authcsocket.connect("svcs.myharmony.com", 80);

    if (authcsocket.getState() != csocket::CONNECTED)
    {
        errorString = "harmonyWebServiceLogin : Unable to connect to Logitech server";
        return 1;
    }

    char contentLength[32];
    sprintf( contentLength, "%d", strJSONText.length() );

    std::string strHttpRequestText;

    strHttpRequestText = "POST ";
    strHttpRequestText.append(LOGITECH_AUTH_URL);
    strHttpRequestText.append(" HTTP/1.1\r\nHost: ");
    strHttpRequestText.append(LOGITECH_AUTH_HOSTNAME);
    strHttpRequestText.append("\r\nAccept-Encoding: identity\r\nContent-Length: ");
    strHttpRequestText.append(contentLength);
    strHttpRequestText.append("\r\ncontent-type: application/json;charset=utf-8\r\n\r\n");

    authcsocket.write(strHttpRequestText.c_str(), strHttpRequestText.size());
    authcsocket.write(strJSONText.c_str(), strJSONText.length());

    memset(databuffer, 0, 1000000);
    authcsocket.read(databuffer, 1000000, false);
    strHttpPayloadText = databuffer;/* <- Expect: 0x00def280 "HTTP/1.1 200 OK Server: nginx/1.2.4 Date: Wed, 05 Feb 2014 17:52:13 GMT Content-Type: application/json; charset=utf-8 Content-Length: 127 Connection: keep-alive Cache-Control: private X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET  {"GetUserAuthTokenResult":{"AccountId":0,"UserAuthToken":"KsRE6VVA3xrhtbqFbh0jWn8YTiweDeB\/b94Qeqf3ofWGM79zLSr62XQh8geJxw\/V"}}"*/

    // Parse the login authorization token from the response
    std::string strAuthTokenTag = "UserAuthToken\":\"";
    int pos = (int)strHttpPayloadText.find(strAuthTokenTag);
    if(pos == std::string::npos)
    {
        errorString = "harmonyWebServiceLogin : Logitech web service response does not contain a login authorization token";
        return 1;  
    }

    strAuthorizationToken = strHttpPayloadText.substr(pos + strAuthTokenTag.length());
    pos = (int)strAuthorizationToken.find("\"}}");
    strAuthorizationToken = strAuthorizationToken.substr(0, pos);

    // Remove forward slashes
    strAuthorizationToken.erase(std::remove(strAuthorizationToken.begin(), strAuthorizationToken.end(), '\\'), strAuthorizationToken.end());
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
cmpq $0x0, 0x8(%rdi)
je 0x407a
movq %rsi, %r14
cmpq $0x0, 0x8(%rsi)
je 0x407a
movq %rdx, %rbx
movq %rdi, %r15
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x82c2(%rip), %rsi      # 0xc0e3
leaq 0x82c5(%rip), %rdx      # 0xc0ed
leaq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x8846
movq (%r15), %rsi
movq %r12, %rdi
callq 0x34f0
leaq 0x82a7(%rip), %rsi      # 0xc0ee
leaq 0x28(%rsp), %rdi
callq 0x34f0
movq (%r14), %rsi
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x8298(%rip), %rsi      # 0xc0fd
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x98(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq $0x0, -0x8(%rbp)
movb $0x0, (%rbp)
leaq 0xa8(%rsp), %rdi
callq 0xba44
leaq 0x8265(%rip), %rsi      # 0xc100
leaq 0xa8(%rsp), %rdi
movl $0x50, %edx
callq 0xbb10
leaq 0xa8(%rsp), %rdi
callq 0xbec8
cmpl $0x1, %eax
jne 0x40a6
movq 0x30(%rsp), %rdx
leaq 0x8282(%rip), %rsi      # 0xc151
leaq 0x100(%rsp), %rdi
xorl %eax, %eax
callq 0x3090
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
leaq 0x8259(%rip), %rcx      # 0xc154
leaq 0x8(%rsp), %rdi
movl $0x5, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3420
leaq 0x8244(%rip), %rsi      # 0xc15a
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x828b(%rip), %rsi      # 0xc1b2
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x81c8(%rip), %rsi      # 0xc100
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x827b(%rip), %rsi      # 0xc1c4
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x34f0
leaq 0x8286(%rip), %rsi      # 0xc1f2
leaq 0x8(%rsp), %rdi
callq 0x34f0
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
leaq 0xa8(%rsp), %rdi
callq 0xbe6c
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %edx
leaq 0xa8(%rsp), %rdi
callq 0xbe6c
leaq 0x10397(%rip), %r14     # 0x14340
movl $0xf4240, %edx          # imm = 0xF4240
movq %r14, %rdi
xorl %esi, %esi
callq 0x3190
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
movl $0xf4240, %edx          # imm = 0xF4240
xorl %ecx, %ecx
callq 0xbdd4
leaq 0x88(%rsp), %r14
movq 0x8(%r14), %r15
leaq 0x1035e(%rip), %r12     # 0x14340
movq %r12, %rdi
callq 0x3150
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x3420
leaq 0x58(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x8218(%rip), %rsi      # 0xc225
leaq 0x8221(%rip), %rdx      # 0xc235
leaq 0x48(%rsp), %rdi
callq 0x8846
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rcx
leaq 0x88(%rsp), %rdi
xorl %edx, %edx
callq 0x3490
shlq $0x20, %rax
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
cmpq %rcx, %rax
jne 0x40d2
movl $0x1, %ebx
movq 0x10266(%rip), %rdx     # 0x142c0
leaq 0x10257(%rip), %rdi     # 0x142b8
leaq 0x81ce(%rip), %rcx      # 0xc236
movl $0x63, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x4194
movq 0x1023f(%rip), %rdx     # 0x142c0
leaq 0x10230(%rip), %rdi     # 0x142b8
leaq 0x801a(%rip), %rcx      # 0xc0a9
movl $0x39, %r8d
xorl %esi, %esi
callq 0x3420
movl $0x1, %ebx
jmp 0x4208
movl $0x1, %ebx
movq 0x1020e(%rip), %rdx     # 0x142c0
leaq 0x101ff(%rip), %rdi     # 0x142b8
leaq 0x8053(%rip), %rcx      # 0xc113
movl $0x3d, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x41c7
sarq $0x20, %rax
addq 0x50(%rsp), %rax
leaq 0x68(%rsp), %rdi
leaq 0x88(%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 0x4121
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x8172(%rip), %rsi      # 0xc29a
movl $0x3, %ecx
movq %rbx, %rdi
xorl %edx, %edx
callq 0x3490
movslq %eax, %rcx
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x32c0
leaq 0x68(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3330
movq (%r14), %rdi
cmpq %r12, %rdi
je 0x416e
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3290
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
addq %rdi, %rsi
leaq 0x68(%rsp), %rdx
movb $0x5c, (%rdx)
callq 0x88d6
movq %rax, %rcx
subq (%rbx), %rcx
movq %rcx, 0x8(%rbx)
movb $0x0, (%rax)
xorl %ebx, %ebx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x41ab
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x41c2
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x38(%rsp), %r13
leaq 0xa8(%rsp), %rdi
callq 0xba6e
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x41f1
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x4208
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movl %ebx, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4222
jmp 0x4222
jmp 0x4222
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x4252
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x4252
jmp 0x424f
movq %rax, %rbx
jmp 0x4276
movq %rax, %rbx
jmp 0x4269
movq %rax, %rbx
jmp 0x4293
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x4269
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0xa8(%rsp), %rdi
callq 0xba6e
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4293
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x42af
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0