#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
char *base64_encode(unsigned char *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ sha1_csum(unsigned char *, int, unsigned char *);
char *ws_compute_key(const char *key, unsigned int key_len) {
  unsigned char digest[20];
  char out[128];
  char *b64;
  if (key_len > 32) {
    return NULL;
  }
  memcpy(out, key, key_len);
  memcpy(out + key_len, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
         sizeof("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") - 1);
  sha1_csum((unsigned char *)out,
            (sizeof("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") - 1) + key_len,
            digest);
  b64 = base64_encode(digest, 20);
  return b64;
}


x86 assembly (clang, -O0):
<ws_compute_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x20,-0x14(%rbp)
jbe    1159 <ws_compute_key+0x29>
movq   $0x0,-0x8(%rbp)
jmp    11c3 <ws_compute_key+0x93>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <memcpy@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    %rax,%rdi
lea    0xe80(%rip),%rsi
mov    $0x24,%edx
call   1030 <memcpy@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    $0x24,%rax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
call   1040 <sha1_csum@plt>
lea    -0x30(%rbp),%rdi
mov    $0x14,%esi
call   1050 <base64_encode@plt>
mov    %rax,-0xb8(%rbp)
mov    -0xb8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ws_compute_key>:
push   %r15
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    %esi,%ebx
cmp    $0x20,%esi
jbe    1147 <ws_compute_key+0x17>
xor    %eax,%eax
jmp    1194 <ws_compute_key+0x64>
mov    %rdi,%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    %ebx,%eax
lea    (%rsp,%rax,1),%rdi
add    $0x20,%rdi
lea    0xe96(%rip),%rsi
mov    $0x24,%edx
call   1030 <memcpy@plt>
add    $0x24,%ebx
mov    %rsp,%r15
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
call   1040 <sha1_csum@plt>
mov    %r15,%rdi
mov    $0x14,%esi
call   1050 <base64_encode@plt>
add    $0xa0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [37 x i8] c"258EAFA5-E914-47DA-95CA-C5AB0DC85B11\00"
define i8* @ws_compute_key(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [20 x i8]
%7 = alloca [128 x i8]
%8 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = icmp ugt i32 %9, 32
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %32
12: ; preds = %2
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%14 = load i8*, i8** %4
%15 = load i32, i32* %5
%16 = call i32 @memcpy(i8* %13, i8* %14, i32 %15)
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%18 = load i32, i32* %5
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @memcpy(i8* %20, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 36)
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* %5
%24 = zext i32 %23 to i64
%25 = add i64 36, %24
%26 = trunc i64 %25 to i32
%27 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%28 = call i32 @sha1_csum(i8* %22, i32 %26, i8* %27)
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%30 = call i8* @base64_encode(i8* %29, i32 20)
store i8* %30, i8** %8
%31 = load i8*, i8** %8
store i8* %31, i8** %3
br label %32
32: ; preds = %12, %11
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@.str = [37 x i8] c"258EAFA5-E914-47DA-95CA-C5AB0DC85B11\00"
define i8* @ws_compute_key(i8* %0, i32 %1) {
%3 = alloca [20 x i8]
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = icmp ugt i32 %1, 32
br i1 %7, label %16, label %8
8: ; preds = %2
%9 = call i32 @memcpy(i8* %6, i8* %0, i32 %1)
%10 = zext i32 %1 to i64
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %10
%12 = call i32 @memcpy(i8* %11, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 36)
%13 = add nuw nsw i32 %1, 36
%14 = call i32 @sha1_csum(i8* %6, i32 %13, i8* %5)
%15 = call i8* @base64_encode(i8* %5, i32 20)
br label %16
16: ; preds = %2, %8
%17 = phi i8* [ %15, %8 ], [ null, %2 ]
ret i8* %17
}