// File: /workspace/github_cpp_makefile_star2_O3/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 0x4670
movq %rsi, %r14
cmpq $0x0, 0x8(%rsi)
je 0x4670
movq %rdx, %rbx
movq %rdi, %r15
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x7ccc(%rip), %rsi      # 0xc0e3
leaq 0x7ccf(%rip), %rdx      # 0xc0ed
leaq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x8dc8
movq (%r15), %rsi
movq %r12, %rdi
callq 0x34f0
leaq 0x7cb1(%rip), %rsi      # 0xc0ee
leaq 0x28(%rsp), %rdi
callq 0x34f0
movq (%r14), %rsi
leaq 0x28(%rsp), %rdi
callq 0x34f0
leaq 0x7ca2(%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 0xb7c4
leaq 0x7c6f(%rip), %rsi      # 0xc100
leaq 0xa8(%rsp), %rdi
movl $0x50, %edx
callq 0xb890
leaq 0xa8(%rsp), %rdi
callq 0xbc44
cmpl $0x1, %eax
jne 0x469c
movq 0x30(%rsp), %rdx
leaq 0x7c8c(%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 0x7c63(%rip), %rcx      # 0xc154
leaq 0x8(%rsp), %rdi
movl $0x5, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3420
leaq 0x7c4e(%rip), %rsi      # 0xc15a
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x7c95(%rip), %rsi      # 0xc1b2
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x7bd2(%rip), %rsi      # 0xc100
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x7c85(%rip), %rsi      # 0xc1c4
leaq 0x8(%rsp), %rdi
callq 0x34f0
leaq 0x8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x34f0
leaq 0x7c90(%rip), %rsi      # 0xc1f2
leaq 0x8(%rsp), %rdi
callq 0x34f0
movq 0x8(%rsp), %rsi
movl 0x10(%rsp), %edx
leaq 0xa8(%rsp), %rdi
callq 0xbbe8
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %edx
leaq 0xa8(%rsp), %rdi
callq 0xbbe8
leaq 0xfda1(%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 0xbb50
leaq 0x88(%rsp), %r14
movq 0x8(%r14), %r15
leaq 0xfd68(%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 0x7c22(%rip), %rsi      # 0xc225
leaq 0x7c2b(%rip), %rdx      # 0xc235
leaq 0x48(%rsp), %rdi
callq 0x8dc8
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 0x46c8
movl $0x1, %ebx
movq 0xfc70(%rip), %rdx      # 0x142c0
leaq 0xfc61(%rip), %rdi      # 0x142b8
leaq 0x7bd8(%rip), %rcx      # 0xc236
movl $0x63, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x478a
movq 0xfc49(%rip), %rdx      # 0x142c0
leaq 0xfc3a(%rip), %rdi      # 0x142b8
leaq 0x7a24(%rip), %rcx      # 0xc0a9
movl $0x39, %r8d
xorl %esi, %esi
callq 0x3420
movl $0x1, %ebx
jmp 0x47fe
movl $0x1, %ebx
movq 0xfc18(%rip), %rdx      # 0x142c0
leaq 0xfc09(%rip), %rdi      # 0x142b8
leaq 0x7a5d(%rip), %rcx      # 0xc113
movl $0x3d, %r8d
xorl %esi, %esi
callq 0x3420
jmp 0x47bd
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 0x4717
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x7b7c(%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 0x4764
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 0x8e5d
movq %rax, %rcx
subq (%rbx), %rcx
movq %rcx, 0x8(%rbx)
movb $0x0, (%rax)
xorl %ebx, %ebx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x47a1
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x47b8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0x38(%rsp), %r13
leaq 0xa8(%rsp), %rdi
callq 0xb7ee
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x47e7
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x47fe
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 0x4818
jmp 0x4818
jmp 0x4818
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x4848
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3290
jmp 0x4848
jmp 0x4845
movq %rax, %rbx
jmp 0x486c
movq %rax, %rbx
jmp 0x485f
movq %rax, %rbx
jmp 0x4889
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x485f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3290
leaq 0xa8(%rsp), %rdi
callq 0xb7ee
movq 0x88(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4889
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x3290
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x48a5
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0