#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 socklen_t;
typedef int /*<<< orphan*/ si_other;
typedef int /*<<< orphan*/ si_me;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc != 2) {
    die("fail");
  }
  int port = atoi(argv[1]);
  struct sockaddr_in si_me, si_other;
  int s, recv_len;
  socklen_t slen = sizeof(si_other);
  char buf[2000];
  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    die("socket");
  }
  int one = 1;
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  memset((char *)&si_me, 0, sizeof(si_me));
  si_me.sin_family = AF_INET;
  si_me.sin_port = htons(port);
  si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me)) == -1) {
    die("bind");
  }
  while (1) {
    if ((recv_len = recvfrom(s, buf, 2000, 0, (struct sockaddr *)&si_other,
                             &slen)) == -1) {
      die("recvfrom()");
    }
    if (sendto(s, buf, recv_len, 0, (struct sockaddr *)&si_other, slen) == -1) {
      die("sendto()");
    }
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cf <main+0x2f>
lea    0xe36(%rip),%rdi
call   10a0 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0xffffffff,%eax
jne    121e <main+0x7e>
lea    0xdec(%rip),%rdi
call   10a0 <die@plt>
movl   $0x1,-0x814(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
lea    -0x814(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    12ad <main+0x10d>
lea    0xd64(%rip),%rdi
call   10a0 <die@plt>
jmp    12b2 <main+0x112>
mov    -0x34(%rbp),%edi
lea    -0x810(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x7d0,%edx
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x38(%rbp)
cmp    $0xffffffff,%eax
jne    12e8 <main+0x148>
lea    0xd2e(%rip),%rdi
call   10a0 <die@plt>
mov    -0x34(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    -0x30(%rbp),%r8
mov    -0x3c(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1319 <main+0x179>
lea    0xd08(%rip),%rdi
call   10a0 <die@plt>
jmp    12b2 <main+0x112>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11c5 <main+0x25>
lea    0xe40(%rip),%rdi
call   10a0 <die@plt>
mov    0x8(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
movl   $0xc,0x8(%rsp)
mov    0x2e00(%rip),%r15
mov    (%r15),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    120d <main+0x6d>
lea    0xdfd(%rip),%rdi
call   10a0 <die@plt>
movl   $0x1,0xc(%rsp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1292 <main+0xf2>
lea    0xd7f(%rip),%rdi
call   10a0 <die@plt>
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%rbp
lea    0x8(%rsp),%r15
lea    0xd74(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x7d0,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r15,%r9
call   1080 <recvfrom@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    12db <main+0x13b>
lea    0xd3b(%rip),%rdi
call   10a0 <die@plt>
mov    0x8(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1070 <sendto@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0x110>
mov    %r13,%rdi
call   10a0 <die@plt>
jmp    12b0 <main+0x110>

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

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