// File: /workspace/github_c_makefile_star2_O1/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), %rbp
leaq 0x3d62(%rip), %rsi      # 0x15338
movl $0x7, %edx
callq 0x808e
cmpq $0x0, 0x18(%rbx)
je 0x115ea
orb $-0x80, (%rbx)
cmpq $0x0, 0x20(%rbx)
je 0x115f4
orb $0x40, (%rbx)
cmpq $0x0, 0x8(%rbx)
je 0x11605
cmpq $0x0, 0x10(%rbx)
je 0x11605
orb $0x4, (%rbx)
cmpw $0x0, 0x2(%rbx)
jne 0x11612
movw $0x3c, 0x2(%rbx)
movq %r14, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x808e
movzwl 0x2(%rbx), %eax
rolw $0x8, %ax
leaq 0xe(%rsp), %r13
movw %ax, (%r13)
movq %r14, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0x808e
movq %r12, %rdi
callq 0x3180
movq %rax, %r15
rolw $0x8, %ax
movw %ax, (%r13)
movq %r14, %rdi
movq %r13, %rsi
movl $0x2, %edx
callq 0x808e
movzwl %r15w, %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x808e
testb $0x4, (%rbx)
jne 0x11685
addl $0xc, %r15d
jmp 0x1170b
movq 0x8(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movq %r14, %rdi
movl $0x2, %edx
callq 0x808e
movq 0x8(%rbx), %rsi
movq %rbp, 0x10(%rsp)
movl $0xffff, %ebp           # imm = 0xFFFF
movl %r12d, %edx
andl %ebp, %edx
movq %r14, %rdi
callq 0x808e
movq 0x10(%rbx), %rdi
callq 0x3180
movq %rax, %r13
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movq %r14, %rdi
movl $0x2, %edx
callq 0x808e
movq 0x10(%rbx), %rsi
andl %r13d, %ebp
movq %r14, %rdi
movl %ebp, %edx
movq 0x10(%rsp), %rbp
callq 0x808e
addl %r15d, %r12d
leal (%r12,%r13), %r15d
addl $0x10, %r15d
cmpb $0x0, (%rbx)
jns 0x1174c
movq 0x18(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movq %r14, %rdi
movl $0x2, %edx
callq 0x808e
movq 0x18(%rbx), %rsi
movzwl %r12w, %edx
movq %r14, %rdi
callq 0x808e
addl %r12d, %r15d
addl $0x2, %r15d
testb $0x40, (%rbx)
je 0x1178d
movq 0x20(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0xe(%rsp), %rsi
movw %ax, (%rsi)
movq %r14, %rdi
movl $0x2, %edx
callq 0x808e
movq 0x20(%rbx), %rsi
movzwl %r12w, %edx
movq %r14, %rdi
callq 0x808e
addl %r12d, %r15d
addl $0x2, %r15d
movzwl %r15w, %ecx
movq %r14, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x117bc
testq %rbp, %rbp
je 0x117ad
movzwl 0x2(%rbx), %eax
movw %ax, (%rbp)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq