#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 */
struct ns_connection {
  int dummy;
};
typedef int /*<<< orphan*/ random;

/* Variables and functions */
int /*<<< orphan*/ ns_base64_encode(unsigned char *, int, char *);
int /*<<< orphan*/ ns_printf(struct ns_connection *, char *, char const *,
                             char *, char *);
void ns_send_websocket_handshake(struct ns_connection *nc, const char *uri,
                                 const char *extra_headers) {
  unsigned long random = (unsigned long)uri;
  char key[sizeof(random) * 3];
  ns_base64_encode((unsigned char *)&random, sizeof(random), key);
  ns_printf(nc,
            "GET %s HTTP/1.1\r\n"
            "Upgrade: websocket\r\n"
            "Connection: Upgrade\r\n"
            "Sec-WebSocket-Version: 13\r\n"
            "Sec-WebSocket-Key: %s\r\n"
            "%s\r\n",
            uri, key, extra_headers == NULL ? "" : extra_headers);
}


x86 assembly (clang, -O0):
<ns_send_websocket_handshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rdx
mov    $0x8,%esi
call   1030 <ns_base64_encode@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1181 <ns_send_websocket_handshake+0x61>
lea    0xef8(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    1189 <ns_send_websocket_handshake+0x69>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x48(%rbp),%rcx
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rdi
mov    -0x60(%rbp),%r8
lea    0xe60(%rip),%rsi
call   1040 <ns_printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ns_send_websocket_handshake>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%r12
mov    $0x8,%esi
mov    %r12,%rdx
call   1030 <ns_base64_encode@plt>
test   %rbx,%rbx
lea    0xf16(%rip),%r8
cmovne %rbx,%r8
lea    0xe9b(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
mov    %r12,%rcx
call   1040 <ns_printf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ns_connection = type { i32 }
@.str = [113 x i8] c"GET %s HTTP/1.1\0D\0AUpgrade: websocket\0D\0AConnection: Upgrade\0D\0ASec-WebSocket-Version: 13\0D\0ASec-WebSocket-Key: %s\0D\0A%s\0D\0A\00"
@.str.1 = [1 x i8] zeroinitializer
define void @ns_send_websocket_handshake(%struct.ns_connection* %0, i8* %1, i8* %2) {
%4 = alloca %struct.ns_connection*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [24 x i8]
store %struct.ns_connection* %0, %struct.ns_connection** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %5
%10 = ptrtoint i8* %9 to i64
store i64 %10, i64* %7
%11 = bitcast i64* %7 to i8*
%12 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%13 = call i32 @ns_base64_encode(i8* %11, i32 8, i8* %12)
%14 = load %struct.ns_connection*, %struct.ns_connection** %4
%15 = load i8*, i8** %5
%16 = getelementptr inbounds [24 x i8], [24 x i8]* %8, i64 0, i64 0
%17 = load i8*, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %3
br label %22
20: ; preds = %3
%21 = load i8*, i8** %6
br label %22
22: ; preds = %20, %19
%23 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %19 ], [ %21, %20 ]
%24 = call i32 @ns_printf(%struct.ns_connection* %14, i8* getelementptr inbounds ([113 x i8], [113 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16, i8* %23)
ret void
}

LLVM IR (-O2):
%struct.ns_connection = type { i32 }
@.str = [113 x i8] c"GET %s HTTP/1.1\0D\0AUpgrade: websocket\0D\0AConnection: Upgrade\0D\0ASec-WebSocket-Version: 13\0D\0ASec-WebSocket-Key: %s\0D\0A%s\0D\0A\00"
@.str.1 = [1 x i8] zeroinitializer
define void @ns_send_websocket_handshake(%struct.ns_connection* %0, i8* %1, i8* %2) {
%4 = alloca i64
%5 = alloca [24 x i8]
%6 = bitcast i64* %4 to i8*
%7 = ptrtoint i8* %1 to i64
store i64 %7, i64* %4
%8 = getelementptr inbounds [24 x i8], [24 x i8]* %5, i64 0, i64 0
%9 = call i32 @ns_base64_encode(i8* %6, i32 8, i8* %8)
%10 = icmp eq i8* %2, null
%11 = select i1 %10, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8* %2
%12 = call i32 @ns_printf(%struct.ns_connection* %0, i8* getelementptr inbounds ([113 x i8], [113 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %8, i8* %11)
ret void
}