#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;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int SO_REUSEADDR;
int SO_REUSEPORT;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int server_fd, new_client;
  struct sockaddr_in address;
  int opt = 1;
  server_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
                 sizeof(opt)))
    return -1;
  address.sin_family = AF_INET;
  address.sin_addr.s_addr = INADDR_ANY;
  address.sin_port = htons(8081);
  if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)))
    return -1;
  printf("Listening on port %i...\n", 8081);
  char buffer[4];
  int addrlen = sizeof(address);
  listen(server_fd, 10);
  new_client =
      accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
  ssize_t num_rcv = recv(new_client, buffer, 4, 0);
  if (num_rcv < 4) {
    printf("Received less than BUFFER_SIZE\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    0x2e0a(%rip),%rax
or     (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
je     11f5 <main+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <main+0x129>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x1f91,%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
je     1241 <main+0xc1>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <main+0x129>
lea    0xdb8(%rip),%rdi
mov    $0x1f91,%esi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xc,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x4,-0x28(%rbp)
jge    12a9 <main+0x129>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x1,0xc(%rsp)
mov    0x2e39(%rip),%r14
mov    (%r14),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    0x2dec(%rip),%rax
mov    0x2e05(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    128d <main+0xfd>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x1f91,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    128d <main+0xfd>
lea    0xdca(%rip),%rdi
xor    %ebx,%ebx
mov    $0x1f91,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movl   $0xc,0x8(%rsp)
mov    %ebp,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   10a0 <accept@plt>
lea    0x1c(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x3,%eax
jg     128d <main+0xfd>
lea    0xd91(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@INADDR_ANY = global i32 0
@.str = [25 x i8] c"Listening on port %i...\0A\00"
@.str.1 = [32 x i8] c"Received less than BUFFER_SIZE\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [4 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = load i32, i32* @SO_REUSEPORT
%16 = or i32 %14, %15
%17 = call i64 @setsockopt(i32 %12, i32 %13, i32 %16, i32* %5, i32 4)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %0
store i32 -1, i32* %1
br label %47
20: ; preds = %0
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = call i32 @htons(i32 8081)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %20
store i32 -1, i32* %1
br label %47
33: ; preds = %20
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 8081)
store i32 12, i32* %7
%35 = load i32, i32* %2
%36 = call i32 @listen(i32 %35, i32 10)
%37 = load i32, i32* %2
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %7)
store i32 %39, i32* %3
%40 = load i32, i32* %3
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%42 = call i32 @recv(i32 %40, i8* %41, i32 4, i32 0)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 4
br i1 %44, label %45, label %47
45: ; preds = %33
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %19, %32, %45, %33
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_REUSEPORT = global i32 0
@INADDR_ANY = global i32 0
@.str = [25 x i8] c"Listening on port %i...\0A\00"
@str = [31 x i8] c"Received less than BUFFER_SIZE\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_REUSEADDR
%12 = load i32, i32* @SO_REUSEPORT
%13 = or i32 %12, %11
%14 = call i64 @setsockopt(i32 %9, i32 %10, i32 %13, i32* %2, i32 4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 8081)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i64 @bind(i32 %9, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %16
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 8081)
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%29 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%30 = call i32 @listen(i32 %9, i32 10)
%31 = call i32 @accept(i32 %9, %struct.sockaddr* %23, i32* %4)
%32 = call i32 @recv(i32 %31, i8* %28, i32 4, i32 0)
%33 = icmp slt i32 %32, 4
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %36
36: ; preds = %34, %26
br label %37
37: ; preds = %16, %0, %36
%38 = phi i32 [ 0, %36 ], [ -1, %0 ], [ -1, %16 ]
ret i32 %38
}