// File: /workspace/github_c_makefile_star2_O1/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 0x69aa
testq %r13, %r13
je 0xb47f
movq 0x8(%r13), %rdx
testq %rdx, %rdx
je 0xb47f
movq (%r13), %rsi
leaq 0x8(%rsp), %r13
movq %r13, %rdi
callq 0x6ba9
leaq 0xae45(%rip), %rsi      # 0x162b7
movl $0x3, %edx
movq %r13, %rdi
callq 0x6ba9
testq %r12, %r12
je 0xb4b3
movq 0x8(%r12), %rdx
testq %rdx, %rdx
je 0xb4b3
movq (%r12), %rsi
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0x6ba9
leaq 0xa547(%rip), %rsi      # 0x159ed
movl $0x1, %edx
movq %r12, %rdi
callq 0x6ba9
testq %r15, %r15
je 0xb4ce
movq 0x8(%r15), %rdx
testq %rdx, %rdx
je 0xb4ce
movq (%r15), %rsi
leaq 0x8(%rsp), %rdi
callq 0x6ba9
testl %ebp, %ebp
je 0xb4fa
leaq 0xaaa3(%rip), %rsi      # 0x15f7c
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 0x6ba9
movl 0x78(%rsp), %eax
testq %r14, %r14
je 0xb566
movq 0x8(%r14), %rdx
testq %rdx, %rdx
je 0xb566
testl %eax, %eax
je 0xb57d
movq (%r14), %rdi
movq %rdx, %rsi
callq 0x6cd6
movq %rax, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
cmpq 0x8(%r14), %rdx
jne 0xb606
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xb295
movq (%r15), %r14
testl %eax, %eax
je 0xb5fe
movq 0x28(%rsp), %rdx
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x6ba9
movq %r14, %rdi
callq 0x3060
jmp 0xb58a
testl %eax, %eax
je 0xb58a
leaq 0xab87(%rip), %rsi      # 0x160f8
leaq 0x8(%rsp), %rdi
movl $0x1, %edx
jmp 0xb585
movq (%r14), %rsi
leaq 0x8(%rsp), %rdi
callq 0x6ba9
movq 0x70(%rsp), %r15
testq %rbx, %rbx
je 0xb5c3
cmpq $0x0, 0x8(%rbx)
je 0xb5c3
leaq 0xa44d(%rip), %rsi      # 0x159ef
leaq 0x8(%rsp), %r14
movl $0x1, %edx
movq %r14, %rdi
callq 0x6ba9
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
movq %r14, %rdi
callq 0x6ba9
xorl %ebx, %ebx
movb $0x1, %bpl
testq %r15, %r15
je 0xb60d
cmpq $0x0, 0x8(%r15)
je 0xb60d
leaq 0xbbbf(%rip), %rsi      # 0x1719a
leaq 0x8(%rsp), %r14
movl $0x1, %edx
movq %r14, %rdi
callq 0x6ba9
movq (%r15), %rsi
movq 0x8(%r15), %rdx
movq %r14, %rdi
callq 0x6ba9
jmp 0xb60d
movq %r14, %rdi
callq 0x3060
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq 0x80(%rsp), %r14
testb %bpl, %bpl
je 0xb62d
movq 0x8(%rsp), %rax
movq %rax, (%r14)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%r14)
jmp 0xb63e
leaq 0x8(%rsp), %rdi
callq 0x6a01
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq