#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__ {
  void *s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  void *sin_port;
  void *sin_family;
};
struct sockaddr {
  int dummy;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ src_addr;
typedef int socklen_t;
typedef int /*<<< orphan*/ recv_buf;
typedef int /*<<< orphan*/ listener_addr;
typedef int /*<<< orphan*/ dst_addr;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
void *htons(int);
void *inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
size_t recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
size_t sendto(int, char const *, int /*<<< orphan*/, int /*<<< orphan*/,
              struct sockaddr *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  int so = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  printf("socket() returned %d.\n", so);
  struct sockaddr_in listener_addr;
  memset(&listener_addr, 0, sizeof(listener_addr));
  listener_addr.sin_family = AF_INET;
  listener_addr.sin_port = htons(760);
  listener_addr.sin_addr.s_addr = inet_addr("172.17.0.1");
  int rc = bind(so, (struct sockaddr *)&listener_addr, sizeof(listener_addr));
  printf("bind() returned %d.\n", rc);
  while (1) {
    usleep(10000);
    struct sockaddr_in dst_addr;
    memset(&dst_addr, 0, sizeof(dst_addr));
    dst_addr.sin_family = AF_INET;
    dst_addr.sin_port = htons(2048);
    dst_addr.sin_addr.s_addr = inet_addr("172.17.0.3");
    const char *send_buf = "Hello world";
    ssize_t s = sendto(so, send_buf, strlen(send_buf), 0,
                       (struct sockaddr *)&dst_addr, sizeof(dst_addr));
    printf("sendto() returned %ld.\n", s);
    char recv_buf[1024];
    struct sockaddr_in src_addr;
    socklen_t src_addr_len;
    src_addr_len = sizeof(src_addr);
    ssize_t r = recvfrom(so, recv_buf, sizeof(recv_buf), 0,
                         (struct sockaddr *)&src_addr, &src_addr_len);
    printf("recvfrom() returned %ld.\n", r);
    if (r > 0) {
      recv_buf[r] = 0;
      printf("Received '%s'.\n", recv_buf);
    }
  }
  return rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    $0x2f8,%edi
call   1040 <htons@plt>
mov    %rax,-0x28(%rbp)
lea    0xdf6(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <bind@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    $0x800,%edi
call   1040 <htons@plt>
mov    %rax,-0x48(%rbp)
lea    0xdab(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %rax,-0x50(%rbp)
lea    0xda6(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x494(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x490(%rbp)
mov    -0x58(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x494(%rbp),%edi
mov    -0x490(%rbp),%rsi
mov    %eax,%edx
lea    -0x50(%rbp),%r8
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1070 <sendto@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x18,-0x47c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x460(%rbp),%rsi
lea    -0x478(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x47c(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %rax,-0x488(%rbp)
mov    -0x488(%rbp),%rsi
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x488(%rbp)
jbe    136f <main+0x1cf>
mov    -0x488(%rbp),%rax
movb   $0x0,-0x460(%rbp,%rax,1)
lea    -0x460(%rbp),%rsi
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124f <main+0xaf>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x458,%rsp
mov    0x2e30(%rip),%rbp
mov    0x0(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
lea    0xe24(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%rax
mov    %rax,0x30(%rsp)
mov    $0x2f8,%edi
call   1040 <htons@plt>
mov    %rax,0x28(%rsp)
lea    0xdff(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %rax,0x20(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   10a0 <bind@plt>
lea    0xdea(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%r14
lea    0xdf5(%rip),%r12
lea    0x50(%rsp),%rbp
lea    0xe0d(%rip),%r15
nopl   0x0(%rax)
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x18(%rsp)
mov    $0x800,%edi
call   1040 <htons@plt>
mov    %rax,0x10(%rsp)
lea    0xd99(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %rax,0x8(%rsp)
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x18,%r9d
call   1070 <sendto@plt>
lea    0xd80(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x18,0x4(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
lea    0x38(%rsp),%r8
lea    0x4(%rsp),%r9
call   1090 <recvfrom@plt>
mov    %rax,%r13
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
test   %r13,%r13
je     1260 <main+0xc0>
movb   $0x0,0x50(%rsp,%r13,1)
lea    0xd5f(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1260 <main+0xc0>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [23 x i8] c"socket() returned %d.\0A\00"
@.str.1 = [11 x i8] c"172.17.0.1\00"
@.str.2 = [21 x i8] c"bind() returned %d.\0A\00"
@.str.3 = [11 x i8] c"172.17.0.3\00"
@.str.4 = [12 x i8] c"Hello world\00"
@.str.5 = [24 x i8] c"sendto() returned %ld.\0A\00"
@.str.6 = [26 x i8] c"recvfrom() returned %ld.\0A\00"
@.str.7 = [16 x i8] c"Received '%s'.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [1024 x i8]
%13 = alloca %struct.sockaddr_in
%14 = alloca i32
%15 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i8*, i8** @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = load i32, i32* @IPPROTO_UDP
%19 = call i32 @socket(i8* %16, i32 %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 24)
%23 = load i8*, i8** @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i8* %23, i8** %24
%25 = call i8* @htons(i32 760)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i8* %25, i8** %26
%27 = call i8* @inet_addr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i8* %27, i8** %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 24)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %2, %66
%36 = call i32 @usleep(i32 10000)
%37 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 24)
%38 = load i8*, i8** @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i8* %38, i8** %39
%40 = call i8* @htons(i32 2048)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i8* %40, i8** %41
%42 = call i8* @inet_addr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
store i8* %42, i8** %44
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8** %10
%45 = load i32, i32* %6
%46 = load i8*, i8** %10
%47 = load i8*, i8** %10
%48 = call i32 @strlen(i8* %47)
%49 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%50 = call i64 @sendto(i32 %45, i8* %46, i32 %48, i32 0, %struct.sockaddr* %49, i32 24)
store i64 %50, i64* %11
%51 = load i64, i64* %11
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i64 %51)
store i32 24, i32* %14
%53 = load i32, i32* %6
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%55 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%56 = call i64 @recvfrom(i32 %53, i8* %54, i32 1024, i32 0, %struct.sockaddr* %55, i32* %14)
store i64 %56, i64* %15
%57 = load i64, i64* %15
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i64 %57)
%59 = load i64, i64* %15
%60 = icmp ugt i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %35
%62 = load i64, i64* %15
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %62
store i8 0, i8* %63
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %64)
br label %66
66: ; preds = %61, %35
br label %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i8*, i8* }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [23 x i8] c"socket() returned %d.\0A\00"
@.str.1 = [11 x i8] c"172.17.0.1\00"
@.str.2 = [21 x i8] c"bind() returned %d.\0A\00"
@.str.3 = [11 x i8] c"172.17.0.3\00"
@.str.4 = [12 x i8] c"Hello world\00"
@.str.5 = [24 x i8] c"sendto() returned %ld.\0A\00"
@.str.6 = [26 x i8] c"recvfrom() returned %ld.\0A\00"
@.str.7 = [16 x i8] c"Received '%s'.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [1024 x i8]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = load i8*, i8** @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = load i32, i32* @IPPROTO_UDP
%11 = tail call i32 @socket(i8* %8, i32 %9, i32 %10)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 24)
%15 = load i8*, i8** @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i8* %15, i8** %16
%17 = call i8* @htons(i32 760)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i8* %17, i8** %18
%19 = call i8* @inet_addr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i8* %19, i8** %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @bind(i32 %11, %struct.sockaddr* %21, i32 24)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = bitcast %struct.sockaddr_in* %4 to i8*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = bitcast %struct.sockaddr_in* %6 to i8*
%31 = bitcast i32* %7 to i8*
%32 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %33
33: ; preds = %48, %2
%34 = call i32 @usleep(i32 10000)
%35 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 24)
%36 = load i8*, i8** @AF_INET
store i8* %36, i8** %25
%37 = call i8* @htons(i32 2048)
store i8* %37, i8** %26
%38 = call i8* @inet_addr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
store i8* %38, i8** %27
%39 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%40 = call i64 @sendto(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %39, i32 0, %struct.sockaddr* %28, i32 24)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i64 %40)
store i32 24, i32* %7
%42 = call i64 @recvfrom(i32 %11, i8* %29, i32 1024, i32 0, %struct.sockaddr* %32, i32* %7)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i64 %42)
%44 = icmp eq i64 %42, 0
br i1 %44, label %48, label %45
45: ; preds = %33
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %42
store i8 0, i8* %46
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i8* %29)
br label %48
48: ; preds = %45, %33
br label %33
}