#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*/ rem_addr;
typedef int /*<<< orphan*/ own_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, unsigned char *, int, int /*<<< orphan*/, struct sockaddr *,
             int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct sockaddr_in own_addr;
  struct sockaddr_in rem_addr;
  int rx_pkt_len;
  int fd;
  int rc;
  socklen_t addr_len = sizeof(rem_addr);
  unsigned char rx_buf[2048];
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("Cannot create socket\n");
    return 0;
  }
  memset((char *)&own_addr, 0, sizeof(own_addr));
  own_addr.sin_family = AF_INET;
  own_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  own_addr.sin_port = htons(18264);
  if ((rc = bind(fd, (struct sockaddr *)&own_addr, sizeof(own_addr))) < 0) {
    perror("Socket binding failed");
    return 0;
  }
  printf(":: Netdebug waiting for data UDP port %d ::\n", 18264);
  while (1) {
    rx_pkt_len =
        recvfrom(fd, rx_buf, 2048, 0, (struct sockaddr *)&rem_addr, &addr_len);
    if (rx_pkt_len > 0) {
      rx_buf[rx_pkt_len] = 0;
      printf("%s\n", rx_buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xc,-0x40(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    11dd <main+0x5d>
lea    0xe34(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <main+0x130>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x4758,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    124c <main+0xcc>
lea    0xddb(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <main+0x130>
lea    0xdd9(%rip),%rdi
mov    $0x4758,%esi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%edi
lea    -0x840(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x800,%edx
xor    %ecx,%ecx
lea    -0x40(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    12ab <main+0x12b>
movslq -0x34(%rbp),%rax
movb   $0x0,-0x840(%rbp,%rax,1)
lea    -0x840(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125f <main+0xdf>
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x838,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2e3c(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     126d <main+0xdd>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x4758,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
js     1276 <main+0xe6>
lea    0xe0c(%rip),%rdi
mov    $0x4758,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %rbp,%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
jle    1240 <main+0xb0>
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
jmp    1240 <main+0xb0>
lea    0xd8c(%rip),%rdi
jmp    127d <main+0xed>
lea    0xd99(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0x838,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"Cannot create socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"Socket binding failed\00"
@.str.2 = [45 x i8] c":: Netdebug waiting for data UDP port %d ::\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2048 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 12, i32* %11
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %52
19: ; preds = %2
%20 = bitcast %struct.sockaddr_in* %6 to i8*
%21 = call i32 @memset(i8* %20, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = call i32 @htons(i32 18264)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %9
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %19
%35 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %52
36: ; preds = %19
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 18264)
br label %38
38: ; preds = %36, %51
%39 = load i32, i32* %9
%40 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i32 @recvfrom(i32 %39, i8* %40, i32 2048, i32 0, %struct.sockaddr* %41, i32* %11)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %38
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 %47
store i8 0, i8* %48
%49 = getelementptr inbounds [2048 x i8], [2048 x i8]* %12, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %45, %38
br label %38
52: ; preds = %34, %17
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [22 x i8] c"Cannot create socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"Socket binding failed\00"
@.str.2 = [45 x i8] c":: Netdebug waiting for data UDP port %d ::\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [2048 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %42
17: ; preds = %2
%18 = call i32 @memset(i8* %7, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = call i32 @htons(i32 18264)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @bind(i32 %13, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %42
31: ; preds = %17
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 18264)
%33 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %34
34: ; preds = %41, %31
%35 = call i32 @recvfrom(i32 %13, i8* %10, i32 2048, i32 0, %struct.sockaddr* %33, i32* %5)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = zext i32 %35 to i64
%39 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %38
store i8 0, i8* %39
%40 = call i32 @puts(i8* %10)
br label %41
41: ; preds = %37, %34
br label %34
42: ; preds = %29, %15
ret i32 0
}