#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_addr;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
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 /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd;
  struct sockaddr_in serv_addr, cli_addr;
  socklen_t cli_len;
  char msg[1024];
  int str_len;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("socket() failed");
    exit(EXIT_FAILURE);
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(atoi(argv[1]));
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    fprintf(stderr, "bind() failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  cli_len = sizeof(cli_addr);
  while (1) {
    str_len = recvfrom(sockfd, msg, sizeof(msg), 0,
                       (struct sockaddr *)&cli_addr, &cli_len);
    printf("recv %d bytes\n", str_len);
    sendto(sockfd, msg, str_len, 0, (struct sockaddr *)&cli_addr, cli_len);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     121d <main+0x4d>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf6(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    125c <main+0x8c>
lea    0xdc9(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12fa <main+0x12a>
mov    0x2d07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x448(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x448(%rbp),%edi
mov    %rax,%rdx
lea    0xd3b(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
movl   $0xc,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x444(%rbp)
mov    -0x444(%rbp),%esi
lea    0xd03(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x444(%rbp),%edx
lea    -0x30(%rbp),%r8
mov    -0x34(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
jmp    1301 <main+0x131>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    12cb <main+0xfb>
mov    0x2df4(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     12e0 <main+0x110>
mov    %eax,%ebx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12ee <main+0x11e>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
lea    0xdac(%rip),%r13
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1090 <recvfrom@plt>
mov    %eax,%ebp
mov    %r13,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    0xc(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1070 <sendto@plt>
jmp    1290 <main+0xc0>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd22(%rip),%rsi
jmp    1311 <main+0x141>
lea    0xd2b(%rip),%rdi
call   10b0 <perror@plt>
jmp    1316 <main+0x146>
mov    0x2cd3(%rip),%rax
mov    (%rax),%ebx
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd16(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1080 <fprintf@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

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

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