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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ recvfrom(int, char *, int, int /*<<< orphan*/,
                            struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int peer_socket;
  if ((peer_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("socket()");
    exit(1);
  }
  struct sockaddr_in self_addr;
  memset((void *)&self_addr, 0, sizeof(self_addr));
  self_addr.sin_family = AF_INET;
  self_addr.sin_port = htons(5555);
  self_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  if (bind(peer_socket, (struct sockaddr *)&self_addr, sizeof(self_addr)) < 0) {
    perror("connect()");
    exit(1);
  }
  struct sockaddr_in input_peer_addr;
  int peer_len = sizeof(input_peer_addr);
  char recvbuf[1024];
  char sendbuf[1024];
  while (1) {
    printf("Input your message:>");
    scanf("%s", sendbuf);
    sendto(peer_socket, sendbuf, 1024, 0, (struct sockaddr *)&self_addr,
           sizeof(self_addr));
    recvfrom(peer_socket, recvbuf, 1024, 0, (struct sockaddr *)&input_peer_addr,
             (socklen_t *)&peer_len);
    printf("Receive message: %s\n\n", recvbuf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1204 <main+0x54>
lea    0xe0b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x15b3,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    0xdd5(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    126d <main+0xbd>
lea    0xdb5(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0xc,-0x34(%rbp)
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x840(%rbp),%rsi
lea    0xda2(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x14(%rbp),%edi
lea    -0x840(%rbp),%rsi
lea    -0x20(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1080 <recvfrom@plt>
lea    -0x440(%rbp),%rsi
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1274 <main+0xc4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    0x2e20(%rip),%r14
mov    (%r14),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12bf <main+0x10f>
mov    %eax,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x15b3,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xdf4(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12c8 <main+0x118>
movl   $0xc,0xc(%rsp)
lea    0x30(%rsp),%r12
lea    0x430(%rsp),%rbp
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xdd9(%rip),%r13
nopl   0x0(%rax)
lea    0xdb6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdbd(%rip),%rdi
mov    %r12,%rsi
call   1090 <scanf@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1080 <recvfrom@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1260 <main+0xb0>
lea    0xd3a(%rip),%rdi
jmp    12cf <main+0x11f>
lea    0xd44(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [10 x i8] c"connect()\00"
@.str.3 = [21 x i8] c"Input your message:>\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [22 x i8] c"Receive message: %s\0A\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 %struct.sockaddr_in
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = bitcast %struct.sockaddr_in* %7 to i8*
%21 = call i32 @memset(i8* %20, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 5555)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %19
%34 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %19
store i32 12, i32* %9
br label %37
37: ; preds = %36, %37
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%40 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %6
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = call i32 @sendto(i32 %41, i8* %42, i32 1024, i32 0, %struct.sockaddr* %43, i32 12)
%45 = load i32, i32* %6
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i32 @recvfrom(i32 %45, i8* %46, i32 1024, i32 0, %struct.sockaddr* %47, i32* %9)
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %49)
br label %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [9 x i8] c"socket()\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [10 x i8] c"connect()\00"
@.str.3 = [21 x i8] c"Input your message:>\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [22 x i8] c"Receive message: %s\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = bitcast %struct.sockaddr_in* %3 to i8*
%17 = call i32 @memset(i8* %16, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 5555)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %10, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %15
%31 = bitcast %struct.sockaddr_in* %4 to i8*
%32 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %36
36: ; preds = %36, %30
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%39 = call i32 @sendto(i32 %10, i8* %34, i32 1024, i32 0, %struct.sockaddr* %24, i32 12)
%40 = call i32 @recvfrom(i32 %10, i8* %33, i32 1024, i32 0, %struct.sockaddr* %35, i32* %5)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %33)
br label %36
}