#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_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ name;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ memcmp(struct sockaddr_in *, struct sockaddr_in *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, 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*/);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int sd = socket(PF_INET, SOCK_DGRAM, 0);
  struct sockaddr_in name, reply;
  name.sin_family = AF_INET;
  name.sin_port = htons(9500);
  name.sin_addr.s_addr = inet_addr("127.0.0.1");
  char buff[20];
  int len = sizeof(name);
  while (1) {
    int n = read(0, buff, sizeof(buff));
    int s = sendto(sd, buff, n, 0, (struct sockaddr *)&name, len);
    printf("data sent : %d\n", s);
    n = recvfrom(sd, buff, 20, 0, (struct sockaddr *)&name, &len);
    if (n != sizeof(name) || memcmp(&name, &reply, n) != 0) {
      printf("reply (ignored)\n");
      continue;
    }
    printf("data received : %d\n", n);
    write(1, buff, n);
  }
  close(sd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x251c,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    0xe25(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
movl   $0xc,-0x44(%rbp)
lea    -0x40(%rbp),%rsi
xor    %edi,%edi
mov    $0x14,%edx
call   1070 <read@plt>
mov    %eax,-0x48(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%edx
lea    -0x18(%rbp),%r8
mov    -0x44(%rbp),%r9d
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%esi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x18(%rbp),%r8
mov    $0x14,%edx
xor    %ecx,%ecx
lea    -0x44(%rbp),%r9
call   10a0 <recvfrom@plt>
mov    %eax,-0x48(%rbp)
movslq -0x48(%rbp),%rax
cmp    $0xc,%rax
jne    1270 <main+0xe0>
mov    -0x48(%rbp),%edx
lea    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <memcmp@plt>
cmp    $0x0,%rax
je     1283 <main+0xf3>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11ea <main+0x5a>
mov    -0x48(%rbp),%esi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11ea <main+0x5a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x251c,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xe15(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%r14
lea    0x10(%rsp),%r15
lea    0xdfd(%rip),%r12
lea    0xc(%rsp),%r13
lea    0xe15(%rip),%rbp
jmp    1228 <main+0x88>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1030 <puts@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x14,%edx
call   1080 <read@plt>
mov    0xc(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1070 <sendto@plt>
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x14,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r13,%r9
call   10b0 <recvfrom@plt>
cmp    $0xc,%eax
jne    1220 <main+0x80>
mov    %r15,%rdi
lea    0x20(%rsp),%rsi
mov    $0xc,%edx
call   1090 <memcmp@plt>
test   %rax,%rax
jne    1220 <main+0x80>
lea    0xd87(%rip),%rdi
mov    $0xc,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1040 <write@plt>
jmp    1228 <main+0x88>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [16 x i8] c"data sent : %d\0A\00"
@.str.2 = [17 x i8] c"reply (ignored)\0A\00"
@.str.3 = [20 x i8] c"data received : %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [20 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 9500)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
store i32 12, i32* %6
br label %19
19: ; preds = %0, %41, %43
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%21 = call i32 @read(i32 0, i8* %20, i32 20)
store i32 %21, i32* %7
%22 = load i32, i32* %2
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%24 = load i32, i32* %7
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = load i32, i32* %6
%27 = call i32 @sendto(i32 %22, i8* %23, i32 %24, i32 0, %struct.sockaddr* %25, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %2
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @recvfrom(i32 %30, i8* %31, i32 20, i32 0, %struct.sockaddr* %32, i32* %6)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = icmp ne i64 %35, 12
br i1 %36, label %41, label %37
37: ; preds = %19
%38 = load i32, i32* %7
%39 = call i64 @memcmp(%struct.sockaddr_in* %3, %struct.sockaddr_in* %4, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37, %19
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %19
43: ; preds = %37
%44 = load i32, i32* %7
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%47 = load i32, i32* %7
%48 = call i32 @write(i32 1, i8* %46, i32 %47)
br label %19
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [16 x i8] c"data sent : %d\0A\00"
@.str.3 = [20 x i8] c"data received : %d\0A\00"
@str = [16 x i8] c"reply (ignored)\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca [20 x i8]
%4 = alloca i32
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = bitcast %struct.sockaddr_in* %1 to i8*
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 9500)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%17 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %19
19: ; preds = %34, %0
%20 = call i32 @read(i32 0, i8* %16, i32 20)
%21 = load i32, i32* %4
%22 = call i32 @sendto(i32 %7, i8* %16, i32 %20, i32 0, %struct.sockaddr* %18, i32 %21)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i32 @recvfrom(i32 %7, i8* %16, i32 20, i32 0, %struct.sockaddr* %18, i32* %4)
%25 = icmp eq i32 %24, 12
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i64 @memcmp(%struct.sockaddr_in* %1, %struct.sockaddr_in* %2, i32 12)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26, %19
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %34
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 12)
%33 = call i32 @write(i32 1, i8* %16, i32 12)
br label %34
34: ; preds = %31, %29
br label %19
}