#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 mg_connection {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
char *mg_get_header(struct mg_connection *, char *);
int /*<<< orphan*/ mg_printf(struct mg_connection *, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
char *strrchr(char *, char);
void websocket_ready_handler(struct mg_connection *conn) {
  const char *prot = mg_get_header(conn, "Sec-WebSocket-Protocol");
  char buf[100];
  if (prot) {
    char *p = NULL;
    snprintf(buf, sizeof(buf), "%s", prot);
    if ((p = strrchr(buf, ',')) != NULL) {
      *p = '\0';
    }
    mg_printf(conn, "Sec-WebSocket-Protocol: %s\r\n", buf);
  }
  mg_printf(conn, "\r\n");
}


x86 assembly (clang, -O0):
<websocket_ready_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1030 <mg_get_header@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d1 <websocket_ready_handler+0x91>
movq   $0x0,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x64,%esi
lea    0xe8a(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x80(%rbp),%rdi
mov    $0x2c,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
je     11bb <websocket_ready_handler+0x7b>
mov    -0x88(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
lea    -0x80(%rbp),%rdx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1040 <mg_printf@plt>
mov    -0x8(%rbp),%rdi
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1040 <mg_printf@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<websocket_ready_handler>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%rbx
lea    0xeaf(%rip),%rsi
call   1030 <mg_get_header@plt>
test   %rax,%rax
je     119e <websocket_ready_handler+0x5e>
lea    0xeb5(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    %r14,%rdi
mov    $0x2c,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     118a <websocket_ready_handler+0x4a>
movb   $0x0,(%rax)
lea    0xe89(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <mg_printf@plt>
lea    0xe8f(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <mg_printf@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.mg_connection = type { i32 }
@.str = [23 x i8] c"Sec-WebSocket-Protocol\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [29 x i8] c"Sec-WebSocket-Protocol: %s\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
define void @websocket_ready_handler(%struct.mg_connection* %0) {
%2 = alloca %struct.mg_connection*
%3 = alloca i8*
%4 = alloca [100 x i8]
%5 = alloca i8*
store %struct.mg_connection* %0, %struct.mg_connection** %2
%6 = load %struct.mg_connection*, %struct.mg_connection** %2
%7 = call i8* @mg_get_header(%struct.mg_connection* %6, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %23
10: ; preds = %1
store i8* null, i8** %5
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%12 = load i8*, i8** %3
%13 = call i32 @snprintf(i8* %11, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%15 = call i8* @strrchr(i8* %14, i8 signext 44)
store i8* %15, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = load i8*, i8** %5
store i8 0, i8* %18
br label %19
19: ; preds = %17, %10
%20 = load %struct.mg_connection*, %struct.mg_connection** %2
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%22 = call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %1
%24 = load %struct.mg_connection*, %struct.mg_connection** %2
%25 = call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.mg_connection = type { i32 }
@.str = [23 x i8] c"Sec-WebSocket-Protocol\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [29 x i8] c"Sec-WebSocket-Protocol: %s\0D\0A\00"
@.str.3 = [3 x i8] c"\0D\0A\00"
define void @websocket_ready_handler(%struct.mg_connection* %0) {
%2 = alloca [100 x i8]
%3 = tail call i8* @mg_get_header(%struct.mg_connection* %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = icmp eq i8* %3, null
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = call i32 @snprintf(i8* %4, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%8 = call i8* @strrchr(i8* %4, i8 signext 44)
%9 = icmp eq i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %6
store i8 0, i8* %8
br label %11
11: ; preds = %10, %6
%12 = call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %13
13: ; preds = %11, %1
%14 = call i32 (%struct.mg_connection*, i8*, ...) @mg_printf(%struct.mg_connection* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
ret void
}