#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server;
typedef int /*<<< orphan*/ messageBuffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ parseMessage(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
void listenOn(int port) {
  int serverfd, clientfd;
  struct sockaddr_in server, client;
  char messageBuffer[4096];
  serverfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (serverfd < 0) {
    printf("Error creating socket");
    return;
  }
  printf("Created socket\n");
  bzero(&server, sizeof(server));
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_family = AF_INET;
  server.sin_port = htons(port);
  if (bind(serverfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
    printf("Couldn't bind\n");
    return;
  }
  printf("Listening on port %d\n", port);
  for (;;) {
    clientfd = recv(serverfd, messageBuffer, sizeof(messageBuffer), 0);
    if (clientfd > 0) {
      if (strlen(messageBuffer) > 0) {
        parseMessage(messageBuffer);
        memset(messageBuffer, 0, sizeof(messageBuffer));
      }
    } else {
      printf("Couldn't recieve. Clientfd was %d\n", clientfd);
    }
  }
}


x86 assembly (clang, -O0):
<listenOn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d7 <listenOn+0x47>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12c8 <listenOn+0x138>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1244 <listenOn+0xb4>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12c8 <listenOn+0x138>
mov    -0x4(%rbp),%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    12b2 <listenOn+0x122>
lea    -0x1030(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jle    12ad <listenOn+0x11d>
lea    -0x1030(%rbp),%rdi
call   10a0 <parseMessage@plt>
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
jmp    12c3 <listenOn+0x133>
mov    -0xc(%rbp),%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1255 <listenOn+0xc5>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenOn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ebp
mov    0x2e32(%rip),%r15
mov    (%r15),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1297 <listenOn+0xf7>
mov    %eax,%ebx
lea    0xe75(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12b2 <listenOn+0x112>
lea    0xdea(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x10(%rsp),%rbp
lea    0xdeb(%rip),%r14
jmp    125c <listenOn+0xbc>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1250 <listenOn+0xb0>
mov    %rbp,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
jle    125c <listenOn+0xbc>
mov    %rbp,%rdi
call   10b0 <parseMessage@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
jmp    125c <listenOn+0xbc>
lea    0xd62(%rip),%rdi
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <printf@plt>
lea    0xda5(%rip),%rdi
call   1040 <puts@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"Error creating socket\00"
@.str.1 = [16 x i8] c"Created socket\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [15 x i8] c"Couldn't bind\0A\00"
@.str.3 = [22 x i8] c"Listening on port %d\0A\00"
@.str.4 = [35 x i8] c"Couldn't recieve. Clientfd was %d\0A\00"
define void @listenOn(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca [4096 x i8]
store i32 %0, i32* %2
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %55
15: ; preds = %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %2
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %3
%27 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %15
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %55
32: ; preds = %15
%33 = load i32, i32* %2
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %54, %32
%36 = load i32, i32* %3
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%38 = call i32 @recv(i32 %36, i8* %37, i32 4096, i32 0)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %35
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%43 = call i64 @strlen(i8* %42)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%47 = call i32 @parseMessage(i8* %46)
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%49 = call i32 @memset(i8* %48, i32 0, i32 4096)
br label %50
50: ; preds = %45, %41
br label %54
51: ; preds = %35
%52 = load i32, i32* %4
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %51, %50
br label %35
55: ; preds = %30, %13
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"Error creating socket\00"
@INADDR_ANY = global i32 0
@.str.3 = [22 x i8] c"Listening on port %d\0A\00"
@.str.4 = [35 x i8] c"Couldn't recieve. Clientfd was %d\0A\00"
@str = [15 x i8] c"Created socket\00"
@str.5 = [14 x i8] c"Couldn't bind\00"
define void @listenOn(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [4096 x i8]
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %40
12: ; preds = %1
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%14 = call i32 @bzero(%struct.sockaddr_in* %2, i32 12)
%15 = load i32, i32* @INADDR_ANY
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i64 @bind(i32 %8, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %40
26: ; preds = %12
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %28
28: ; preds = %34, %26
%29 = call i32 @recv(i32 %8, i8* %5, i32 4096, i32 0)
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = call i64 @strlen(i8* %5)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %35, label %34
34: ; preds = %31, %35, %38
br label %28
35: ; preds = %31
%36 = call i32 @parseMessage(i8* %5)
%37 = call i32 @memset(i8* %5, i32 0, i32 4096)
br label %34
38: ; preds = %28
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %34
40: ; preds = %24, %10
ret void
}