#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ SHA_CTX;

/* Variables and functions */
int /*<<< orphan*/ SHA1_Final(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, char const *, int);
int SHA_DIGEST_LENGTH;
char *base64_encode(int /*<<< orphan*/ *, int, char *);
char *websock_accept_key(char *__restrict dest, const char *__restrict sec_key,
                         size_t sec_key_len) {
  static const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  uint8_t digest[SHA_DIGEST_LENGTH];
  SHA_CTX ctx;
  SHA1_Init(&ctx);
  SHA1_Update(&ctx, sec_key, sec_key_len);
  SHA1_Update(&ctx, websocket_guid, sizeof websocket_guid - 1);
  SHA1_Final(digest, &ctx);
  return base64_encode(digest, sizeof digest, dest);
}


x86 assembly (clang, -O0):
<websock_accept_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
call   1050 <SHA1_Init@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
lea    -0x2c(%rbp),%rdi
call   1030 <SHA1_Update@plt>
lea    -0x2c(%rbp),%rdi
lea    0xe54(%rip),%rsi
mov    $0x24,%edx
call   1030 <SHA1_Update@plt>
mov    -0x40(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1060 <SHA1_Final@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
call   1040 <base64_encode@plt>
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<websock_accept_key>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,-0x30(%rbp)
mov    0x2e86(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(,%r14,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x34(%rbp),%r13
mov    %r13,%rdi
call   1050 <SHA1_Init@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <SHA1_Update@plt>
lea    0xe65(%rip),%rsi
mov    %r13,%rdi
mov    $0x24,%edx
call   1030 <SHA1_Update@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <SHA1_Final@plt>
lea    0x0(,%r14,4),%esi
mov    %rbx,%rdi
mov    -0x30(%rbp),%rdx
call   1040 <base64_encode@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@websock_accept_key.websocket_guid = internal constant [37 x i8] c"258EAFA5-E914-47DA-95CA-C5AB0DC85B11\00"
@SHA_DIGEST_LENGTH = global i32 0
define i8* @websock_accept_key(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i32, i32* @SHA_DIGEST_LENGTH
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i32, i64 %11
store i64 %11, i64* %8
%14 = call i32 @SHA1_Init(i32* %9)
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = trunc i64 %16 to i32
%18 = call i32 @SHA1_Update(i32* %9, i8* %15, i32 %17)
%19 = call i32 @SHA1_Update(i32* %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @websock_accept_key.websocket_guid, i64 0, i64 0), i32 36)
%20 = call i32 @SHA1_Final(i32* %13, i32* %9)
%21 = mul nuw i64 4, %11
%22 = trunc i64 %21 to i32
%23 = load i8*, i8** %4
%24 = call i8* @base64_encode(i32* %13, i32 %22, i8* %23)
%25 = load i8*, i8** %7
ret i8* %24
}

LLVM IR (-O2):
@websock_accept_key.websocket_guid = internal constant [37 x i8] c"258EAFA5-E914-47DA-95CA-C5AB0DC85B11\00"
@SHA_DIGEST_LENGTH = global i32 0
define i8* @websock_accept_key(i8* noalias %0, i8* noalias %1, i64 %2) {
%4 = alloca i32
%5 = load i32, i32* @SHA_DIGEST_LENGTH
%6 = zext i32 %5 to i64
%7 = alloca i32, i64 %6
%8 = bitcast i32* %4 to i8*
%9 = call i32 @SHA1_Init(i32* %4)
%10 = trunc i64 %2 to i32
%11 = call i32 @SHA1_Update(i32* %4, i8* %1, i32 %10)
%12 = call i32 @SHA1_Update(i32* %4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @websock_accept_key.websocket_guid, i64 0, i64 0), i32 36)
%13 = call i32 @SHA1_Final(i32* %7, i32* %4)
%14 = shl i32 %5, 2
%15 = call i8* @base64_encode(i32* %7, i32 %14, i8* %0)
ret i8* %15
}