// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rcx, %r13
movq %rdx, 0x18(%rsp)
testq %rdi, %rdi
sete %al
testq %rsi, %rsi
sete %cl
orb %al, %cl
jne 0x10302
movq %r9, %r15
movq %r8, %r12
movq %rsi, %rbx
movq %rdi, %r14
callq 0xb849
testl %eax, %eax
je 0x10302
cmpb $0x2d, (%r14)
jne 0x10302
cmpb $0x2d, 0x1(%r14)
jne 0x10302
cmpb $0xa, 0x2(%r14)
je 0x10302
movslq %eax, %rsi
xorl %eax, %eax
movq %r15, 0x30(%rsp)
movq %r13, 0x20(%rsp)
cmpb $0xa, (%r14,%rax)
je 0x101d5
incq %rax
cmpq %rax, %rbx
jne 0x101c3
movq %rbx, %rax
leaq 0x1(%rax), %rcx
xorl %edx, %edx
cmpq %rbx, %rax
movl $0x0, %r13d
cmovneq %rcx, %r13
movq %r12, 0x28(%rsp)
movb %dl, (%r12)
movq 0x18(%rsp), %rax
movb %dl, (%rax)
movq %r13, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
leaq (%r14,%r13), %r15
movq %rsi, %rax
xorl %r12d, %r12d
subq %r13, %rax
je 0x10223
cmpb $0xa, (%r15,%r12)
je 0x10223
incq %r12
cmpq %r12, %rax
jne 0x10211
movq %rax, %r12
movq %r12, %rbp
incq %rbp
sete %cl
cmpq %rax, %r12
je 0x102a3
testb %cl, %cl
jne 0x102a3
movl $0x15, %edx
leaq 0x51ff(%rip), %rdi      # 0x15440
movq %r15, %rsi
callq 0x6c88
testl %eax, %eax
jne 0x10296
addq $0x15, %r15
movq %r15, 0x38(%rsp)
addq $-0x16, %r12
movq %r12, 0x40(%rsp)
leaq 0x38(%rsp), %r15
movq %r15, %rdi
leaq 0x5d8f(%rip), %rsi      # 0x15ffd
movq 0x18(%rsp), %rdx
movq 0x20(%rsp), %rcx
callq 0xd707
movq %r15, %rdi
leaq 0x5d72(%rip), %rsi      # 0x15ff9
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
callq 0xd707
addq %rbp, %r13
movq 0x10(%rsp), %rsi
jmp 0x10202
movq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rdx
leaq (%rdx,%rcx), %rax
addq $-0x2, %rax
cmpq %rbx, %rax
jae 0x10302
addq $-0x2, %rdx
leaq (%r14,%rcx), %r12
movq %rdx, 0x8(%rsp)
leaq (%rcx,%rdx), %rbp
xorl %r15d, %r15d
xorl %r13d, %r13d
cmpb $0x2d, (%r12,%r13)
jne 0x102ed
leaq (%r12,%r13), %rsi
movq %r14, %rdi
movq 0x8(%rsp), %rdx
callq 0x30c0
testl %eax, %eax
je 0x10317
leaq 0x1(,%r13), %rax
addq %rbp, %rax
incq %r13
cmpq %rbx, %rax
jb 0x102d1
jmp 0x10305
xorl %r15d, %r15d
movq %r15, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x88(%rsp), %rcx
testq %rcx, %rcx
je 0x1032b
leaq -0x2(%r13), %rax
movq %rax, (%rcx)
movq 0x10(%rsp), %r15
addq %r13, %r15
movq 0x80(%rsp), %rax
testq %rax, %rax
je 0x10305
movq %r12, (%rax)
jmp 0x10305