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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int, int, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char const *argv[]) {
  int s = 0;
  int n = 0;
  int reuse = 1;
  int cli_len = sizeof(struct sockaddr);
  int port = 1994;
  char buf[256] = {0};
  struct sockaddr_in addr, cli;
  bzero(&addr, sizeof(addr));
  addr.sin_family = PF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(port);
  s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
  if (s < 0) {
    perror("socket");
    return -1;
  }
  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
  if (bind(s, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    return -1;
  }
  while (1) {
    memset(buf, 0, 256);
    n = recvfrom(s, buf, 256, 0, (struct sockaddr *)&cli,
                 (socklen_t *)&cli_len);
    if (n < 0) {
      perror("recvfrom");
      break;
    } else {
      printf("receive msg from %s(port=%d) len %d: %s\n",
             inet_ntoa(cli.sin_addr), port, n, buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x4,-0x20(%rbp)
movl   $0x7ca,-0x24(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x140(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x138(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x140(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x13c(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1271 <main+0xd1>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <main+0x1b5>
mov    -0x14(%rbp),%edi
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
mov    0x2d2c(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x14(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12cb <main+0x12b>
lea    0xd4d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <main+0x1b5>
jmp    12d0 <main+0x130>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
lea    -0x150(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1322 <main+0x182>
lea    0xcf4(%rip),%rdi
call   10a0 <perror@plt>
jmp    134e <main+0x1ae>
mov    -0x150(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    -0x130(%rbp),%r8
lea    0xcd1(%rip),%rdi
call   1060 <printf@plt>
jmp    12d0 <main+0x130>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
movl   $0x1,0xc(%rsp)
movl   $0x4,0x8(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
lea    0x10(%rsp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x7ca,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
jns    1299 <main+0xf9>
lea    0xd69(%rip),%rdi
jmp    12dc <main+0x13c>
mov    %eax,%ebx
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    12eb <main+0x14b>
lea    0xd2b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    139a <main+0x1fa>
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    0x20(%rsp),%r8
lea    0x8(%rsp),%r9
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
test   %eax,%eax
js     138c <main+0x1ec>
mov    %eax,%r14d
lea    0xced(%rip),%r15
lea    0x30(%rsp),%rbp
lea    0x20(%rsp),%r12
lea    0x8(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0x7ca,%edx
mov    %r14d,%ecx
mov    %rbp,%r8
call   1060 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1080 <recvfrom@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1340 <main+0x1a0>
lea    0xc79(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
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
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@PF_UNSPEC = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [9 x i8] c"recvfrom\00"
@.str.3 = [41 x i8] c"receive msg from %s(port=%d) len %d: %s\0A\00"
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 i32
%11 = alloca [256 x i8]
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 1, i32* %8
store i32 4, i32* %9
store i32 1994, i32* %10
%14 = bitcast [256 x i8]* %11 to i8*
%15 = call i32 @bzero(%struct.sockaddr_in* %12, i32 12)
%16 = load i32, i32* @PF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* %10
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %22, i32* %23
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_DGRAM
%26 = load i32, i32* @PF_UNSPEC
%27 = call i32 @socket(i32 %24, i32 %25, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
32: ; preds = %2
%33 = load i32, i32* %6
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i32 @setsockopt(i32 %33, i32 %34, i32 %35, i32* %8, i32 4)
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
43: ; preds = %32
br label %44
44: ; preds = %43, %64
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%46 = call i32 @memset(i8* %45, i32 0, i32 256)
%47 = load i32, i32* %6
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%49 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%50 = call i32 @recvfrom(i32 %47, i8* %48, i32 256, i32 0, %struct.sockaddr* %49, i32* %9)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %65
55: ; preds = %44
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i8* @inet_ntoa(i32 %58)
%60 = load i32, i32* %10
%61 = load i32, i32* %7
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%63 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %59, i32 %60, i32 %61, i8* %62)
br label %64
64: ; preds = %55
br label %44
65: ; preds = %53
store i32 0, i32* %3
br label %66
66: ; preds = %65, %41, %30
%67 = load i32, i32* %3
ret i32 %67
}

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
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@PF_UNSPEC = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [9 x i8] c"recvfrom\00"
@.str.3 = [41 x i8] c"receive msg from %s(port=%d) len %d: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%9 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = bitcast %struct.sockaddr_in* %7 to i8*
%13 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%14 = load i32, i32* @PF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 1994)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_DGRAM
%22 = load i32, i32* @PF_UNSPEC
%23 = tail call i32 @socket(i32 %20, i32 %21, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %53
27: ; preds = %2
%28 = load i32, i32* @SOL_SOCKET
%29 = load i32, i32* @SO_REUSEADDR
%30 = call i32 @setsockopt(i32 %23, i32 %28, i32 %29, i32* %3, i32 4)
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%32 = call i64 @bind(i32 %23, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %27
%35 = call i32 @memset(i8* %10, i32 0, i32 256)
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = call i32 @recvfrom(i32 %23, i8* %10, i32 256, i32 0, %struct.sockaddr* %36, i32* %4)
%38 = icmp slt i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %34
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0, i32 0
br label %45
41: ; preds = %27
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %53
43: ; preds = %45, %34
%44 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %53
45: ; preds = %39, %45
%46 = phi i32 [ %37, %39 ], [ %51, %45 ]
%47 = load i32, i32* %40
%48 = call i8* @inet_ntoa(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %48, i32 1994, i32 %46, i8* %10)
%50 = call i32 @memset(i8* %10, i32 0, i32 256)
%51 = call i32 @recvfrom(i32 %23, i8* %10, i32 256, i32 0, %struct.sockaddr* %36, i32* %4)
%52 = icmp slt i32 %51, 0
br i1 %52, label %43, label %45
53: ; preds = %43, %41, %25
%54 = phi i32 [ -1, %25 ], [ -1, %41 ], [ 0, %43 ]
ret i32 %54
}