// File: Computer-Kids-Club[P]screenshare-pro-backend/websocket.c
// Function: ws_compute_handshake(char const*, char*, unsigned long*)
int ws_compute_handshake(const char *key, char *out, size_t *out_sz) {
    unsigned char *buffer, sha1_output[20];
    char magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    SHA1Context ctx;
    base64_encodestate b64_ctx;
    int pos, i;

    // websocket handshake
    size_t key_sz = key ? strlen(key) : 0, buffer_sz = key_sz + sizeof(magic) - 1;
    buffer = (unsigned char *)calloc(buffer_sz, 1);

    // concatenate key and guid in buffer
    memcpy(buffer, key, key_sz);
    memcpy(buffer + key_sz, magic, sizeof(magic) - 1);

    // compute sha-1
    SHA1Reset(&ctx);
    SHA1Input(&ctx, buffer, buffer_sz);
    SHA1Result(&ctx);
    for (i = 0; i < 5; ++i) {  // put in correct byte order before memcpy.
        ctx.Message_Digest[i] = ntohl(ctx.Message_Digest[i]);
    }
    memcpy(sha1_output, (unsigned char *)ctx.Message_Digest, 20);

    // encode `sha1_output' in base 64, into `out'.
    base64_init_encodestate(&b64_ctx);
    pos = base64_encode_block((const char *)sha1_output, 20, out, &b64_ctx);
    base64_encode_blockend(out + pos, &b64_ctx);

    // compute length, without \n
    *out_sz = strlen(out);
    if (out[*out_sz - 1] == '\n')
        (*out_sz)--;

    free(buffer);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x70(%rbp), %rdi
leaq 0xbbe(%rip), %rsi       # 0x115a0
movl $0x25, %edx
callq 0x32b0
cmpq $0x0, -0x8(%rbp)
je 0x10a05
movq -0x8(%rbp), %rdi
callq 0x3180
movq %rax, -0x108(%rbp)
jmp 0x10a10
xorl %eax, %eax
movq %rax, -0x108(%rbp)
jmp 0x10a10
movq -0x108(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0xf8(%rbp), %rax
addq $0x25, %rax
subq $0x1, %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
movl $0x1, %esi
callq 0x3240
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0xf8(%rbp), %rdx
callq 0x32b0
movq -0x20(%rbp), %rdi
addq -0xf8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x24, %edx
callq 0x32b0
leaq -0xd8(%rbp), %rdi
callq 0xf960
movq -0x20(%rbp), %rsi
movq -0x100(%rbp), %rax
movl %eax, %edx
leaq -0xd8(%rbp), %rdi
callq 0xfbd0
leaq -0xd8(%rbp), %rdi
callq 0xf9e0
movl $0x0, -0xec(%rbp)
cmpl $0x5, -0xec(%rbp)
jge 0x10aee
movslq -0xec(%rbp), %rax
movl -0xd8(%rbp,%rax,4), %edi
callq 0x33f0
movl %eax, %ecx
movslq -0xec(%rbp), %rax
movl %ecx, -0xd8(%rbp,%rax,4)
movl -0xec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xec(%rbp)
jmp 0x10ab1
movq -0xd8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x38(%rbp)
movl -0xc8(%rbp), %eax
movl %eax, -0x30(%rbp)
leaq -0xe4(%rbp), %rdi
callq 0xeaf0
leaq -0x40(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x14, %esi
leaq -0xe4(%rbp), %rcx
callq 0xeb60
movl %eax, -0xe8(%rbp)
movq -0x10(%rbp), %rdi
movslq -0xe8(%rbp), %rax
addq %rax, %rdi
leaq -0xe4(%rbp), %rsi
callq 0xedd0
movq -0x10(%rbp), %rdi
callq 0x3180
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0xa, %eax
jne 0x10b8b
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $-0x1, %rcx
movq %rcx, (%rax)
movq -0x20(%rbp), %rdi
callq 0x33a0
xorl %eax, %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nop