#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 sockaddr_storage {
  int dummy;
};
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 size_t ssize_t;
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int, char *);
size_t recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, size_t, int /*<<< orphan*/,
                          struct sockaddr *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void *listen_in_port(void *arg) {
  unsigned int port = (unsigned int)(long)arg;
  int sockfd;
  struct sockaddr_in servaddr;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket error");
    exit(-1);
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(port);
  if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("bind error");
    exit(-1);
  }
  char buf[4096];
  struct sockaddr_storage recvaddr;
  socklen_t addrlen;
  ssize_t n;
  while ((n = recvfrom(sockfd, buf, 4096, 0, (struct sockaddr *)&recvaddr,
                       &addrlen)) > 0) {
    buf[n] = '\0';
    sendto(sockfd, buf, n, 0, (struct sockaddr *)&recvaddr, addrlen);
    printf("Receive from %s:%u\t%s\n",
           inet_ntoa(((struct sockaddr_in *)&recvaddr)->sin_addr),
           ntohs(((struct sockaddr_in *)&recvaddr)->sin_port), buf);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<listen_in_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %eax,-0xc(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1218 <listen_in_port+0x58>
lea    0xdf7(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    1281 <listen_in_port+0xc1>
lea    0xd9b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
jmp    1286 <listen_in_port+0xc6>
mov    -0x10(%rbp),%edi
lea    -0x1020(%rbp),%rsi
lea    -0x1028(%rbp),%r8
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x102c(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %rax,-0x1038(%rbp)
cmp    $0x0,%rax
jbe    132e <listen_in_port+0x16e>
mov    -0x1038(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x10(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x1038(%rbp),%rdx
lea    -0x1028(%rbp),%r8
mov    -0x102c(%rbp),%r9d
xor    %ecx,%ecx
call   1080 <sendto@plt>
mov    -0x1024(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x1040(%rbp)
mov    -0x1028(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x1040(%rbp),%rsi
mov    %eax,%edx
lea    -0x1020(%rbp),%rcx
lea    0xcf4(%rip),%rdi
call   1050 <printf@plt>
jmp    1286 <listen_in_port+0xc6>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_in_port>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %rdi,%rbx
mov    0x2e0d(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     1303 <listen_in_port+0x143>
mov    %eax,%r13d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     130c <listen_in_port+0x14c>
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r13d,%edi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
test   %rax,%rax
je     12ef <listen_in_port+0x12f>
lea    0x20(%rsp),%rbx
lea    0x8(%rsp),%r14
lea    0xd8f(%rip),%r15
lea    0xc(%rsp),%r12
xchg   %ax,%ax
movb   $0x0,0x20(%rsp,%rax,1)
mov    0xc(%rsp),%r9d
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
mov    %r14,%r8
call   1080 <sendto@plt>
mov    0xc(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1050 <printf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r12,%r9
call   1090 <recvfrom@plt>
test   %rax,%rax
jne    1290 <listen_in_port+0xd0>
xor    %eax,%eax
add    $0x1028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf6(%rip),%rdi
jmp    1313 <listen_in_port+0x153>
lea    0xcfa(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [13 x i8] c"socket error\00"
@INADDR_ANY = global i32 0
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [23 x i8] c"Receive from %s:%u\09%s\0A\00"
define i8* @listen_in_port(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca [4096 x i8]
%7 = alloca %struct.sockaddr_storage
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = ptrtoint i8* %10 to i64
%12 = trunc i64 %11 to i32
store i32 %12, i32* %3
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = load i32, i32* @IPPROTO_UDP
%16 = call i32 @socket(i32 %13, i32 %14, i32 %15)
store i32 %16, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %1
%22 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %3
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %4
%33 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %21
%37 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %21
br label %40
40: ; preds = %46, %39
%41 = load i32, i32* %4
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%43 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%44 = call i64 @recvfrom(i32 %41, i8* %42, i32 4096, i32 0, %struct.sockaddr* %43, i32* %8)
store i64 %44, i64* %9
%45 = icmp ugt i64 %44, 0
br i1 %45, label %46, label %66
46: ; preds = %40
%47 = load i64, i64* %9
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 %47
store i8 0, i8* %48
%49 = load i32, i32* %4
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%51 = load i64, i64* %9
%52 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr*
%53 = load i32, i32* %8
%54 = call i32 @sendto(i32 %49, i8* %50, i64 %51, i32 0, %struct.sockaddr* %52, i32 %53)
%55 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in*
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %55, i32 0, i32 1
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i8* @inet_ntoa(i32 %58)
%60 = bitcast %struct.sockaddr_storage* %7 to %struct.sockaddr_in*
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @ntohs(i32 %62)
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%65 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %59, i32 %63, i8* %64)
br label %40
66: ; preds = %40
ret i8* null
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [13 x i8] c"socket error\00"
@INADDR_ANY = global i32 0
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [23 x i8] c"Receive from %s:%u\09%s\0A\00"
define noalias i8* @listen_in_port(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [4096 x i8]
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @IPPROTO_UDP
%10 = tail call i32 @socket(i32 %7, i32 %8, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %1
%16 = ptrtoint i8* %0 to i64
%17 = trunc i64 %16 to i32
%18 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = call i32 @htons(i32 %17)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i64 @bind(i32 %10, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %15
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%34 = bitcast %struct.sockaddr_storage* %4 to i8*
%35 = bitcast i32* %5 to i8*
%36 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%37 = call i64 @recvfrom(i32 %10, i8* %33, i32 4096, i32 0, %struct.sockaddr* %36, i32* %5)
%38 = icmp eq i64 %37, 0
br i1 %38, label %54, label %39
39: ; preds = %32
%40 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 1, i32 0
%41 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
br label %42
42: ; preds = %39, %42
%43 = phi i64 [ %37, %39 ], [ %52, %42 ]
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %43
store i8 0, i8* %44
%45 = load i32, i32* %5
%46 = call i32 @sendto(i32 %10, i8* %33, i64 %43, i32 0, %struct.sockaddr* %36, i32 %45)
%47 = load i32, i32* %40
%48 = call i8* @inet_ntoa(i32 %47)
%49 = load i32, i32* %41
%50 = call i32 @ntohs(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %48, i32 %50, i8* %33)
%52 = call i64 @recvfrom(i32 %10, i8* %33, i32 4096, i32 0, %struct.sockaddr* %36, i32* %5)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %42
54: ; preds = %42, %32
ret i8* null
}