#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_zero;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ my_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int inet_ntoa(TYPE_1__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void main() {
  int sockfd;
  struct sockaddr_in my_addr;
  struct sockaddr_in their_addr;
  int sin_size, retval;
  char buf[128];
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(3490);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  bzero(&(my_addr.sin_zero), 8);
  if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) {
    perror("bind");
    exit(1);
  }
  while (1) {
    retval = recvfrom(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr,
                      &sin_size);
    printf("Received datagram from %d \n", inet_ntoa(their_addr.sin_addr));
    if (retval == 0) {
      perror("recvfrom");
      close(sockfd);
      break;
    }
    retval =
        sendto(sockfd, buf, 128, 0, (struct sockaddr *)&their_addr, sin_size);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11f6 <main+0x46>
lea    0xe19(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0xda2,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
add    $0x4,%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    125d <main+0xad>
lea    0xdb9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1262 <main+0xb2>
mov    -0x4(%rbp),%edi
lea    -0xb0(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%esi
lea    0xd78(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x30(%rbp)
jne    12bc <main+0x10c>
lea    0xd7e(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
jmp    12e2 <main+0x132>
mov    -0x4(%rbp),%edi
lea    -0xb0(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    -0x2c(%rbp),%r9d
mov    $0x80,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,-0x30(%rbp)
jmp    1262 <main+0xb2>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb8,%rsp
mov    0x2e20(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1301 <main+0x151>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0x1c(%rsp)
mov    $0xda2,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x14(%rsp),%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     130a <main+0x15a>
lea    0x30(%rsp),%rsi
lea    0x20(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %r14d,%edi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
mov    %eax,%ebx
mov    0x20(%rsp),%edi
call   1030 <inet_ntoa@plt>
lea    0xdab(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
test   %ebx,%ebx
je     12db <main+0x12b>
lea    0x30(%rsp),%r13
lea    0x20(%rsp),%rbp
lea    0xc(%rsp),%r15
lea    0xd8a(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0xc(%rsp),%r9d
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1060 <sendto@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r15,%r9
call   1080 <recvfrom@plt>
mov    %eax,%ebx
mov    0x20(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
test   %ebx,%ebx
jne    1290 <main+0xe0>
lea    0xd46(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0xb8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf8(%rip),%rdi
jmp    1311 <main+0x161>
lea    0xcf6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = 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 = [28 x i8] c"Received datagram from %d \0A\00"
@.str.3 = [9 x i8] c"recvfrom\00"
define void @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %1
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 3
store i32 %15, i32* %16
%17 = call i32 @htons(i32 3490)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%23 = call i32 @bzero(i32* %22, i32 8)
%24 = load i32, i32* %1
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 16)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %14
br label %32
32: ; preds = %31, %48
%33 = load i32, i32* %1
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @recvfrom(i32 %33, i8* %34, i32 128, i32 0, %struct.sockaddr* %35, i32* %4)
store i32 %36, i32* %5
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @inet_ntoa(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %32
%45 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %1
%47 = call i32 @close(i32 %46)
br label %54
48: ; preds = %32
%49 = load i32, i32* %1
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%51 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%52 = load i32, i32* %4
%53 = call i32 @sendto(i32 %49, i8* %50, i32 128, i32 0, %struct.sockaddr* %51, i32 %52)
store i32 %53, i32* %5
br label %32
54: ; preds = %44
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = 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 = [28 x i8] c"Received datagram from %d \0A\00"
@.str.3 = [9 x i8] c"recvfrom\00"
define void @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 3490)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%24 = call i32 @bzero(i32* %23, i32 8)
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i32 @bind(i32 %11, %struct.sockaddr* %25, i32 16)
%27 = icmp eq i32 %26, -1
br i1 %27, label %36, label %28
28: ; preds = %16
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @recvfrom(i32 %11, i8* %8, i32 128, i32 0, %struct.sockaddr* %29, i32* %3)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @inet_ntoa(i32 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %30, 0
br i1 %35, label %39, label %42
36: ; preds = %16
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %42, %28
%40 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @close(i32 %11)
ret void
42: ; preds = %28, %42
%43 = load i32, i32* %3
%44 = call i32 @sendto(i32 %11, i8* %8, i32 128, i32 0, %struct.sockaddr* %29, i32 %43)
%45 = call i32 @recvfrom(i32 %11, i8* %8, i32 128, i32 0, %struct.sockaddr* %29, i32* %3)
%46 = load i32, i32* %31
%47 = call i32 @inet_ntoa(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = icmp eq i32 %45, 0
br i1 %49, label %39, label %42
}