#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ srvAddr;
typedef int socklen_t;
typedef int /*<<< orphan*/ cliAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
                    int *);
scalar_t__ sendto(int, char *, size_t, int /*<<< orphan*/,
                  struct sockaddr const *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sockFd;
  if ((sockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    perror("socket() error");
  struct sockaddr_in srvAddr;
  bzero(&srvAddr, sizeof(srvAddr));
  srvAddr.sin_family = AF_INET;
  srvAddr.sin_port = htons(57345);
  srvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sockFd, (const struct sockaddr *)&srvAddr, sizeof(srvAddr)) < 0)
    perror("bind() error");
  while (1) {
    ssize_t len;
    char buf[4096];
    struct sockaddr_in cliAddr;
    socklen_t addrLen = sizeof(cliAddr);
    if ((len = recvfrom(sockFd, buf, 4096, 0, (struct sockaddr *)&cliAddr,
                        &addrLen)) < 0)
      perror("recvfrom() error");
    if (sendto(sockFd, buf, (size_t)len, 0, (const struct sockaddr *)&cliAddr,
               addrLen) < 0)
      perror("sendto() error");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c3 <main+0x43>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0xe001,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1222 <main+0xa2>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
jmp    1227 <main+0xa7>
movl   $0xc,-0x1034(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
lea    -0x1030(%rbp),%r8
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x1034(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    126f <main+0xef>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x1030(%rbp),%r8
mov    -0x1034(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
cmp    $0x0,%rax
jge    12a8 <main+0x128>
lea    0xd8a(%rip),%rdi
call   1080 <perror@plt>
jmp    1227 <main+0xa7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1038,%rsp
mov    0x2e50(%rip),%r14
mov    (%r14),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1279 <main+0xf9>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0xe001,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     128a <main+0x10a>
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xe11(%rip),%rbp
nopl   0x0(%rax)
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1060 <recvfrom@plt>
mov    %rax,%r13
test   %rax,%rax
jns    1253 <main+0xd3>
lea    0xdce(%rip),%rdi
call   1080 <perror@plt>
mov    0xc(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %ecx,%ecx
mov    %r15,%r8
call   1050 <sendto@plt>
test   %rax,%rax
jns    1220 <main+0xa0>
mov    %rbp,%rdi
call   1080 <perror@plt>
jmp    1220 <main+0xa0>
lea    0xd80(%rip),%rdi
call   1080 <perror@plt>
jmp    11b5 <main+0x35>
lea    0xd7e(%rip),%rdi
call   1080 <perror@plt>
jmp    1206 <main+0x86>

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
@SOCK_DGRAM = global i32 0
@.str = [15 x i8] c"socket() error\00"
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"bind() error\00"
@.str.2 = [17 x i8] c"recvfrom() error\00"
@.str.3 = [15 x i8] c"sendto() error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i64
%5 = alloca [4096 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 0, i32* %1
%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* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 57345)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %14
br label %31
31: ; preds = %30, %49
store i32 12, i32* %7
%32 = load i32, i32* %2
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%34 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%35 = call i64 @recvfrom(i32 %32, i8* %33, i32 4096, i32 0, %struct.sockaddr* %34, i32* %7)
store i64 %35, i64* %4
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %31
%40 = load i32, i32* %2
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%42 = load i64, i64* %4
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = load i32, i32* %7
%45 = call i64 @sendto(i32 %40, i8* %41, i64 %42, i32 0, %struct.sockaddr* %43, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %39
br label %31
}

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
@SOCK_DGRAM = global i32 0
@.str = [15 x i8] c"socket() error\00"
@INADDR_ANY = global i32 0
@.str.1 = [13 x i8] c"bind() error\00"
@.str.2 = [17 x i8] c"recvfrom() error\00"
@.str.3 = [15 x i8] c"sendto() error\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [4096 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = bitcast %struct.sockaddr_in* %1 to i8*
%13 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 57345)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i64 @bind(i32 %7, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %11
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%28 = bitcast %struct.sockaddr_in* %3 to i8*
%29 = bitcast i32* %4 to i8*
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %31
31: ; preds = %42, %26
store i32 12, i32* %4
%32 = call i64 @recvfrom(i32 %7, i8* %27, i32 4096, i32 0, %struct.sockaddr* %30, i32* %4)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = load i32, i32* %4
%38 = call i64 @sendto(i32 %7, i8* %27, i64 %32, i32 0, %struct.sockaddr* %30, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
br label %31
}