// File: nilnilnil[P]autocomplete/deps/mongoose/mongoose.c
// Function: put_file
static void put_file(struct mg_connection *conn, const char *path) {
  struct mgstat st;
  const char *range;
  int64_t r1, r2;
  FILE *fp;
  int rc;

  conn->request_info.status_code = mg_stat(path, &st) == 0 ? 200 : 201;

  if ((rc = put_dir(path)) == 0) {
    mg_printf(conn, "HTTP/1.1 %d OK\r\n\r\n", conn->request_info.status_code);
  } else if (rc == -1) {
    send_http_error(conn, 500, http_500_error,
        "put_dir(%s): %s", path, strerror(ERRNO));
  } else if ((fp = mg_fopen(path, "wb+")) == NULL) {
    send_http_error(conn, 500, http_500_error,
        "fopen(%s): %s", path, strerror(ERRNO));
  } else {
    set_close_on_exec(fileno(fp));
    range = mg_get_header(conn, "Content-Range");
    r1 = r2 = 0;
    if (range != NULL && parse_range_header(range, &r1, &r2) > 0) {
      conn->request_info.status_code = 206;
      // TODO(lsm): handle seek error
      (void) fseeko(fp, (off_t) r1, SEEK_SET);
    }
    if (forward_body_data(conn, fp, INVALID_SOCKET, NULL))
      (void) mg_printf(conn, "HTTP/1.1 %d OK\r\n\r\n",
          conn->request_info.status_code);
    (void) fclose(fp);
  }
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x1a8a0
movl %eax, %edx
movl $0xc9, %ecx
movl $0xc8, %eax
cmpl $0x0, %edx
cmovel %eax, %ecx
movq 0x80(%rsp), %rax
movl %ecx, 0x4c(%rax)
movq 0x78(%rsp), %rdi
callq 0x1f840
movl %eax, 0x3c(%rsp)
cmpl $0x0, %eax
jne 0x1dc49
movq 0x80(%rsp), %rdi
movq 0x80(%rsp), %rax
movl 0x4c(%rax), %edx
leaq 0x560e(%rip), %rsi      # 0x2324b
movb $0x0, %al
callq 0x17030
jmp 0x1ddea
cmpl $-0x1, 0x3c(%rsp)
jne 0x1dca9
movq 0x80(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xddfc(%rip), %rax      # 0x2ba60
movq %rax, 0x28(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
callq 0x5060
movl (%rax), %edi
callq 0x5190
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %r8
movq %rax, %r9
movl $0x1f4, %esi            # imm = 0x1F4
leaq 0x55c1(%rip), %rcx      # 0x2325e
movb $0x0, %al
callq 0x1bc40
jmp 0x1dde8
movq 0x78(%rsp), %rdi
leaq 0x55b9(%rip), %rsi      # 0x2326e
callq 0x56b0
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
jne 0x1dd1e
movq 0x80(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xdd87(%rip), %rax      # 0x2ba60
movq %rax, 0x10(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
callq 0x5060
movl (%rax), %edi
callq 0x5190
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %r8
movq %rax, %r9
movl $0x1f4, %esi            # imm = 0x1F4
leaq 0x5be4(%rip), %rcx      # 0x238f6
movb $0x0, %al
callq 0x1bc40
jmp 0x1dde6
movq 0x40(%rsp), %rdi
callq 0x55c0
movl %eax, %edi
callq 0x1ae20
movq 0x80(%rsp), %rdi
leaq 0x5534(%rip), %rsi      # 0x23272
callq 0x16b70
movq %rax, 0x58(%rsp)
movq $0x0, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
cmpq $0x0, 0x58(%rsp)
je 0x1dd9b
movq 0x58(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x1f940
cmpl $0x0, %eax
jle 0x1dd9b
movq 0x80(%rsp), %rax
movl $0xce, 0x4c(%rax)
movq 0x40(%rsp), %rdi
movq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0x5300
movq 0x80(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %eax, %eax
movl %eax, %ecx
callq 0x1d4c0
cmpl $0x0, %eax
je 0x1dddc
movq 0x80(%rsp), %rdi
movq 0x80(%rsp), %rax
movl 0x4c(%rax), %edx
leaq 0x5476(%rip), %rsi      # 0x2324b
movb $0x0, %al
callq 0x17030
movq 0x40(%rsp), %rdi
callq 0x5560
jmp 0x1dde8
jmp 0x1ddea
addq $0x88, %rsp
retq
nopw %cs:(%rax,%rax)