#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int recvfrom(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 /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int s, len, o, port = 6666;
  char buf[512];
  struct sockaddr_in addr;
  int addr_len = sizeof addr;
  if (argc > 1)
    port = atoi(argv[1]);
  s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  o = 1;
  len = 4;
  setsockopt(3, SOL_SOCKET, SO_REUSEADDR, &o, len);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = INADDR_ANY;
  bind(s, (struct sockaddr *)&addr, sizeof addr);
  for (;;) {
    len = recvfrom(s, buf, sizeof buf, 0, (struct sockaddr *)&addr, &addr_len);
    if (len < 0)
      break;
    write(1, buf, len);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1a0a,-0x20(%rbp)
movl   $0xc,-0x234(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11b4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x4,-0x18(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%r8d
mov    $0x3,%edi
lea    -0x1c(%rbp),%rcx
call   1030 <setsockopt@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x228(%rbp)
mov    -0x20(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x22c(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x230(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
lea    -0x230(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x234(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    127f <main+0x10f>
jmp    1298 <main+0x128>
lea    -0x220(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    1249 <main+0xd9>
xor    %eax,%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x228,%rsp
movl   $0xc,0x8(%rsp)
mov    $0x1a0a,%ebp
cmp    $0x2,%edi
jl     119a <main+0x2a>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,%r15d
movl   $0x1,0xc(%rsp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x3,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%r9
mov    %r15d,%edi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1060 <recvfrom@plt>
test   %eax,%eax
js     128b <main+0x11b>
lea    0x20(%rsp),%rbp
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %rbx,%r9
call   1060 <recvfrom@plt>
test   %eax,%eax
jns    1260 <main+0xf0>
xor    %eax,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [512 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 6666, i32* %9
store i32 12, i32* %12
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %9
br label %20
20: ; preds = %15, %2
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_DGRAM
%23 = load i32, i32* @IPPROTO_UDP
%24 = call i32 @socket(i32 %21, i32 %22, i32 %23)
store i32 %24, i32* %6
store i32 1, i32* %8
store i32 4, i32* %7
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = load i32, i32* %7
%28 = call i32 @setsockopt(i32 3, i32 %25, i32 %26, i32* %8, i32 %27)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* %9
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @INADDR_ANY
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%39 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
br label %40
40: ; preds = %48, %20
%41 = load i32, i32* %6
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%43 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%44 = call i32 @recvfrom(i32 %41, i8* %42, i32 512, i32 0, %struct.sockaddr* %43, i32* %12)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %40
br label %52
48: ; preds = %40
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%50 = load i32, i32* %7
%51 = call i32 @write(i32 1, i8* %49, i32 %50)
br label %40
52: ; preds = %47
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%11 = icmp sgt i32 %0, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
br label %16
16: ; preds = %12, %2
%17 = phi i32 [ %15, %12 ], [ 6666, %2 ]
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = load i32, i32* @IPPROTO_UDP
%21 = tail call i32 @socket(i32 %18, i32 %19, i32 %20)
store i32 1, i32* %3
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 3, i32 %22, i32 %23, i32* %3, i32 4)
%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 = call i32 @htons(i32 %17)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %29, i32* %30
%31 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%32 = call i32 @bind(i32 %21, %struct.sockaddr* %31, i32 12)
%33 = call i32 @recvfrom(i32 %21, i8* %8, i32 512, i32 0, %struct.sockaddr* %31, i32* %6)
%34 = icmp slt i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %16, %35
%36 = phi i32 [ %38, %35 ], [ %33, %16 ]
%37 = call i32 @write(i32 1, i8* %8, i32 %36)
%38 = call i32 @recvfrom(i32 %21, i8* %8, i32 512, i32 0, %struct.sockaddr* %31, i32* %6)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %35
40: ; preds = %35, %16
ret i32 0
}