#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 socklen_t;
typedef int /*<<< orphan*/ serv_adr;
typedef int /*<<< orphan*/ clnt_adr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_handling(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int serv_sock;
  char message[30];
  int str_len;
  socklen_t clnt_adr_sz;
  struct sockaddr_in serv_adr, clnt_adr;
  if (argc != 2) {
    printf("Usage: %s <port>\n", argv[0]);
    exit(1);
  }
  serv_sock = socket(PF_INET, SOCK_DGRAM, 0);
  if (serv_sock == -1)
    error_handling("UPD socket creation error");
  memset(&serv_adr, 0, sizeof(serv_adr));
  serv_adr.sin_family = AF_INET;
  serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_adr.sin_port = htons(atoi(argv[1]));
  if (bind(serv_sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) == -1)
    error_handling("bind() error");
  while (1) {
    clnt_adr_sz = sizeof(clnt_adr);
    str_len = recvfrom(serv_sock, message, 30, 0, (struct sockaddr *)&clnt_adr,
                       &clnt_adr_sz);
    sendto(serv_sock, message, str_len, 0, (struct sockaddr *)&clnt_adr,
           clnt_adr_sz);
  }
  close(serv_sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ed <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    121f <main+0x6f>
lea    0xdf8(%rip),%rdi
call   1080 <error_handling@plt>
lea    -0x58(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x58(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x58(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1289 <main+0xd9>
lea    0xda8(%rip),%rdi
call   1080 <error_handling@plt>
jmp    128e <main+0xde>
movl   $0xc,-0x48(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x68(%rbp),%r8
mov    $0x1e,%edx
xor    %ecx,%ecx
lea    -0x48(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
lea    -0x68(%rbp),%r8
mov    -0x48(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
jmp    128e <main+0xde>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    12a9 <main+0xf9>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11f5 <main+0x45>
lea    0xe22(%rip),%rdi
call   1080 <error_handling@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%r14),%rdi
call   10b0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    125d <main+0xad>
lea    0xdd4(%rip),%rdi
call   1080 <error_handling@plt>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%rbp
lea    0xc(%rsp),%r14
nopl   0x0(%rax)
movl   $0xc,0xc(%rsp)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r14,%r9
call   1090 <recvfrom@plt>
mov    0xc(%rsp),%r9d
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1070 <sendto@plt>
jmp    1270 <main+0xc0>
mov    (%r14),%rsi
lea    0xd4d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [26 x i8] c"UPD socket creation error\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [13 x i8] c"bind() error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [30 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @error_handling(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = call i32 @htonl(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%44 = call i32 @bind(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %28
%47 = call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %28
br label %49
49: ; preds = %48, %49
store i32 12, i32* %9
%50 = load i32, i32* %6
%51 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%52 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%53 = call i32 @recvfrom(i32 %50, i8* %51, i32 30, i32 0, %struct.sockaddr* %52, i32* %9)
store i32 %53, i32* %8
%54 = load i32, i32* %6
%55 = getelementptr inbounds [30 x i8], [30 x i8]* %7, i64 0, i64 0
%56 = load i32, i32* %8
%57 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%58 = load i32, i32* %9
%59 = call i32 @sendto(i32 %54, i8* %55, i32 %56, i32 0, %struct.sockaddr* %57, i32 %58)
br label %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [26 x i8] c"UPD socket creation error\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [13 x i8] c"bind() error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [30 x i8]
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i8*, i8** %1
%14 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @error_handling(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %28, i32* %29
%30 = getelementptr inbounds i8*, i8** %1, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%36 = call i32 @bind(i32 %19, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %23
%39 = call i32 @error_handling(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %23
%41 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %42
42: ; preds = %42, %40
store i32 12, i32* %4
%43 = call i32 @recvfrom(i32 %19, i8* %7, i32 30, i32 0, %struct.sockaddr* %41, i32* %4)
%44 = load i32, i32* %4
%45 = call i32 @sendto(i32 %19, i8* %7, i32 %43, i32 0, %struct.sockaddr* %41, i32 %44)
br label %42
}