// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_assemble_uri
int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info,
                    const struct mg_str *host, unsigned int port,
                    const struct mg_str *path, const struct mg_str *query,
                    const struct mg_str *fragment, int normalize_path,
                    struct mg_str *uri) {
  int result = -1;
  struct mbuf out;
  mbuf_init(&out, 0);

  if (scheme != NULL && scheme->len > 0) {
    mbuf_append(&out, scheme->p, scheme->len);
    mbuf_append(&out, "://", 3);
  }

  if (user_info != NULL && user_info->len > 0) {
    mbuf_append(&out, user_info->p, user_info->len);
    mbuf_append(&out, "@", 1);
  }

  if (host != NULL && host->len > 0) {
    mbuf_append(&out, host->p, host->len);
  }

  if (port != 0) {
    char port_str[20];
    int port_str_len = sprintf(port_str, ":%u", port);
    mbuf_append(&out, port_str, port_str_len);
  }

  if (path != NULL && path->len > 0) {
    if (normalize_path) {
      struct mg_str npath = mg_strdup(*path);
      if (npath.len != path->len) goto out;
      if (!mg_normalize_uri_path(path, &npath)) {
        free((void *) npath.p);
        goto out;
      }
      mbuf_append(&out, npath.p, npath.len);
      free((void *) npath.p);
    } else {
      mbuf_append(&out, path->p, path->len);
    }
  } else if (normalize_path) {
    mbuf_append(&out, "/", 1);
  }

  if (query != NULL && query->len > 0) {
    mbuf_append(&out, "?", 1);
    mbuf_append(&out, query->p, query->len);
  }

  if (fragment != NULL && fragment->len > 0) {
    mbuf_append(&out, "#", 1);
    mbuf_append(&out, fragment->p, fragment->len);
  }

  result = 0;

out:
  if (result == 0) {
    uri->p = out.buf;
    uri->len = out.len;
  } else {
    mbuf_free(&out);
    uri->p = NULL;
    uri->len = 0;
  }
  return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %rbx
movq %r8, %r14
movl %ecx, %ebp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
callq 0x662f
testq %r13, %r13
je 0xaa71
movq 0x8(%r13), %rdx
testq %rdx, %rdx
je 0xaa71
movq (%r13), %rsi
leaq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x6836
leaq 0xa852(%rip), %rsi      # 0x152b8
pushq $0x3
popq %rdx
movq %r13, %rdi
callq 0x6836
testq %r12, %r12
je 0xaaa3
movq 0x8(%r12), %rdx
testq %rdx, %rdx
je 0xaaa3
movq (%r12), %rsi
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x6836
leaq 0x9f59(%rip), %rsi      # 0x149f1
pushq $0x1
popq %rdx
movq %r12, %rdi
callq 0x6836
testq %r15, %r15
je 0xaabe
movq 0x8(%r15), %rdx
testq %rdx, %rdx
je 0xaabe
movq (%r15), %rsi
leaq 0x8(%rsp), %rdi
callq 0x6836
testl %ebp, %ebp
je 0xaaea
leaq 0xa4b7(%rip), %rsi      # 0x14f80
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x35a0
movslq %eax, %rdx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0x6836
movq 0x80(%rsp), %r12
movl 0x78(%rsp), %eax
testq %r14, %r14
je 0xab5e
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0xab5e
testl %eax, %eax
je 0xab73
movq (%r14), %rdi
movq %rdx, %rsi
callq 0x6969
movq %rax, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
cmpq 0x8(%r14), %rdx
jne 0xac15
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xa8f1
movq (%r15), %r14
testl %eax, %eax
je 0xac0d
movq 0x28(%rsp), %rdx
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x6836
movq %r14, %rdi
callq 0x3060
jmp 0xab80
testl %eax, %eax
je 0xab80
leaq 0xa590(%rip), %rsi      # 0x150f9
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rdx
jmp 0xab7b
movq (%r14), %rsi
leaq 0x8(%rsp), %rdi
callq 0x6836
movq 0x70(%rsp), %r15
testq %rbx, %rbx
je 0xabb7
cmpq $0x0, 0x8(%rbx)
je 0xabb7
leaq 0x9e5b(%rip), %rsi      # 0x149f3
leaq 0x8(%rsp), %r14
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x6836
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r14, %rdi
callq 0x6836
testq %r15, %r15
je 0xabe9
cmpq $0x0, 0x8(%r15)
je 0xabe9
leaq 0xb5d1(%rip), %rsi      # 0x1619b
leaq 0x8(%rsp), %rbx
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0x6836
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %rbx, %rdi
callq 0x6836
movq 0x8(%rsp), %rax
movq %rax, (%r12)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r12)
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x3060
leaq 0x8(%rsp), %rdi
callq 0x6683
xorps %xmm0, %xmm0
movups %xmm0, (%r12)
pushq $-0x1
popq %rax
jmp 0xabfe