#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 ws_client {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ generate_upgrade_response_ws(struct ws_client *, char *);
scalar_t__ readline_client_ws(struct ws_client *);
char *strdup(char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ *strstr(char const *, char *);
scalar_t__ upgrade_client_ws(struct ws_client *);
int parse_http_proto_ws(struct ws_client *c) {
  char *line, *key, *proto;
  key = NULL;
  proto = NULL;
  while ((line = (char *)readline_client_ws(c)) != NULL) {
    if (strstr((const char *)line, "Sec-WebSocket-Key: ") != NULL) {
      key = strdup(line + strlen("Sec-WebSocket-Key: "));
    } else if (strstr((const char *)line, "Sec-WebSocket-Protocol: ") != NULL) {
      proto = strdup(line + strlen("Sec-WebSocket-Protocol: "));
    }
  }
  if (key != NULL && proto != NULL &&
      strncmp(proto, "katcp", strlen("katcp")) == 0) {
    if (proto != NULL) {
      free(proto);
      proto = NULL;
    }
    if (generate_upgrade_response_ws(c, key) < 0) {
      if (key != NULL) {
        free(key);
        key = NULL;
      }
      return -1;
    }
    if (key != NULL) {
      free(key);
      key = NULL;
    }
    if (upgrade_client_ws(c) < 0) {
      return -1;
    }
  } else {
    if (proto != NULL) {
      free(proto);
      proto = NULL;
    }
    if (key != NULL) {
      free(key);
      key = NULL;
    }
    return -1;
  }
  if (proto != NULL) {
    free(proto);
    proto = NULL;
  }
  if (key != NULL) {
    free(key);
    key = NULL;
  }
  return 0;
}


x86 assembly (clang, -O0):
<parse_http_proto_ws>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <readline_client_ws@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1242 <parse_http_proto_ws+0xc2>
mov    -0x18(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     11f8 <parse_http_proto_ws+0x78>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe24(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
call   1090 <strdup@plt>
mov    %rax,-0x20(%rbp)
jmp    123d <parse_http_proto_ws+0xbd>
mov    -0x18(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   10a0 <strstr@plt>
cmp    $0x0,%rax
je     1238 <parse_http_proto_ws+0xb8>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xdf3(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
call   1090 <strdup@plt>
mov    %rax,-0x28(%rbp)
jmp    123d <parse_http_proto_ws+0xbd>
jmp    119c <parse_http_proto_ws+0x1c>
cmpq   $0x0,-0x20(%rbp)
je     1323 <parse_http_proto_ws+0x1a3>
cmpq   $0x0,-0x28(%rbp)
je     1323 <parse_http_proto_ws+0x1a3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    0xdc6(%rip),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xdb4(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1323 <parse_http_proto_ws+0x1a3>
cmpq   $0x0,-0x28(%rbp)
je     12a4 <parse_http_proto_ws+0x124>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <generate_upgrade_response_ws@plt>
cmp    $0x0,%rax
jge    12e3 <parse_http_proto_ws+0x163>
cmpq   $0x0,-0x20(%rbp)
je     12d7 <parse_http_proto_ws+0x157>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a6 <parse_http_proto_ws+0x226>
cmpq   $0x0,-0x20(%rbp)
je     12ff <parse_http_proto_ws+0x17f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <upgrade_client_ws@plt>
cmp    $0x0,%rax
jge    131e <parse_http_proto_ws+0x19e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a6 <parse_http_proto_ws+0x226>
jmp    1367 <parse_http_proto_ws+0x1e7>
cmpq   $0x0,-0x28(%rbp)
je     133f <parse_http_proto_ws+0x1bf>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     135b <parse_http_proto_ws+0x1db>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a6 <parse_http_proto_ws+0x226>
cmpq   $0x0,-0x28(%rbp)
je     1383 <parse_http_proto_ws+0x203>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     139f <parse_http_proto_ws+0x21f>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_http_proto_ws>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r13
call   1050 <readline_client_ws@plt>
mov    %rax,%r14
mov    $0xffffffff,%eax
test   %r14,%r14
je     126c <parse_http_proto_ws+0xec>
xor    %r12d,%r12d
lea    0xe52(%rip),%rbx
lea    0xe5f(%rip),%rbp
xor    %r15d,%r15d
jmp    11e8 <parse_http_proto_ws+0x68>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
add    %rax,%r14
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1050 <readline_client_ws@plt>
mov    %rax,%r14
test   %rax,%rax
je     1222 <parse_http_proto_ws+0xa2>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
jne    11c0 <parse_http_proto_ws+0x40>
mov    %r14,%rdi
mov    %rbp,%rsi
call   10a0 <strstr@plt>
test   %rax,%rax
je     11d8 <parse_http_proto_ws+0x58>
mov    %rbp,%rdi
call   1060 <strlen@plt>
cltq
add    %rax,%r14
mov    %r14,%rdi
call   1090 <strdup@plt>
mov    %rax,%r12
jmp    11d8 <parse_http_proto_ws+0x58>
test   %r15,%r15
je     124d <parse_http_proto_ws+0xcd>
test   %r12,%r12
je     124d <parse_http_proto_ws+0xcd>
lea    0xdfa(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     127b <parse_http_proto_ws+0xfb>
test   %r12,%r12
je     125a <parse_http_proto_ws+0xda>
mov    %r12,%rdi
call   1030 <free@plt>
test   %r15,%r15
je     1267 <parse_http_proto_ws+0xe7>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <generate_upgrade_response_ws@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <free@plt>
test   %rbx,%rbx
mov    $0xffffffff,%eax
js     126c <parse_http_proto_ws+0xec>
mov    %r13,%rdi
call   1080 <upgrade_client_ws@plt>
sar    $0x3f,%rax
jmp    126c <parse_http_proto_ws+0xec>

LLVM IR (-O0):
%struct.ws_client = type { i32 }
@.str = [20 x i8] c"Sec-WebSocket-Key: \00"
@.str.1 = [25 x i8] c"Sec-WebSocket-Protocol: \00"
@.str.2 = [6 x i8] c"katcp\00"
define i32 @parse_http_proto_ws(%struct.ws_client* %0) {
%2 = alloca i32
%3 = alloca %struct.ws_client*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store %struct.ws_client* %0, %struct.ws_client** %3
store i8* null, i8** %5
store i8* null, i8** %6
br label %7
7: ; preds = %33, %1
%8 = load %struct.ws_client*, %struct.ws_client** %3
%9 = call i64 @readline_client_ws(%struct.ws_client* %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %34
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = call i32* @strstr(i8* %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i8* @strdup(i8* %20)
store i8* %21, i8** %5
br label %33
22: ; preds = %12
%23 = load i8*, i8** %4
%24 = call i32* @strstr(i8* %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i8*, i8** %4
%28 = call i32 @strlen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = call i8* @strdup(i8* %30)
store i8* %31, i8** %6
br label %32
32: ; preds = %26, %22
br label %33
33: ; preds = %32, %16
br label %7
34: ; preds = %7
%35 = load i8*, i8** %5
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %75
37: ; preds = %34
%38 = load i8*, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %75
40: ; preds = %37
%41 = load i8*, i8** %6
%42 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%43 = call i64 @strncmp(i8* %41, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %75
45: ; preds = %40
%46 = load i8*, i8** %6
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
store i8* null, i8** %6
br label %51
51: ; preds = %48, %45
%52 = load %struct.ws_client*, %struct.ws_client** %3
%53 = load i8*, i8** %5
%54 = call i64 @generate_upgrade_response_ws(%struct.ws_client* %52, i8* %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %51
%57 = load i8*, i8** %5
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i8*, i8** %5
%61 = call i32 @free(i8* %60)
store i8* null, i8** %5
br label %62
62: ; preds = %59, %56
store i32 -1, i32* %2
br label %101
63: ; preds = %51
%64 = load i8*, i8** %5
%65 = icmp ne i8* %64, null
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = load i8*, i8** %5
%68 = call i32 @free(i8* %67)
store i8* null, i8** %5
br label %69
69: ; preds = %66, %63
%70 = load %struct.ws_client*, %struct.ws_client** %3
%71 = call i64 @upgrade_client_ws(%struct.ws_client* %70)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %69
store i32 -1, i32* %2
br label %101
74: ; preds = %69
br label %88
75: ; preds = %40, %37, %34
%76 = load i8*, i8** %6
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i8*, i8** %6
%80 = call i32 @free(i8* %79)
store i8* null, i8** %6
br label %81
81: ; preds = %78, %75
%82 = load i8*, i8** %5
%83 = icmp ne i8* %82, null
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = load i8*, i8** %5
%86 = call i32 @free(i8* %85)
store i8* null, i8** %5
br label %87
87: ; preds = %84, %81
store i32 -1, i32* %2
br label %101
88: ; preds = %74
%89 = load i8*, i8** %6
%90 = icmp ne i8* %89, null
br i1 %90, label %91, label %94
91: ; preds = %88
%92 = load i8*, i8** %6
%93 = call i32 @free(i8* %92)
store i8* null, i8** %6
br label %94
94: ; preds = %91, %88
%95 = load i8*, i8** %5
%96 = icmp ne i8* %95, null
br i1 %96, label %97, label %100
97: ; preds = %94
%98 = load i8*, i8** %5
%99 = call i32 @free(i8* %98)
store i8* null, i8** %5
br label %100
100: ; preds = %97, %94
store i32 0, i32* %2
br label %101
101: ; preds = %100, %87, %73, %62
%102 = load i32, i32* %2
ret i32 %102
}

LLVM IR (-O2):
%struct.ws_client = type { i32 }
@.str = [20 x i8] c"Sec-WebSocket-Key: \00"
@.str.1 = [25 x i8] c"Sec-WebSocket-Protocol: \00"
@.str.2 = [6 x i8] c"katcp\00"
define i32 @parse_http_proto_ws(%struct.ws_client* %0) {
%2 = tail call i64 @readline_client_ws(%struct.ws_client* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %52, label %4
4: ; preds = %1, %24
%5 = phi i64 [ %27, %24 ], [ %2, %1 ]
%6 = phi i8* [ %26, %24 ], [ null, %1 ]
%7 = phi i8* [ %25, %24 ], [ null, %1 ]
%8 = inttoptr i64 %5 to i8*
%9 = tail call i32* @strstr(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %16, label %11
11: ; preds = %4
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %8, i64 %13
%15 = tail call i8* @strdup(i8* %14)
br label %24
16: ; preds = %4
%17 = tail call i32* @strstr(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = tail call i32 @strlen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %8, i64 %21
%23 = tail call i8* @strdup(i8* %22)
br label %24
24: ; preds = %16, %19, %11
%25 = phi i8* [ %15, %11 ], [ %7, %19 ], [ %7, %16 ]
%26 = phi i8* [ %6, %11 ], [ %23, %19 ], [ %6, %16 ]
%27 = tail call i64 @readline_client_ws(%struct.ws_client* %0)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %4
29: ; preds = %24
%30 = icmp ne i8* %25, null
%31 = icmp ne i8* %26, null
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %33, label %46
33: ; preds = %29
%34 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i64 @strncmp(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %33
%38 = tail call i32 @free(i8* %26)
%39 = tail call i64 @generate_upgrade_response_ws(%struct.ws_client* %0, i8* %25)
%40 = icmp slt i64 %39, 0
%41 = tail call i32 @free(i8* %25)
br i1 %40, label %52, label %42
42: ; preds = %37
%43 = tail call i64 @upgrade_client_ws(%struct.ws_client* %0)
%44 = ashr i64 %43, 63
%45 = trunc i64 %44 to i32
br label %52
46: ; preds = %33, %29
br i1 %31, label %47, label %49
47: ; preds = %46
%48 = tail call i32 @free(i8* %26)
br i1 %30, label %50, label %52
49: ; preds = %46
br i1 %30, label %50, label %52
50: ; preds = %47, %49
%51 = tail call i32 @free(i8* %25)
br label %52
52: ; preds = %37, %1, %47, %42, %49, %50
%53 = phi i32 [ -1, %50 ], [ -1, %49 ], [ %45, %42 ], [ -1, %47 ], [ -1, %1 ], [ -1, %37 ]
ret i32 %53
}