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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(void) {
  struct sockaddr_in si_me;
  struct sockaddr_in si_other;
  int s = 0;
  int i = 0;
  int slen = sizeof(si_other);
  int recv_len = 0;
  char buf[(512)] = {};
  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    die("socket");
  }
  memset((char *)&si_me, 0, sizeof(si_me));
  si_me.sin_family = AF_INET;
  si_me.sin_port = htons((8888));
  si_me.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me)) == -1) {
    die("bind");
  }
  while (1) {
    printf("Waiting for data...");
    fflush(stdout);
    if ((recv_len = recvfrom(s, buf, (512), 0, (struct sockaddr *)&si_other,
                             (socklen_t *)&slen)) == -1) {
      die("recvfrom()");
    }
    printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr),
           ntohs(si_other.sin_port));
    printf("Data: %s\n", buf);
    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    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0xc,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1239 <main+0x79>
lea    0xdcc(%rip),%rdi
call   10d0 <die@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1299 <main+0xd9>
lea    0xd73(%rip),%rdi
call   10d0 <die@plt>
jmp    129e <main+0xde>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x200,%edx
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    12f0 <main+0x130>
lea    0xd35(%rip),%rdi
call   10d0 <die@plt>
mov    -0x1c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x238(%rbp)
mov    -0x20(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x238(%rbp),%rsi
mov    %eax,%edx
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x230(%rbp),%rsi
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    -0x20(%rbp),%r8
mov    -0x2c(%rbp),%r9d
xor    %ecx,%ecx
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1364 <main+0x1a4>
lea    0xcf2(%rip),%rdi
call   10d0 <die@plt>
jmp    129e <main+0xde>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x238,%rsp
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%rdi
mov    $0x200,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x2df7(%rip),%r14
mov    (%r14),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    121e <main+0x5e>
lea    0xde7(%rip),%rdi
call   10d0 <die@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1279 <main+0xb9>
lea    0xd93(%rip),%rdi
call   10d0 <die@plt>
lea    0x30(%rsp),%rbx
lea    0x20(%rsp),%r15
lea    0xdbd(%rip),%r14
nopw   0x0(%rax,%rax,1)
lea    0xd75(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
mov    %r15,%r8
lea    0xc(%rsp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    12d9 <main+0x119>
lea    0xd4c(%rip),%rdi
call   10d0 <die@plt>
mov    0x24(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r13
mov    0x20(%rsp),%edi
call   10b0 <ntohs@plt>
lea    0xd36(%rip),%rdi
mov    %r13,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%r9d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1080 <sendto@plt>
cmp    $0xffffffff,%eax
jne    1290 <main+0xd0>
lea    0xd1c(%rip),%rdi
call   10d0 <die@plt>
jmp    1290 <main+0xd0>

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
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [20 x i8] c"Waiting for data...\00"
@stdout = global i32 0
@.str.3 = [11 x i8] c"recvfrom()\00"
@.str.4 = [28 x i8] c"Received packet from %s:%d\0A\00"
@.str.5 = [10 x i8] c"Data: %s\0A\00"
@.str.6 = [9 x i8] c"sendto()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
store i32 0, i32* %1
store i32 0, i32* %4
store i32 0, i32* %5
store i32 12, i32* %6
store i32 0, i32* %7
%9 = bitcast [512 x i8]* %8 to i8*
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = load i32, i32* @IPPROTO_UDP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = bitcast %struct.sockaddr_in* %2 to i8*
%19 = call i32 @memset(i8* %18, i32 0, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 8888)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
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* %2, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %4
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %17
br label %35
35: ; preds = %34, %66
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @stdout
%38 = call i32 @fflush(i32 %37)
%39 = load i32, i32* %4
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i32 @recvfrom(i32 %39, i8* %40, i32 512, i32 0, %struct.sockaddr* %41, i32* %6)
store i32 %42, i32* %7
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %35
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%48 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @ntohs(i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %50, i32 %53)
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %55)
%57 = load i32, i32* %4
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %7
%60 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%61 = load i32, i32* %6
%62 = call i32 @sendto(i32 %57, i8* %58, i32 %59, i32 0, %struct.sockaddr* %60, i32 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %46
%65 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %64, %46
br label %35
}

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
@IPPROTO_UDP = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [20 x i8] c"Waiting for data...\00"
@stdout = global i32 0
@.str.3 = [11 x i8] c"recvfrom()\00"
@.str.4 = [28 x i8] c"Received packet from %s:%d\0A\00"
@.str.5 = [10 x i8] c"Data: %s\0A\00"
@.str.6 = [9 x i8] c"sendto()\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @IPPROTO_UDP
%12 = tail call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = call i32 @memset(i8* %5, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 8888)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i32 @bind(i32 %12, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %16
%31 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %34
34: ; preds = %54, %30
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @stdout
%37 = call i32 @fflush(i32 %36)
%38 = call i32 @recvfrom(i32 %12, i8* %8, i32 512, i32 0, %struct.sockaddr* %31, i32* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = load i32, i32* %32
%44 = call i8* @inet_ntoa(i32 %43)
%45 = load i32, i32* %33
%46 = call i32 @ntohs(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %44, i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%49 = load i32, i32* %3
%50 = call i32 @sendto(i32 %12, i8* %8, i32 %38, i32 0, %struct.sockaddr* %31, i32 %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %42
%53 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %52, %42
br label %34
}