#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__ {
  char *s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(void *, int, 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(void) {
  int fd, n, addrlen;
  struct sockaddr_in addr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == 1) {
    exit(1);
  }
  memset((void *)&addr, (int)'\0', sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = "tejo.ist.utl.pt";
  addr.sin_port = htons(5800);
  n = sendto(fd, "Hello!\n", 7, 0, (struct sockaddr *)&addr, sizeof(addr));
  char buffer[128];
  addrlen = sizeof(addr);
  n = recvfrom(fd, buffer, 128, 0, (struct sockaddr *)&addr, &addrlen);
  if (n == 1) {
    exit(1);
  }
  write(1, "echo: ", 6);
  write(1, buffer, n);
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11c2 <main+0x42>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe1b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x16a8,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%r8
lea    0xe0c(%rip),%rsi
mov    $0x7,%edx
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1060 <sendto@plt>
mov    %eax,-0xc(%rbp)
movl   $0x18,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xb0(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
lea    -0x10(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1255 <main+0xd5>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x1,%edi
lea    0xdb7(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
lea    -0xb0(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    0x2e56(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0x1,%eax
je     1227 <main+0xa7>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
lea    0xe32(%rip),%rax
mov    %rax,0x10(%rsp)
mov    $0x16a8,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xe28(%rip),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x18,%r9d
call   1060 <sendto@plt>
movl   $0x18,0x4(%rsp)
lea    0x20(%rsp),%rsi
lea    0x4(%rsp),%r9
mov    %ebx,%edi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    %r14,%r8
call   1080 <recvfrom@plt>
cmp    $0x1,%eax
jne    1231 <main+0xb1>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebp
lea    0xdde(%rip),%rsi
mov    $0x1,%edi
mov    $0x6,%edx
call   1030 <write@plt>
lea    0x20(%rsp),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"tejo.ist.utl.pt\00"
@.str.1 = [8 x i8] c"Hello!\0A\00"
@.str.2 = [7 x i8] c"echo: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca [128 x i8]
store i32 0, i32* %1
%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* %2
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = bitcast %struct.sockaddr_in* %5 to i8*
%16 = call i32 @memset(i8* %15, i32 0, i32 24)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %20
%21 = call i32 @htons(i32 5800)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = call i32 @sendto(i32 %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7, i32 0, %struct.sockaddr* %24, i32 24)
store i32 %25, i32* %3
store i32 24, i32* %4
%26 = load i32, i32* %2
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%28 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%29 = call i32 @recvfrom(i32 %26, i8* %27, i32 128, i32 0, %struct.sockaddr* %28, i32* %4)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %34
32: ; preds = %14
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %14
%35 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%37 = load i32, i32* %3
%38 = call i32 @write(i32 1, i8* %36, i32 %37)
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
%41 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [16 x i8] c"tejo.ist.utl.pt\00"
@.str.1 = [8 x i8] c"Hello!\0A\00"
@.str.2 = [7 x i8] c"echo: \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca [128 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 @memset(i8* %5, i32 0, i32 24)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %16
%17 = call i32 @htons(i32 5800)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%20 = call i32 @sendto(i32 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7, i32 0, %struct.sockaddr* %19, i32 24)
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i32 24, i32* %1
%22 = call i32 @recvfrom(i32 %8, i8* %21, i32 128, i32 0, %struct.sockaddr* %19, i32* %1)
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %12
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %12
%27 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%28 = call i32 @write(i32 1, i8* %21, i32 %22)
%29 = call i32 @close(i32 %8)
%30 = call i32 @exit(i32 0)
unreachable
}