#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 */

/* Type definitions */
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
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) {
  struct hostent *h;
  struct in_addr *a;
  struct sockaddr_in addr;
  int fd, n, addrlen;
  char buffer[128];
  if ((h = gethostbyname("tejo")) == NULL)
    exit(1);
  a = (struct in_addr *)h->h_addr_list[0];
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    exit(1);
  }
  addrlen = sizeof(addr);
  memset((void *)&addr, (int)'\0', addrlen);
  addr.sin_family = AF_INET;
  addr.sin_addr = *a;
  addr.sin_port = htons(58001);
  n = sendto(fd, "Hello!\n", 7, 0, (struct sockaddr *)&addr, addrlen);
  if (n == -1)
    exit(1);
  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    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe57(%rip),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c6 <main+0x36>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1204 <main+0x74>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0xc,-0x34(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0xe291,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%r8
mov    -0x34(%rbp),%r9d
lea    0xdb8(%rip),%rsi
mov    $0x7,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1270 <main+0xe0>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    -0xc0(%rbp),%rsi
lea    -0x28(%rbp),%r8
mov    $0x80,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1090 <recvfrom@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    12a5 <main+0x115>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x1,%edi
lea    0xd5c(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
lea    -0xc0(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
lea    0xe5c(%rip),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1256 <main+0xc6>
mov    (%rax),%rax
mov    (%rax),%rbp
mov    0x2e29(%rip),%r15
mov    (%r15),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1256 <main+0xc6>
mov    %eax,%ebx
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    $0xe291,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0xded(%rip),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
cmp    $0xffffffff,%eax
je     1256 <main+0xc6>
lea    0x20(%rsp),%rsi
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1090 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    1260 <main+0xd0>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%ebp
lea    0xda4(%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   10a0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"tejo\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.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.hostent*
%3 = alloca %struct.in_addr*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
store i32 0, i32* %1
%9 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %9, %struct.hostent** %2
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load %struct.hostent*, %struct.hostent** %2
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 0
%16 = load i64*, i64** %15
%17 = getelementptr inbounds i64, i64* %16, i64 0
%18 = load i64, i64* %17
%19 = inttoptr i64 %18 to %struct.in_addr*
store %struct.in_addr* %19, %struct.in_addr** %3
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_DGRAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %5
%23 = load i32, i32* %5
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %13
store i32 12, i32* %7
%28 = bitcast %struct.sockaddr_in* %4 to i8*
%29 = load i32, i32* %7
%30 = call i32 @memset(i8* %28, i32 0, i32 %29)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%34 = load %struct.in_addr*, %struct.in_addr** %3
%35 = bitcast %struct.in_addr* %33 to i8*
%36 = bitcast %struct.in_addr* %34 to i8*
%37 = call i32 @htons(i32 58001)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %5
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = load i32, i32* %7
%42 = call i32 @sendto(i32 %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 7, i32 0, %struct.sockaddr* %40, i32 %41)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %27
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %27
%48 = load i32, i32* %5
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%50 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%51 = call i32 @recvfrom(i32 %48, i8* %49, i32 128, i32 0, %struct.sockaddr* %50, i32* %7)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %47
%57 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%59 = load i32, i32* %6
%60 = call i32 @write(i32 1, i8* %58, i32 %59)
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
%63 = call i32 @exit(i32 0)
unreachable
}

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