#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ source;
typedef int socklen_t;
typedef int /*<<< orphan*/ reuse;
typedef int /*<<< orphan*/ endpoint;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ SO_REUSEPORT;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ printf(char *, int, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_loop(void) {
  struct sockaddr_in endpoint;
  endpoint.sin_family = AF_INET;
  endpoint.sin_port = htons(9004);
  endpoint.sin_addr.s_addr = INADDR_ANY;
  int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
  int reuse = 1;
  int setsockopt_err =
      setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  if (setsockopt_err)
    printf("Error in setsockopt (SO_REUSEADDR): %d\n", setsockopt_err);
  setsockopt_err =
      setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &reuse, sizeof(reuse));
  if (setsockopt_err)
    printf("Error in setsockopt (SO_REUSEPORT): %d\n", setsockopt_err);
  int bind_err = bind(sockfd, (struct sockaddr *)&endpoint, sizeof(endpoint));
  if (bind_err)
    printf("Error in bind: %d\n", bind_err);
  char message[100];
  for (;;) {
    struct sockaddr_in source;
    socklen_t sizeof_source = sizeof(source);
    int recv_result = recvfrom(sockfd, message, 100 - 1, 0,
                               (struct sockaddr *)&source, &sizeof_source);
    printf("recv'd %d bytes from %s: '%s'\n", recv_result,
           inet_ntoa(source.sin_addr), message);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<listen_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x232c,%edi
call   1050 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1205 <listen_loop+0x95>
mov    -0x1c(%rbp),%esi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1247 <listen_loop+0xd7>
mov    -0x1c(%rbp),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1276 <listen_loop+0x106>
mov    -0x20(%rbp),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    127b <listen_loop+0x10b>
movl   $0xc,-0xa4(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
lea    -0xa0(%rbp),%r8
mov    $0x63,%edx
xor    %ecx,%ecx
lea    -0xa4(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0xa8(%rbp)
mov    -0xa8(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0xa0(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0xac(%rbp),%esi
mov    %rax,%rdx
lea    -0x90(%rbp),%rcx
lea    0xd86(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    127b <listen_loop+0x10b>

x86 assembly (clang, -O2):
<listen_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x232c,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%r14d
movl   $0x1,0x8(%rsp)
mov    0x2e14(%rip),%rbx
mov    (%rbx),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     1206 <listen_loop+0x96>
lea    0xe03(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rbx),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
je     1238 <listen_loop+0xc8>
lea    0xdf9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %eax,%eax
je     125e <listen_loop+0xee>
lea    0xdfb(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xdef(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movl   $0xc,0xc(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x63,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1070 <recvfrom@plt>
mov    %eax,%ebp
mov    0x20(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1280 <listen_loop+0x110>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [40 x i8] c"Error in setsockopt (SO_REUSEADDR): %d\0A\00"
@SO_REUSEPORT = global i32 0
@.str.1 = [40 x i8] c"Error in setsockopt (SO_REUSEPORT): %d\0A\00"
@.str.2 = [19 x i8] c"Error in bind: %d\0A\00"
@.str.3 = [31 x i8] c"recv'd %d bytes from %s: '%s'\0A\00"
define i32 @listen_loop() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @htons(i32 9004)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %2
store i32 1, i32* %3
%20 = load i32, i32* %2
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %3, i32 4)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %0
%27 = load i32, i32* %4
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %0
%30 = load i32, i32* %2
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_REUSEPORT
%33 = call i32 @setsockopt(i32 %30, i32 %31, i32 %32, i32* %3, i32 4)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* %4
%38 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %29
%40 = load i32, i32* %2
%41 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%42 = call i32 @bind(i32 %40, %struct.sockaddr* %41, i32 12)
store i32 %42, i32* %5
%43 = load i32, i32* %5
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i32, i32* %5
%47 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %45, %39
br label %49
49: ; preds = %49, %48
store i32 12, i32* %8
%50 = load i32, i32* %2
%51 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%52 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%53 = call i32 @recvfrom(i32 %50, i8* %51, i32 99, i32 0, %struct.sockaddr* %52, i32* %8)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i8* @inet_ntoa(i32 %57)
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%60 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %54, i8* %58, i8* %59)
br label %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [40 x i8] c"Error in setsockopt (SO_REUSEADDR): %d\0A\00"
@SO_REUSEPORT = global i32 0
@.str.1 = [40 x i8] c"Error in setsockopt (SO_REUSEPORT): %d\0A\00"
@.str.2 = [19 x i8] c"Error in bind: %d\0A\00"
@.str.3 = [31 x i8] c"recv'd %d bytes from %s: '%s'\0A\00"
define i32 @listen_loop() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 9004)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %15, i32 %17, i32 %18, i32* %2, i32 4)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %0
%22 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %23
23: ; preds = %21, %0
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEPORT
%26 = call i32 @setsockopt(i32 %15, i32 %24, i32 %25, i32* %2, i32 4)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %23
%29 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %30
30: ; preds = %28, %23
%31 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%32 = call i32 @bind(i32 %15, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %32)
br label %36
36: ; preds = %34, %30
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%38 = bitcast %struct.sockaddr_in* %4 to i8*
%39 = bitcast i32* %5 to i8*
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %42
42: ; preds = %42, %36
store i32 12, i32* %5
%43 = call i32 @recvfrom(i32 %15, i8* %37, i32 99, i32 0, %struct.sockaddr* %40, i32* %5)
%44 = load i32, i32* %41
%45 = call i8* @inet_ntoa(i32 %44)
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %43, i8* %45, i8* %37)
br label %42
}