// File: /workspace/github_c_makefile_star2_O3/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)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
leaq 0x3c1c(%rip), %rsi      # 0x15338
movl $0x7, %edx
movq %r14, %rdi
callq *0x48(%rcx,%rax,4)
cmpq $0x0, 0x18(%rbx)
je 0x11732
orb $-0x80, (%rbx)
cmpq $0x0, 0x20(%rbx)
je 0x1173c
orb $0x40, (%rbx)
cmpq $0x0, 0x8(%rbx)
je 0x1174d
cmpq $0x0, 0x10(%rbx)
je 0x1174d
orb $0x4, (%rbx)
cmpw $0x0, 0x2(%rbx)
jne 0x1175a
movw $0x3c, 0x2(%rbx)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movl $0x1, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq *0x48(%rcx,%rax,4)
movzwl 0x2(%rbx), %eax
rolw $0x8, %ax
leaq 0x6(%rsp), %r13
movw %ax, (%r13)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movl $0x2, %edx
movq %r14, %rdi
movq %r13, %rsi
callq *0x48(%rcx,%rax,4)
movq %r12, %rdi
callq 0x3180
movq %rax, %r15
rolw $0x8, %ax
movw %ax, (%r13)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movl $0x2, %edx
movq %r14, %rdi
movq %r13, %rsi
callq *0x48(%rcx,%rax,4)
movzwl %r15w, %r13d
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq *0x48(%rcx,%rax,4)
movb (%rbx), %al
testb $0x4, %al
jne 0x11859
addl $0xc, %r15d
jmp 0x11986
movq 0x8(%rbx), %rdi
callq 0x3180
movq %rax, %r12
movl %r12d, %ecx
movq %rax, 0x8(%rsp)
rolw $0x8, %cx
leaq 0x6(%rsp), %r13
movw %cx, (%r13)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movl $0x2, %edx
movq %r14, %rdi
movq %r13, %rsi
callq *0x48(%rcx,%rax,4)
movq 0x8(%rbx), %rbp
movl $0xffff, %eax           # imm = 0xFFFF
andq %rax, %r12
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movq %r14, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq *0x48(%rcx,%rax,4)
movq 0x10(%rbx), %rdi
callq 0x3180
movq %rax, %r13
rolw $0x8, %ax
leaq 0x6(%rsp), %r12
movw %ax, (%r12)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movl $0x2, %edx
movq %r14, %rdi
movq %r12, %rsi
callq *0x48(%rcx,%rax,4)
movq 0x10(%rbx), %r12
movq %r13, %rbp
movl $0xffff, %eax           # imm = 0xFFFF
andq %rax, %rbp
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq *0x48(%rcx,%rax,4)
movq 0x8(%rsp), %rax
addl %r15d, %eax
leal (%rax,%r13), %r15d
addl $0x10, %r15d
movb (%rbx), %al
testb %al, %al
jns 0x11a1b
movq 0x18(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0x6(%rsp), %r13
movw %ax, (%r13)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movl $0x2, %edx
movq %r14, %rdi
movq %r13, %rsi
callq *0x48(%rcx,%rax,4)
movq 0x18(%rbx), %r13
movzwl %r12w, %ebp
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq *0x48(%rcx,%rax,4)
addl %r12d, %r15d
addl $0x2, %r15d
movb (%rbx), %al
testb $0x40, %al
je 0x11aae
movq 0x20(%rbx), %rdi
callq 0x3180
movq %rax, %r12
rolw $0x8, %ax
leaq 0x6(%rsp), %r13
movw %ax, (%r13)
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movl $0x2, %edx
movq %r14, %rdi
movq %r13, %rsi
callq *0x48(%rcx,%rax,4)
movq 0x20(%rbx), %r13
movzwl %r12w, %ebp
callq 0x4aab
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%r14)
movl 0xc8(%r14), %eax
andl $0x2, %eax
movq 0xc0(%r14), %rcx
movq 0x10(%rcx), %rcx
movq %r14, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq *0x48(%rcx,%rax,4)
addl %r12d, %r15d
addl $0x2, %r15d
movzwl %r15w, %ecx
movq %r14, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x11ae1
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x11ad2
movzwl 0x2(%rbx), %eax
movw %ax, (%rcx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq