// File: jliesch[P]ofxget/ofxget.cc
// Function: ofxget::OfxGetContext::PostRequest()
OfxGetContext& OfxGetContext::PostRequest() {
  response_.clear();
  if (is_error()) return *this;
    // Fake account
    if (0) { response_ = R"FAKE(OFXHEADER:100
                  DATA:OFXSGML
                  VERSION:102
                  SECURITY:NONE
                  ENCODING:USASCII
                  CHARSET:1252
                  COMPRESSION:NONE
                  OLDFILEUID:NONE
                  NEWFILEUID:20180421105945.000

                  <OFX>
                  <SIGNONMSGSRSV1>
                  <SONRS>
                  <STATUS>
                  <CODE>0
                  <SEVERITY>INFO
                  <MESSAGE>Successful Sign On
                  </STATUS>
                  <DTSERVER>20180421125958[-5:EST]
                  <LANGUAGE>ENG
                  <DTPROFUP>20140605083000
                  <FI>
                  <ORG>Vanguard
                  <FID>15103
                  </FI>
                  <SESSCOOKIE>xxx
                  </SONRS>
                  </SIGNONMSGSRSV1>
                  <SIGNUPMSGSRSV1>
                  <ACCTINFOTRNRS>
                  <TRNUID>20180421105945.000
                  <STATUS>
                  <CODE>0
                  <SEVERITY>INFO
                  <MESSAGE>AcctInfoTrnRsV1 is successful
                  </STATUS>
                  <CLTCOOKIE>1
                  <ACCTINFORS>
                  <DTACCTUP>20180421125958[-5:EST]
                  <ACCTINFO>
                  <DESC>ACCT1
                  <INVACCTINFO>
                  <INVACCTFROM>
                  <BROKERID>vanguard.com
                  <ACCTID>1
                  </INVACCTFROM>
                  <USPRODUCTTYPE>401K
                  <CHECKING>N
                  <SVCSTATUS>ACTIVE
                  <INVACCTTYPE>INDIVIDUAL
                  </INVACCTINFO>
                  </ACCTINFO>
                  <ACCTINFO>
                  <INVACCTINFO>
                  <INVACCTFROM>
                  <BROKERID>vanguard.com
                  <ACCTID>2
                  </INVACCTFROM>
                  <USPRODUCTTYPE>IRA
                  <CHECKING>N
                  <SVCSTATUS>ACTIVE
                  <INVACCTTYPE>INDIVIDUAL
                  </INVACCTINFO>
                  </ACCTINFO>
                  <ACCTINFO>
                  <INVACCTINFO>
                  <INVACCTFROM>
                  <BROKERID>vanguard.com
                  <ACCTID>3
                  </INVACCTFROM>
                  <USPRODUCTTYPE>IRA
                  <CHECKING>N
                  <SVCSTATUS>ACTIVE
                  <INVACCTTYPE>INDIVIDUAL
                  </INVACCTINFO>
                  </ACCTINFO>
                  <ACCTINFO>
                  <INVACCTINFO>
                  <INVACCTFROM>
                  <BROKERID>vanguard.com
                  <ACCTID>4
                  </INVACCTFROM>
                  <USPRODUCTTYPE>IRA
                  <CHECKING>N
                  <SVCSTATUS>ACTIVE
                  <INVACCTTYPE>INDIVIDUAL
                  </INVACCTINFO>
                  </ACCTINFO>
                  <ACCTINFO>
                  <INVACCTINFO>
                  <INVACCTFROM>
                  <BROKERID>vanguard.com
                  <ACCTID>5
                  </INVACCTFROM>
                  <USPRODUCTTYPE>IRA
                  <CHECKING>N
                  <SVCSTATUS>ACTIVE
                  <INVACCTTYPE>INDIVIDUAL
                  </INVACCTINFO>
                  </ACCTINFO>
                  </ACCTINFORS>
                  </ACCTINFOTRNRS>
                  </SIGNUPMSGSRSV1>
                  </OFX>)FAKE"; return *this; }
    // Fake investment
    if (0) { response_ = R"FAKE(OFXHEADER:100
            DATA:OFXSGML
            VERSION:102
            SECURITY:NONE
            ENCODING:USASCII
            CHARSET:1252
            COMPRESSION:NONE
            OLDFILEUID:NONE
            NEWFILEUID:20180321182302.000

            <OFX><SIGNONMSGSRSV1><SONRS><STATUS><CODE>0<SEVERITY>INFO<MESSAGE>Successful Sign On</STATUS><DTSERVER>20180321202323[-5:EST]<LANGUAGE>ENG<DTPROFUP>20140605083000<FI><ORG>Vanguard<FID>15103</FI><SESSCOOKIE>xx</SONRS></SIGNONMSGSRSV1><INVSTMTMSGSRSV1><INVSTMTTRNRS><TRNUID>20180321182302.000<STATUS><CODE>0<SEVERITY>INFO</STATUS><CLTCOOKIE>4<INVSTMTRS><DTASOF>20180321160000.000[-5:EST]<CURDEF>USD<INVACCTFROM><BROKERID>vanguard.com<ACCTID>123</INVACCTFROM><INVTRANLIST><DTSTART>20160921160000.000[-5:EST]<DTEND>20180321202323.000[-5:EST]<BUYMF><INVBUY><INVTRAN><FITID>88032745229.5132.12212016.0<DTTRADE>20161221160000.000[-5:EST]<DTSETTLE>20161221160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>921937702<UNIQUEIDTYPE>CUSIP</SECID><UNITS>190.385<UNITPRICE>10.4<TOTAL>-1980.0<SUBACCTSEC>CASH<SUBAC
            CTFUND>OTHER</INVBUY><BUYTYPE>BUY</BUYMF><BUYMF><INVBUY><INVTRAN><FITID>88032745229.5132.01302017.0<DTTRADE>20170130160000.000[-5:EST]<DTSETTLE>20170130160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>921937702<UNIQUEIDTYPE>CUSIP</SECID><UNITS>671.141<UNITPRICE>10.43<TOTAL>-7000.0<SUBACCTSEC>CASH<SUBACCTFUND>OTHER</INVBUY><BUYTYPE>BUY</BUYMF><BUYMF><INVBUY><INVTRAN><FITID>88032745229.5132.01302017.1<DTTRADE>20170130160000.000[-5:EST]<DTSETTLE>20170130160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>921937702<UNIQUEIDTYPE>CUSIP</SECID><UNITS>287.632<UNITPRICE>10.43<TOTAL>-3000.0<SUBACCTSEC>CASH<SUBACCTFUND>OTHER</INVBUY><BUYTYPE>BUY</BUYMF><BUYMF><INVBUY><INVTRAN><FITID>88032745229.0569.12212016.0<DTTRADE>20161221160000.000[-5:EST]<DTSETTLE>20161221160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>921909818<UNIQUEIDTYPE>CUSIP</SECID><UNITS>143.615<UNITPRICE>24.51<TOTAL>-3520.0<SUBACCTSEC>CASH<SUBACCTFUND>OTHER</INVBUY><BUYTYPE>BUY</BUYMF><BUYMF><INVBUY><INVTRAN><FITID>88032745229.0569.12212016.1<DTTRADE>20161221160000.000[
            -5:EST]<DTSETTLE>20161221160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>921909818<UNIQUEIDTYPE>CUSIP</SECID><UNITS>0.004<UNITPRICE>24.51<TOTAL>-0.11<SUBACCTSEC>CASH<SUBACCTFUND>OTHER</INVBUY><BUYTYPE>BUY</BUYMF><BUYMF><INVBUY><INVTRAN><FITID>88032745229.0569.01162018.0<DTTRADE>20180116160000.000[-5:EST]<DTSETTLE>20180116160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>921909818<UNIQUEIDTYPE>CUSIP</SECID><UNITS>136.292<UNITPRICE>31.88<TOTAL>-4345.0<SUBACCTSEC>CASH<SUBACCTFUND>OTHER</INVBUY><BUYTYPE>BUY</BUYMF><BUYMF><INVBUY><INVTRAN><FITID>88032745229.0585.01162018.0<DTTRADE>20180116160000.000[-5:EST]<DTSETTLE>20180116160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>922908728<UNIQUEIDTYPE>CUSIP</SECID><UNITS>16.698<UNITPRICE>69.17<TOTAL>-1155.0<SUBACCTSEC>CASH<SUBACCTFUND>OTHER</INVBUY><BUYTYPE>BUY</BUYMF><BUYMF><INVBUY><INVTRAN><FITID>88032745229.0585.01162018.1<DTTRADE>20180116160000.000[-5:EST]<DTSETTLE>20180116160000.000[-5:EST]</INVTRAN><SECID><UNIQUEID>922908728<UNIQUEIDTYPE>CUSIP</SECID><UNITS>0.022<UNITPRICE>69.)FAKE"; return *this; }
  if (vars_map_.find("URL") == vars_map_.end()) {
    error_string_ = "URL not in vars map";
    return *this;
  }
  if (request_template_.empty()) {
    error_string_ = "no request template was added";
    return *this;
  }

  CURL *curl = curl_easy_init();
  if (!curl) {
    error_string_ = "Could not initialize curl";
    return *this;
  }

  string request_str = request();
  curl_easy_setopt(curl, CURLOPT_URL, vars_map_["URL"].c_str());
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_str.c_str());

  struct curl_slist *headerlist = NULL;
  headerlist = curl_slist_append(headerlist, "Content-type: application/x-ofx");
  headerlist = curl_slist_append(headerlist, "Accept: */*, application/x-ofx");

  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteToString);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) this);
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, REQUEST_TIMEOUT /* seconds */);

  CURLcode res = curl_easy_perform(curl);

  curl_easy_cleanup(curl);
  curl_slist_free_all(headerlist);

  if (res != CURLE_OK) {
    error_string_ = "Curl error: " + std::to_string(res);
    return *this;
  }
  return *this;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x108(%rbp)
addq $0x70, %rdi
callq 0x7510
movq -0x108(%rbp), %rdi
callq 0xad00
testb $0x1, %al
jne 0xa135
jmp 0xa145
movq -0x108(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa5a3
movq -0x108(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x78f0
movq -0x110(%rbp), %rdx
leaq 0x5efb0(%rip), %rsi     # 0x69121
leaq -0x38(%rbp), %rdi
callq 0x77b0
jmp 0xa17c
movq -0x118(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xb480
movq %rax, -0x120(%rbp)
jmp 0xa195
movq -0x108(%rbp), %rdi
movq -0x120(%rbp), %rax
movq %rax, -0x18(%rbp)
callq 0xb500
movq %rax, -0x58(%rbp)
leaq -0x18(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xb550
movb %al, -0x121(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7b88
leaq -0x39(%rbp), %rdi
callq 0x7b70
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0xa1e1
jmp 0xa239
movq -0x108(%rbp), %rdi
addq $0x30, %rdi
leaq 0x5ef32(%rip), %rsi     # 0x69125
callq 0x73b0
movq -0x108(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa5a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa22b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x7b88
leaq -0x39(%rbp), %rdi
callq 0x7b70
jmp 0xa5b0
movq -0x108(%rbp), %rdi
addq $0x50, %rdi
callq 0x7250
testb $0x1, %al
jne 0xa24f
jmp 0xa276
movq -0x108(%rbp), %rdi
addq $0x30, %rdi
leaq 0x5eed8(%rip), %rsi     # 0x69139
callq 0x73b0
movq -0x108(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa5a3
callq 0x7920
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xa2ad
movq -0x108(%rbp), %rdi
addq $0x30, %rdi
leaq 0x5eebf(%rip), %rsi     # 0x69157
callq 0x73b0
movq -0x108(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa5a3
movq -0x108(%rbp), %rsi
leaq -0x80(%rbp), %rdi
callq 0x9d70
movq -0x108(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, -0x140(%rbp)
movq %rax, -0x138(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x78f0
movq -0x130(%rbp), %rdx
leaq 0x5ee2a(%rip), %rsi     # 0x69121
leaq -0xa0(%rbp), %rdi
callq 0x77b0
jmp 0xa305
movq -0x138(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0xae10
movq %rax, -0x148(%rbp)
jmp 0xa321
movq -0x148(%rbp), %rdi
callq 0x7100
movq -0x140(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
movl $0x2712, %esi           # imm = 0x2712
callq 0x7820
jmp 0xa345
leaq -0xa0(%rbp), %rdi
callq 0x7b88
leaq -0xa1(%rbp), %rdi
callq 0x7b70
movq -0x60(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x7100
movq -0x150(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
movl $0x271f, %esi           # imm = 0x271F
callq 0x7820
jmp 0xa389
movq $0x0, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
leaq 0x5edcf(%rip), %rsi     # 0x69171
callq 0x7610
movq %rax, -0x158(%rbp)
jmp 0xa3b0
movq -0x158(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
leaq 0x5edc5(%rip), %rsi     # 0x69191
callq 0x7610
movq %rax, -0x160(%rbp)
jmp 0xa3da
movq -0x160(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x60(%rbp), %rdi
movq -0xb0(%rbp), %rdx
xorl %eax, %eax
movl $0x2727, %esi           # imm = 0x2727
callq 0x7820
jmp 0xa401
movq -0x60(%rbp), %rdi
leaq 0x1b4(%rip), %rdx       # 0xa5c0
xorl %eax, %eax
movl $0x4e2b, %esi           # imm = 0x4E2B
callq 0x7820
jmp 0xa41a
movq -0x108(%rbp), %rdx
movq -0x60(%rbp), %rdi
xorl %eax, %eax
movl $0x2711, %esi           # imm = 0x2711
callq 0x7820
jmp 0xa433
movq -0x60(%rbp), %rdi
leaq 0x312(%rip), %rdx       # 0xa750
xorl %eax, %eax
movl $0x4e6f, %esi           # imm = 0x4E6F
callq 0x7820
jmp 0xa44c
movq -0x60(%rbp), %rdi
xorl %eax, %eax
movl $0xd, %esi
movl $0xa, %edx
callq 0x7820
jmp 0xa463
movq -0x60(%rbp), %rdi
callq 0x7670
movl %eax, -0x164(%rbp)
jmp 0xa474
movl -0x164(%rbp), %eax
movl %eax, -0xb4(%rbp)
movq -0x60(%rbp), %rdi
callq 0x7120
jmp 0xa48b
movq -0xb0(%rbp), %rdi
callq 0x7860
jmp 0xa499
cmpl $0x0, -0xb4(%rbp)
je 0xa578
movl -0xb4(%rbp), %esi
leaq -0xf8(%rbp), %rdi
callq 0xb000
jmp 0xa4ba
leaq 0x5ecef(%rip), %rsi     # 0x691b0
leaq -0xd8(%rbp), %rdi
leaq -0xf8(%rbp), %rdx
callq 0xb580
jmp 0xa4d6
movq -0x108(%rbp), %rdi
addq $0x30, %rdi
leaq -0xd8(%rbp), %rsi
callq 0x74e0
leaq -0xd8(%rbp), %rdi
callq 0x7b88
leaq -0xf8(%rbp), %rdi
callq 0x7b88
movq -0x108(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0xfc(%rbp)
jmp 0xa58d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa542
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x7b88
leaq -0xa1(%rbp), %rdi
callq 0x7b70
jmp 0xa598
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xa598
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x7b88
jmp 0xa598
movq -0x108(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x1, -0xfc(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x7b88
jmp 0xa5a3
leaq -0x80(%rbp), %rdi
callq 0x7b88
jmp 0xa5b0
movq -0x8(%rbp), %rax
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x7600
nopl (%rax)