#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*/ addr_this;
typedef int /*<<< orphan*/ addr_sender;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fd_recv;
  struct sockaddr_in addr_this;
  int retval;
  fd_recv = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  if (fd_recv < 0) {
    printf("Failed to setup socket\n");
    return 1;
  }
  memset(&addr_this, 0, sizeof(addr_this));
  addr_this.sin_family = AF_INET;
  addr_this.sin_addr.s_addr = inet_addr("127.0.0.1");
  addr_this.sin_port = htons(53);
  retval = bind(fd_recv, (struct sockaddr *)&addr_this, sizeof(addr_this));
  if (retval != 0) {
    printf("Failed to bind socket\n");
    return 1;
  }
  char msgbuf[512];
  struct sockaddr_in addr_sender;
  socklen_t addr_sender_len = sizeof(addr_sender);
  int nbytes;
  printf("Starting sink\n");
  while (1) {
    nbytes = recvfrom(fd_recv, msgbuf, 512 - 1, 0,
                      (struct sockaddr *)&addr_sender, &addr_sender_len);
    if (nbytes < 0)
      break;
    if (nbytes > 0) {
      msgbuf[nbytes] = 0;
      printf("Received:\n%s\n", msgbuf);
    } else {
      printf("0 Bytes received");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c9 <main+0x59>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x16d>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xe2c(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x35,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1239 <main+0xc9>
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x16d>
movl   $0xc,-0x234(%rbp)
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
lea    -0x230(%rbp),%r8
mov    $0x1ff,%edx
xor    %ecx,%ecx
lea    -0x234(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x238(%rbp)
cmpl   $0x0,-0x238(%rbp)
jge    128d <main+0x11d>
jmp    12d6 <main+0x166>
cmpl   $0x0,-0x238(%rbp)
jle    12c3 <main+0x153>
movslq -0x238(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d1 <main+0x161>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1251 <main+0xe1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1212 <main+0x92>
mov    %eax,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0xe21(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %eax,%eax
je     1239 <main+0xb9>
lea    0xe27(%rip),%rdi
jmp    1219 <main+0x99>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x238,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movl   $0xc,0xc(%rsp)
lea    0xde1(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%rsi
xor    %r15d,%r15d
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
test   %eax,%eax
js     1224 <main+0xa4>
lea    0x30(%rsp),%rbp
lea    0x20(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0xd83(%rip),%r14
jmp    12bd <main+0x13d>
nopl   0x0(%rax)
lea    0xd81(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1ff,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1080 <recvfrom@plt>
test   %eax,%eax
js     1224 <main+0xa4>
test   %eax,%eax
je     1290 <main+0x110>
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    129e <main+0x11e>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, 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
@.str = [24 x i8] c"Failed to setup socket\0A\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [23 x i8] c"Failed to bind socket\0A\00"
@.str.3 = [15 x i8] c"Starting sink\0A\00"
@.str.4 = [14 x i8] c"Received:\0A%s\0A\00"
@.str.5 = [17 x i8] c"0 Bytes received\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = call i32 @socket(i32 %9, i32 %10, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %56
17: ; preds = %0
%18 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = call i32 @htons(i32 53)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %17
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %56
33: ; preds = %17
store i32 12, i32* %7
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %33, %54
%36 = load i32, i32* %2
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%38 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%39 = call i32 @recvfrom(i32 %36, i8* %37, i32 511, i32 0, %struct.sockaddr* %38, i32* %7)
store i32 %39, i32* %8
%40 = load i32, i32* %8
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %35
br label %55
43: ; preds = %35
%44 = load i32, i32* %8
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %43
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 %48
store i8 0, i8* %49
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %54
52: ; preds = %43
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %54
54: ; preds = %52, %46
br label %35
55: ; preds = %42
store i32 0, i32* %1
br label %56
56: ; preds = %55, %31, %15
%57 = load i32, i32* %1
ret i32 %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, 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
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.4 = [14 x i8] c"Received:\0A%s\0A\00"
@.str.5 = [17 x i8] c"0 Bytes received\00"
@str = [14 x i8] c"Starting sink\00"
@str.6 = [22 x i8] c"Failed to bind socket\00"
@str.7 = [23 x i8] c"Failed to setup socket\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [512 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_UDP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
br label %47
13: ; preds = %0
%14 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = call i32 @htons(i32 53)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %13
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
br label %47
26: ; preds = %13
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%28 = bitcast %struct.sockaddr_in* %3 to i8*
%29 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i32 @recvfrom(i32 %9, i8* %27, i32 511, i32 0, %struct.sockaddr* %31, i32* %4)
%33 = icmp slt i32 %32, 0
br i1 %33, label %46, label %34
34: ; preds = %26, %43
%35 = phi i32 [ %44, %43 ], [ %32, %26 ]
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %34
%38 = zext i32 %35 to i64
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %38
store i8 0, i8* %39
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %27)
br label %43
41: ; preds = %34
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = call i32 @recvfrom(i32 %9, i8* %27, i32 511, i32 0, %struct.sockaddr* %31, i32* %4)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %34
46: ; preds = %43, %26
br label %47
47: ; preds = %46, %24, %11
%48 = phi i32 [ 1, %11 ], [ 1, %24 ], [ 0, %46 ]
ret i32 %48
}