// File: /workspace/github_c_makefile_star2_O2/sfeakes[P]unifi-motion/mongoose.c
// Function: mg_send_mqtt_handshake_opt
void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
                                struct mg_send_mqtt_handshake_opts opts) {
  uint16_t hlen, nlen, rem_len = 0;
  struct mg_mqtt_proto_data *pd = (struct mg_mqtt_proto_data *) nc->proto_data;

  mg_send(nc, "\00\04MQTT\04", 7);
  rem_len += 7;

  if (opts.user_name != NULL) {
    opts.flags |= MG_MQTT_HAS_USER_NAME;
  }
  if (opts.password != NULL) {
    opts.flags |= MG_MQTT_HAS_PASSWORD;
  }
  if (opts.will_topic != NULL && opts.will_message != NULL) {
    opts.flags |= MG_MQTT_HAS_WILL;
  }
  if (opts.keep_alive == 0) {
    opts.keep_alive = 60;
  }

  mg_send(nc, &opts.flags, 1);
  rem_len += 1;

  nlen = htons(opts.keep_alive);
  mg_send(nc, &nlen, 2);
  rem_len += 2;

  hlen = strlen(client_id);
  nlen = htons((uint16_t) hlen);
  mg_send(nc, &nlen, 2);
  mg_send(nc, client_id, hlen);
  rem_len += 2 + hlen;

  if (opts.flags & MG_MQTT_HAS_WILL) {
    hlen = strlen(opts.will_topic);
    nlen = htons((uint16_t) hlen);
    mg_send(nc, &nlen, 2);
    mg_send(nc, opts.will_topic, hlen);
    rem_len += 2 + hlen;

    hlen = strlen(opts.will_message);
    nlen = htons((uint16_t) hlen);
    mg_send(nc, &nlen, 2);
    mg_send(nc, opts.will_message, hlen);
    rem_len += 2 + hlen;
  }

  if (opts.flags & MG_MQTT_HAS_USER_NAME) {
    hlen = strlen(opts.user_name);
    nlen = htons((uint16_t) hlen);
    mg_send(nc, &nlen, 2);
    mg_send(nc, opts.user_name, hlen);
    rem_len += 2 + hlen;
  }
  if (opts.flags & MG_MQTT_HAS_PASSWORD) {
    hlen = strlen(opts.password);
    nlen = htons((uint16_t) hlen);
    mg_send(nc, &nlen, 2);
    mg_send(nc, opts.password, hlen);
    rem_len += 2 + hlen;
  }

  mg_mqtt_prepend_header(nc, MG_MQTT_CMD_CONNECT, 0, rem_len);

  if (pd != NULL) {
    pd->keep_alive = opts.keep_alive;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %r14
leaq 0x50(%rsp), %rbx
movq 0x88(%rdi), %rax
movq %rax, 0x10(%rsp)
leaq 0x3912(%rip), %rsi      # 0x14340
pushq $0x7
popq %rdx
callq 0x7b89
cmpq $0x0, 0x18(%rbx)
je 0x10a40
orb $-0x80, (%rbx)
cmpq $0x0, 0x20(%rbx)
je 0x10a4a
orb $0x40, (%rbx)
cmpq $0x0, 0x8(%rbx)
je 0x10a5b
cmpq $0x0, 0x10(%rbx)
je 0x10a5b
orb $0x4, (%rbx)
cmpw $0x0, 0x2(%rbx)
jne 0x10a68
movw $0x3c, 0x2(%rbx)
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7b89
movzwl 0x2(%rbx), %eax
rolw $0x8, %ax
leaq 0xe(%rsp), %r13
movw %ax, (%r13)
pushq $0x2
popq %rbp
movq %r14, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x7b89
movq %r12, %rdi
callq 0x3180
movq %rax, %r15
rolw $0x8, %ax
movw %ax, (%r13)
movq %r14, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x7b89
movzwl %r15w, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x7b89
movb (%rbx), %al
testb $0x4, %al
jne 0x10ad4
addl $0xc, %r15d
jmp 0x10b4e
movq 0x8(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x7b89
movq 0x8(%rbx), %rsi
movl $0xffff, %ebp           # imm = 0xFFFF
movl %r12d, %edx
andl %ebp, %edx
movq %r14, %rdi
callq 0x7b89
movq 0x10(%rbx), %rdi
callq 0x3180
movq %rax, %r13
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movq %r14, %rdi
pushq $0x2
popq %rdx
callq 0x7b89
movq 0x10(%rbx), %rsi
andl %r13d, %ebp
movq %r14, %rdi
movl %ebp, %edx
callq 0x7b89
addl %r15d, %r12d
leal (%r12,%r13), %r15d
addl $0x10, %r15d
movb (%rbx), %al
testb %al, %al
jns 0x10b8e
movq 0x18(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x7b89
movq 0x18(%rbx), %rsi
movzwl %r12w, %edx
movq %r14, %rdi
callq 0x7b89
addl %r12d, %r15d
addl $0x2, %r15d
movb (%rbx), %al
testb $0x40, %al
je 0x10bcc
movq 0x20(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
pushq $0x2
popq %rdx
movq %r14, %rdi
callq 0x7b89
movq 0x20(%rbx), %rsi
movzwl %r12w, %edx
movq %r14, %rdi
callq 0x7b89
addl %r12d, %r15d
addl $0x2, %r15d
movzwl %r15w, %ecx
pushq $0x1
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x10bfd
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x10bee
movzwl 0x2(%rbx), %eax
movw %ax, (%rcx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq