#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 */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *ws_answer_key(char *);
char *ws_answer_frame(char *key) {
  static char answer_frame_tmpl[] = "HTTP/1.1 101 Switching Protocols\r\n"
                                    "Upgrade: websocket\r\n"
                                    "Connection: Upgrade\r\n"
                                    "Sec-WebSocket-Accept: %s\r\n\r\n";
  char *answer = (char *)malloc(256);
  if (!answer)
    return NULL;
  memset(answer, 0, 256);
  char *answer_key = ws_answer_key(key);
  if (!answer_key) {
    free(answer);
    return NULL;
  }
  sprintf(answer, answer_frame_tmpl, answer_key);
  free(answer_key);
  return answer;
}


x86 assembly (clang, -O0):
<ws_answer_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1182 <ws_answer_frame+0x32>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <ws_answer_frame+0x95>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1040 <ws_answer_key@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <ws_answer_frame+0x70>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e5 <ws_answer_frame+0x95>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0x2e81(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ws_answer_frame>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
mov    $0x100,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ae <ws_answer_frame+0x5e>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    %r15,%rdi
call   1040 <ws_answer_key@plt>
test   %rax,%rax
je     11a4 <ws_answer_frame+0x54>
mov    %rax,%r15
mov    %rbx,%r14
lea    0x2eba(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
call   1070 <sprintf@plt>
mov    %r15,%rbx
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11b1 <ws_answer_frame+0x61>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@ws_answer_frame.answer_frame_tmpl = internal global [104 x i8] c"HTTP/1.1 101 Switching Protocols\0D\0AUpgrade: websocket\0D\0AConnection: Upgrade\0D\0ASec-WebSocket-Accept: %s\0D\0A\0D\0A\00"
define i8* @ws_answer_frame(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = call i64 @malloc(i32 256)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %28
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = call i32 @memset(i8* %12, i32 0, i32 256)
%14 = load i8*, i8** %3
%15 = call i8* @ws_answer_key(i8* %14)
store i8* %15, i8** %5
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %21, label %18
18: ; preds = %11
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
store i8* null, i8** %2
br label %28
21: ; preds = %11
%22 = load i8*, i8** %4
%23 = load i8*, i8** %5
%24 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([104 x i8], [104 x i8]* @ws_answer_frame.answer_frame_tmpl, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %4
store i8* %27, i8** %2
br label %28
28: ; preds = %21, %18, %10
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@ws_answer_frame.answer_frame_tmpl = internal global [104 x i8] c"HTTP/1.1 101 Switching Protocols\0D\0AUpgrade: websocket\0D\0AConnection: Upgrade\0D\0ASec-WebSocket-Accept: %s\0D\0A\0D\0A\00"
define i8* @ws_answer_frame(i8* %0) {
%2 = tail call i64 @malloc(i32 256)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %15, label %5
5: ; preds = %1
%6 = tail call i32 @memset(i8* %3, i32 0, i32 256)
%7 = tail call i8* @ws_answer_key(i8* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([104 x i8], [104 x i8]* @ws_answer_frame.answer_frame_tmpl, i64 0, i64 0), i8* %7)
br label %11
11: ; preds = %5, %9
%12 = phi i8* [ %7, %9 ], [ %3, %5 ]
%13 = phi i8* [ %3, %9 ], [ null, %5 ]
%14 = tail call i32 @free(i8* %12)
br label %15
15: ; preds = %11, %1
%16 = phi i8* [ null, %1 ], [ %13, %11 ]
ret i8* %16
}