// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_parse_multipart
size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name,
                          size_t var_name_len, char *file_name,
                          size_t file_name_len, const char **data,
                          size_t *data_len) {
  static const char cd[] = "Content-Disposition: ";
  size_t hl, bl, n, ll, pos, cdl = sizeof(cd) - 1;

  if (buf == NULL || buf_len <= 0) return 0;
  if ((hl = mg_http_get_request_len(buf, buf_len)) <= 0) return 0;
  if (buf[0] != '-' || buf[1] != '-' || buf[2] == '\n') return 0;

  /* Get boundary length */
  bl = mg_get_line_len(buf, buf_len);

  /* Loop through headers, fetch variable name and file name */
  var_name[0] = file_name[0] = '\0';
  for (n = bl; (ll = mg_get_line_len(buf + n, hl - n)) > 0; n += ll) {
    if (mg_ncasecmp(cd, buf + n, cdl) == 0) {
      struct mg_str header;
      header.p = buf + n + cdl;
      header.len = ll - (cdl + 2);
      mg_http_parse_header(&header, "name", var_name, var_name_len);
      mg_http_parse_header(&header, "filename", file_name, file_name_len);
    }
  }

  /* Scan through the body, search for terminating boundary */
  for (pos = hl; pos + (bl - 2) < buf_len; pos++) {
    if (buf[pos] == '-' && !strncmp(buf, &buf[pos], bl - 2)) {
      if (data_len != NULL) *data_len = (pos - 2) - hl;
      if (data != NULL) *data = buf + hl;
      return pos;
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %r13
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0xf6f9
movq %r9, %rbx
movq %r8, %r12
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r15
callq 0xae20
testl %eax, %eax
je 0xf6f9
cmpb $0x2d, (%r15)
jne 0xf6f9
cmpb $0x2d, 0x1(%r15)
jne 0xf6f9
cmpb $0xa, 0x2(%r15)
jne 0xf70d
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x10(%rsp)
movq %rbx, 0x20(%rsp)
movslq %eax, %rbx
movq %r15, %rdi
movq %r14, %rsi
callq 0xf831
xorl %ecx, %ecx
movq %r12, 0x18(%rsp)
movb %cl, (%r12)
movq %rbp, 0x8(%rsp)
movb %cl, (%rbp)
movq %rax, (%rsp)
movq %rax, %r12
leaq (%r15,%r12), %r13
movq %rbx, %rsi
subq %r12, %rsi
movq %r13, %rdi
callq 0xf831
testq %rax, %rax
je 0xf7bd
movq %rax, %rbp
leaq 0x4cd0(%rip), %rdi      # 0x14430
movq %r13, %rsi
pushq $0x15
popq %rdx
callq 0x6915
testl %eax, %eax
jne 0xf7b8
addq $0x15, %r13
movq %r13, 0x28(%rsp)
leaq -0x17(%rbp), %rax
movq %rax, 0x30(%rsp)
leaq 0x28(%rsp), %r13
movq %r13, %rdi
leaq 0x5871(%rip), %rsi      # 0x15001
movq 0x8(%rsp), %rdx
movq 0x10(%rsp), %rcx
callq 0xc94d
movq %r13, %rdi
leaq 0x5854(%rip), %rsi      # 0x14ffd
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0xc94d
addq %rbp, %r12
jmp 0xf73f
leaq (%r15,%rbx), %r12
movq (%rsp), %rax
leaq (%rbx,%rax), %rbp
addq $-0x2, %rbp
addq $-0x2, %rax
movq %rax, (%rsp)
xorl %r13d, %r13d
movq %rbp, %rax
addq %r13, %rax
cmpq %r14, %rax
jae 0xf6f9
cmpb $0x2d, (%r12,%r13)
jne 0xf802
leaq (%r12,%r13), %rsi
movq %r15, %rdi
movq (%rsp), %rdx
callq 0x30c0
testl %eax, %eax
je 0xf807
incq %r13
jmp 0xf7d8
movq 0x78(%rsp), %rcx
testq %rcx, %rcx
je 0xf818
leaq -0x2(%r13), %rax
movq %rax, (%rcx)
addq %r13, %rbx
movq 0x70(%rsp), %rax
testq %rax, %rax
je 0xf6fb
movq %r12, (%rax)
jmp 0xf6fb