// File: errantlinguist[P]Forecast/src/main.cpp
// Function: readUrl(char const*)
static int readUrl(const char* pUrl)
{
	int result(EXIT_FAILURE);

	// Initialise cURL
	CURLcode initCode(curl_global_init(CURL_GLOBAL_DEFAULT));
	if (CURLE_OK == initCode)
	{
		try {
			std::unique_ptr<char[]> pErrorBuffer(new char[CURL_ERROR_SIZE]); // The buffer to which cURL handle error messages are written
			curl::CallbackClient curlClient(pErrorBuffer.get());
			curlClient.addHeader("Accept: application/json, text/json");
			try {
				console_weather::ForecastServiceClient forecastClient(curlClient, DEFAULT_MEASUREMENT_UNITS);
				std::unique_ptr<forecast_io::Forecast> pForecast = forecastClient.get(pUrl);
				if (pForecast == nullptr)
				{
					printCurlErrorMessage(pErrorBuffer.get());
					result = common::getSysExitCode(common::SysExit::HOST_UNAVAILABLE);
				} else
				{
					print(*pForecast);
					result = EXIT_SUCCESS;
				}
			} catch (const CURLcode& getterCode)
			{
				const char* pCurlErrorMessage = pErrorBuffer.get();
				if (std::strcmp(pCurlErrorMessage, "") == 0)
				{
					pCurlErrorMessage = curl_easy_strerror(getterCode);
				}
				printCurlErrorMessage(pCurlErrorMessage);
				result = common::getSysExitCode(common::SysExit::INTERNAL_ERROR);
			} catch (const json::ParseError& e)
			{
				std::cerr << createJsonParseErrorMessage(e) << std::endl;

				// Check if an invalid content type was returned by the server
				char* pInfoBuffer = NULL;
				if (isLastResponseContentInvalid(curlClient, pInfoBuffer))
				{
					std::cerr << "The response content type was expected to be JSON but was \"" << pInfoBuffer << "\" instead." << std::endl;
				}
			}
		} catch (const CURLcode& handleInitCode)
		{
			printCurlErrorMessage(curl_easy_strerror(handleInitCode));
			result = common::getSysExitCode(common::SysExit::INTERNAL_ERROR);
		}

		// De-initialise cURL
		curl_global_cleanup();
	} else
	{
		const char* pCurlErrorMessage = curl_easy_strerror(initCode);
		std::cerr << "An error occurred while initialising the cURL library: " << pCurlErrorMessage << std::endl;
		result = common::getSysExitCode(common::SysExit::INTERNAL_ERROR);
	}

	return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x8(%rbp)
movl $0x1, -0xc(%rbp)
movl $0x3, %edi
callq 0xa190
movl %eax, -0x10(%rbp)
xorl %eax, %eax
cmpl -0x10(%rbp), %eax
jne 0xd8f1
movl $0x100, %edi            # imm = 0x100
callq 0xa390
movq %rax, -0xd8(%rbp)
jmp 0xd521
movq -0xd8(%rbp), %rsi
leaq -0x18(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0xf6a0
movq -0xe0(%rbp), %rdi
callq 0xf6d0
movq %rax, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3bc60
jmp 0xd557
leaq 0x58d2e(%rip), %rax     # 0x6628c
movq %rax, -0x58(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3be30
jmp 0xd571
leaq -0x68(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
leaq -0x48(%rbp), %rsi
movl $0x1, %edx
callq 0x381e0
movq -0xe8(%rbp), %rsi
movq -0x8(%rbp), %rdx
leaq -0x70(%rbp), %rdi
callq 0x38210
jmp 0xd5a0
leaq -0x70(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xde70
testb $0x1, %al
jne 0xd5b6
jmp 0xd7ba
leaq -0x18(%rbp), %rdi
callq 0xf6d0
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rdi
callq 0xdae0
jmp 0xd5ce
movl $0x44, %edi
callq 0xde60
movl %eax, -0xc(%rbp)
jmp 0xd7d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xd872
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xd869
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xd860
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xd636
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xded0
movl -0x24(%rbp), %eax
movl %eax, -0xec(%rbp)
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0xd6ae
movq -0x20(%rbp), %rdi
callq 0xa080
movq %rax, -0xb0(%rbp)
leaq -0x18(%rbp), %rdi
callq 0xf6d0
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
leaq 0x58de3(%rip), %rsi     # 0x66459
callq 0xa670
cmpl $0x0, %eax
jne 0xd82c
movq -0xb0(%rbp), %rax
movl (%rax), %edi
callq 0xa350
movq %rax, -0xf8(%rbp)
jmp 0xd69b
movq -0xf8(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0xd82c
movl -0xec(%rbp), %eax
movl $0x2, %ecx
cmpl %ecx, %eax
jne 0xd860
movq -0x20(%rbp), %rdi
callq 0xa080
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0xd9f0
jmp 0xd6e0
movq 0x8f8d9(%rip), %rdi     # 0x9cfc0
leaq -0xa0(%rbp), %rsi
callq 0xa240
movq %rax, -0x100(%rbp)
jmp 0xd6fc
movq -0x100(%rbp), %rdi
movq 0x8f8a6(%rip), %rsi     # 0x9cfb0
callq 0xa800
jmp 0xd711
leaq -0xa0(%rbp), %rdi
callq 0xa978
movq $0x0, -0xa8(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xdb60
movb %al, -0x101(%rbp)
jmp 0xd740
movb -0x101(%rbp), %al
testb $0x1, %al
jne 0xd74f
jmp 0xd807
movq 0x8f86a(%rip), %rdi     # 0x9cfc0
leaq 0x58b53(%rip), %rsi     # 0x662b0
callq 0xa4a0
movq %rax, -0x110(%rbp)
jmp 0xd76b
movq -0x110(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0xa4a0
movq %rax, -0x118(%rbp)
jmp 0xd787
movq -0x118(%rbp), %rdi
leaq 0x58b57(%rip), %rsi     # 0x662ec
callq 0xa4a0
movq %rax, -0x120(%rbp)
jmp 0xd7a3
movq -0x120(%rbp), %rdi
movq 0x8f7ff(%rip), %rsi     # 0x9cfb0
callq 0xa800
jmp 0xd7b8
jmp 0xd807
leaq -0x70(%rbp), %rdi
callq 0xdea0
movq %rax, %rdi
callq 0xd960
jmp 0xd7cd
movl $0x0, -0xc(%rbp)
leaq -0x70(%rbp), %rdi
callq 0xded0
jmp 0xd84c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0xd810
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0xa978
jmp 0xd810
callq 0xa630
jmp 0xd80e
jmp 0xd84c
callq 0xa630
jmp 0xd817
jmp 0xd860
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xa630
jmp 0xd860
leaq -0xb8(%rbp), %rdi
callq 0xdae0
jmp 0xd83a
movl $0x46, %edi
callq 0xde60
movl %eax, -0xc(%rbp)
callq 0xa630
leaq -0x48(%rbp), %rdi
callq 0x3bda0
leaq -0x18(%rbp), %rdi
callq 0xf4b0
jmp 0xd8d7
leaq -0x48(%rbp), %rdi
callq 0x3bda0
leaq -0x18(%rbp), %rdi
callq 0xf4b0
movl -0x24(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0xd94a
movq -0x20(%rbp), %rdi
callq 0xa080
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movl (%rax), %edi
callq 0xa350
movq %rax, -0x128(%rbp)
jmp 0xd8a9
movq -0x128(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0xdae0
jmp 0xd8c5
movl $0x46, %edi
callq 0xde60
movl %eax, -0xc(%rbp)
callq 0xa630
callq 0xa730
jmp 0xd93e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0xa630
jmp 0xd94a
movl -0x10(%rbp), %edi
callq 0xa350
movq %rax, -0xd0(%rbp)
movq 0x8f6b9(%rip), %rdi     # 0x9cfc0
leaq 0x589e9(%rip), %rsi     # 0x662f7
callq 0xa4a0
movq %rax, %rdi
movq -0xd0(%rbp), %rsi
callq 0xa4a0
movq %rax, %rdi
movq 0x8f684(%rip), %rsi     # 0x9cfb0
callq 0xa800
movl $0x46, %edi
callq 0xde60
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0xa520
movq %rax, %rdi
callq 0xdf40
nopl (%rax,%rax)