// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
leaq 0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movw $0x0, -0x16(%rbp)
movq -0x8(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x7f10(%rip), %rsi      # 0x1f338
movl $0x7, %edx
callq 0xb260
movq -0x28(%rbp), %rax
movzwl -0x16(%rbp), %ecx
addl $0x7, %ecx
movw %cx, -0x16(%rbp)
cmpq $0x0, 0x18(%rax)
je 0x17457
movq -0x28(%rbp), %rax
movzbl (%rax), %ecx
orl $0x80, %ecx
movb %cl, (%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x1746e
movq -0x28(%rbp), %rax
movzbl (%rax), %ecx
orl $0x40, %ecx
movb %cl, (%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x17490
movq -0x28(%rbp), %rax
cmpq $0x0, 0x10(%rax)
je 0x17490
movq -0x28(%rbp), %rax
movzbl (%rax), %ecx
orl $0x4, %ecx
movb %cl, (%rax)
movq -0x28(%rbp), %rax
movzwl 0x2(%rax), %eax
cmpl $0x0, %eax
jne 0x174a7
movq -0x28(%rbp), %rax
movw $0x3c, 0x2(%rax)
movq -0x28(%rbp), %rsi
movq -0x8(%rbp), %rdi
movl $0x1, %edx
callq 0xb260
movq -0x28(%rbp), %rax
movzwl -0x16(%rbp), %ecx
addl $0x1, %ecx
movw %cx, -0x16(%rbp)
movzwl 0x2(%rax), %edi
callq 0x31a0
movw %ax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
movl $0x2, %edx
callq 0xb260
movzwl -0x16(%rbp), %eax
addl $0x2, %eax
movw %ax, -0x16(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3180
movw %ax, -0x12(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x31a0
movw %ax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
movl $0x2, %edx
callq 0xb260
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movzwl -0x12(%rbp), %edx
callq 0xb260
movq -0x28(%rbp), %rax
movzwl -0x12(%rbp), %edx
addl $0x2, %edx
movzwl -0x16(%rbp), %ecx
addl %edx, %ecx
movw %cx, -0x16(%rbp)
movzbl (%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x175ff
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x3180
movw %ax, -0x12(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x31a0
movw %ax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
movl $0x2, %edx
callq 0xb260
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rdi
movq 0x8(%rax), %rsi
movzwl -0x12(%rbp), %edx
callq 0xb260
movq -0x28(%rbp), %rax
movzwl -0x12(%rbp), %edx
addl $0x2, %edx
movzwl -0x16(%rbp), %ecx
addl %edx, %ecx
movw %cx, -0x16(%rbp)
movq 0x10(%rax), %rdi
callq 0x3180
movw %ax, -0x12(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x31a0
movw %ax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
movl $0x2, %edx
callq 0xb260
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rdi
movq 0x10(%rax), %rsi
movzwl -0x12(%rbp), %edx
callq 0xb260
movzwl -0x12(%rbp), %ecx
addl $0x2, %ecx
movzwl -0x16(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x16(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x17666
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x3180
movw %ax, -0x12(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x31a0
movw %ax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
movl $0x2, %edx
callq 0xb260
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rdi
movq 0x18(%rax), %rsi
movzwl -0x12(%rbp), %edx
callq 0xb260
movzwl -0x12(%rbp), %ecx
addl $0x2, %ecx
movzwl -0x16(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x16(%rbp)
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x176cb
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x3180
movw %ax, -0x12(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x31a0
movw %ax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x14(%rbp), %rsi
movl $0x2, %edx
callq 0xb260
movq -0x28(%rbp), %rax
movq -0x8(%rbp), %rdi
movq 0x20(%rax), %rsi
movzwl -0x12(%rbp), %edx
callq 0xb260
movzwl -0x12(%rbp), %ecx
addl $0x2, %ecx
movzwl -0x16(%rbp), %eax
addl %ecx, %eax
movw %ax, -0x16(%rbp)
movq -0x8(%rbp), %rdi
movzwl -0x16(%rbp), %eax
movl %eax, %ecx
movl $0x1, %esi
xorl %edx, %edx
callq 0x17700
cmpq $0x0, -0x20(%rbp)
je 0x176f7
movq -0x28(%rbp), %rax
movw 0x2(%rax), %cx
movq -0x20(%rbp), %rax
movw %cx, (%rax)
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)