#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*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohl(int);
int /*<<< orphan*/ onIndexFilesRequest(int);
int /*<<< orphan*/ onIndexLoadRequest(int);
int /*<<< orphan*/ onQueryEvalRequest(int);
int /*<<< orphan*/ onTestConnectionRequest(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_socket(int, char *, int);
scalar_t__ strcmp(char *, char *);
void onAccept(int socketfd) {
  int messageLength, numbytes;
  read_socket(socketfd, (char *)&messageLength, sizeof(int));
  messageLength = ntohl(messageLength);
  printf("Message size: %d\n", messageLength);
  char action[messageLength + 1];
  memset(action, 0, messageLength + 1);
  if ((numbytes = read_socket(socketfd, action, messageLength)) == -1) {
    perror("recv");
    exit(1);
  }
  action[numbytes] = '\0';
  printf("Action received: %s\n", action);
  if (strcmp(action, "0") == 0) {
    printf("Closing connection on socket %d\n", socketfd);
  } else if (strcmp(action, "I_L") == 0) {
    onIndexLoadRequest(socketfd);
  } else if (strcmp(action, "I_F") == 0) {
    onIndexFilesRequest(socketfd);
  } else if (strcmp(action, "EVA") == 0) {
    onQueryEvalRequest(socketfd);
  } else if (strcmp(action, "TEST") == 0) {
    onTestConnectionRequest(socketfd);
  } else {
    printf("No action\n");
  }
  close(socketfd);
}


x86 assembly (clang, -O0):
<onAccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read_socket@plt>
mov    -0x8(%rbp),%edi
call   10e0 <ntohl@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe0f(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x28(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1040 <read_socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    125f <onAccept+0x9f>
lea    0xdc2(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rsi
movslq -0xc(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xda8(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12a9 <onAccept+0xe9>
mov    -0x4(%rbp),%esi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1367 <onAccept+0x1a7>
mov    -0x28(%rbp),%rdi
lea    0xd9b(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12d0 <onAccept+0x110>
mov    -0x4(%rbp),%edi
call   10c0 <onIndexLoadRequest@plt>
jmp    1362 <onAccept+0x1a2>
mov    -0x28(%rbp),%rdi
lea    0xd78(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    12f7 <onAccept+0x137>
mov    -0x4(%rbp),%edi
call   1060 <onIndexFilesRequest@plt>
jmp    135d <onAccept+0x19d>
mov    -0x28(%rbp),%rdi
lea    0xd55(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    131e <onAccept+0x15e>
mov    -0x4(%rbp),%edi
call   1090 <onQueryEvalRequest@plt>
jmp    1358 <onAccept+0x198>
mov    -0x28(%rbp),%rdi
lea    0xd32(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1345 <onAccept+0x185>
mov    -0x4(%rbp),%edi
call   10a0 <onTestConnectionRequest@plt>
jmp    1353 <onAccept+0x193>
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1358 <onAccept+0x198>
jmp    135d <onAccept+0x19d>
jmp    1362 <onAccept+0x1a2>
jmp    1367 <onAccept+0x1a7>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<onAccept>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read_socket@plt>
mov    -0x1c(%rbp),%edi
call   10f0 <ntohl@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe03(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%r15
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x1c(%rbp),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1050 <read_socket@plt>
cmp    $0xffffffff,%eax
je     131f <onAccept+0x14f>
cltq
movb   $0x0,(%rbx,%rax,1)
lea    0xdc3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdc7(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12d0 <onAccept+0x100>
lea    0xdd6(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12e3 <onAccept+0x113>
lea    0xdc6(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12ed <onAccept+0x11d>
lea    0xdb6(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     12f7 <onAccept+0x127>
lea    0xda6(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1301 <onAccept+0x131>
lea    0xd97(%rip),%rdi
call   1030 <puts@plt>
jmp    1309 <onAccept+0x139>
lea    0xd57(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1309 <onAccept+0x139>
mov    %r14d,%edi
call   10d0 <onIndexLoadRequest@plt>
jmp    1309 <onAccept+0x139>
mov    %r14d,%edi
call   1070 <onIndexFilesRequest@plt>
jmp    1309 <onAccept+0x139>
mov    %r14d,%edi
call   10a0 <onQueryEvalRequest@plt>
jmp    1309 <onAccept+0x139>
mov    %r14d,%edi
call   10b0 <onTestConnectionRequest@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    %r15,%rsp
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcec(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Message size: %d\0A\00"
@.str.1 = [5 x i8] c"recv\00"
@.str.2 = [21 x i8] c"Action received: %s\0A\00"
@.str.3 = [2 x i8] c"0\00"
@.str.4 = [33 x i8] c"Closing connection on socket %d\0A\00"
@.str.5 = [4 x i8] c"I_L\00"
@.str.6 = [4 x i8] c"I_F\00"
@.str.7 = [4 x i8] c"EVA\00"
@.str.8 = [5 x i8] c"TEST\00"
@.str.9 = [11 x i8] c"No action\0A\00"
define void @onAccept(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = bitcast i32* %3 to i8*
%9 = call i32 @read_socket(i32 %7, i8* %8, i32 4)
%10 = load i32, i32* %3
%11 = call i32 @ntohl(i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = add nsw i32 %14, 1
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %5
%18 = alloca i8, i64 %16
store i64 %16, i64* %6
%19 = load i32, i32* %3
%20 = add nsw i32 %19, 1
%21 = call i32 @memset(i8* %18, i32 0, i32 %20)
%22 = load i32, i32* %2
%23 = load i32, i32* %3
%24 = call i32 @read_socket(i32 %22, i8* %18, i32 %23)
store i32 %24, i32* %4
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %1
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %1
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %18, i64 %31
store i8 0, i8* %32
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%34 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* %2
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %37)
br label %69
39: ; preds = %29
%40 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* %2
%44 = call i32 @onIndexLoadRequest(i32 %43)
br label %68
45: ; preds = %39
%46 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %2
%50 = call i32 @onIndexFilesRequest(i32 %49)
br label %67
51: ; preds = %45
%52 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* %2
%56 = call i32 @onQueryEvalRequest(i32 %55)
br label %66
57: ; preds = %51
%58 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load i32, i32* %2
%62 = call i32 @onTestConnectionRequest(i32 %61)
br label %65
63: ; preds = %57
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
br label %66
66: ; preds = %65, %54
br label %67
67: ; preds = %66, %48
br label %68
68: ; preds = %67, %42
br label %69
69: ; preds = %68, %36
%70 = load i32, i32* %2
%71 = call i32 @close(i32 %70)
%72 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"Message size: %d\0A\00"
@.str.1 = [5 x i8] c"recv\00"
@.str.2 = [21 x i8] c"Action received: %s\0A\00"
@.str.3 = [2 x i8] c"0\00"
@.str.4 = [33 x i8] c"Closing connection on socket %d\0A\00"
@.str.5 = [4 x i8] c"I_L\00"
@.str.6 = [4 x i8] c"I_F\00"
@.str.7 = [4 x i8] c"EVA\00"
@.str.8 = [5 x i8] c"TEST\00"
@str = [10 x i8] c"No action\00"
define void @onAccept(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @read_socket(i32 %0, i8* %3, i32 4)
%5 = load i32, i32* %2
%6 = call i32 @ntohl(i32 %5)
store i32 %6, i32* %2
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* %2
%9 = add nsw i32 %8, 1
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
%15 = call i32 @memset(i8* %12, i32 0, i32 %14)
%16 = load i32, i32* %2
%17 = call i32 @read_socket(i32 %0, i8* %12, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = sext i32 %17 to i64
%24 = getelementptr inbounds i8, i8* %12, i64 %23
store i8 0, i8* %24
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%26 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br label %52
30: ; preds = %22
%31 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @onIndexLoadRequest(i32 %0)
br label %52
35: ; preds = %30
%36 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @onIndexFilesRequest(i32 %0)
br label %52
40: ; preds = %35
%41 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @onQueryEvalRequest(i32 %0)
br label %52
45: ; preds = %40
%46 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @onTestConnectionRequest(i32 %0)
br label %52
50: ; preds = %45
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %52
52: ; preds = %33, %43, %50, %48, %38, %28
%53 = call i32 @close(i32 %0)
ret void
}