#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;
};
struct in_addr {
  int dummy;
};
struct hostent {
  char *h_name;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ memset(void *, int, int);
int /*<<< orphan*/ nthohs(int /*<<< orphan*/);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int /*<<< orphan*/);
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 main(void) {
  int fd, n, addrlen;
  struct sockaddr_in addr;
  char buffer[128];
  struct hostent *h;
  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));
  addrlen = sizeof(addr);
  n = recvfrom(fd, buffer, 128, 0, (struct sockaddr *)&addr, &addrlen);
  if (n == 1) {
    exit(1);
  }
  h = gethostbyaddr((char *)&addr.sin_addr, sizeof(struct in_addr), AF_INET);
  if (h == NULL) {
    printf("sent by [%s:%hu]\n", inet_ntoa(addr.sin_addr),
           nthohs(addr.sin_port));
  } else {
    printf("sent by [%s:%hu]\n", h->h_name, ntohs(addr.sin_port));
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11f2 <main+0x42>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xdeb(%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    0xddc(%rip),%rsi
mov    $0x7,%edx
xor    %ecx,%ecx
mov    $0x18,%r9d
call   1070 <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   1090 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    1285 <main+0xd5>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x28(%rbp),%rdi
add    $0x8,%rdi
mov    0x2d54(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   10b0 <gethostbyaddr@plt>
mov    %rax,-0xb8(%rbp)
cmpq   $0x0,-0xb8(%rbp)
jne    12e7 <main+0x137>
mov    -0x20(%rbp),%rdi
call   1030 <inet_ntoa@plt>
mov    %rax,-0xc0(%rbp)
mov    -0x28(%rbp),%edi
call   1080 <nthohs@plt>
mov    -0xc0(%rbp),%rsi
mov    %eax,%edx
lea    0xd3b(%rip),%rdi
call   1050 <printf@plt>
jmp    1315 <main+0x165>
mov    -0xb8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xc8(%rbp)
mov    -0x28(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0xc8(%rbp),%rsi
mov    %eax,%edx
lea    0xd08(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xa0,%rsp
mov    0x2e25(%rip),%r15
mov    (%r15),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0x1,%eax
je     1258 <main+0xa8>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
lea    0xe01(%rip),%rax
mov    %rax,0x10(%rsp)
mov    $0x16a8,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xdf7(%rip),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x18,%r9d
call   1070 <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   1090 <recvfrom@plt>
cmp    $0x1,%eax
jne    1262 <main+0xb2>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0x10(%rsp),%rdi
mov    (%r15),%edx
mov    $0x4,%esi
call   10b0 <gethostbyaddr@plt>
test   %rax,%rax
jne    1291 <main+0xe1>
mov    0x10(%rsp),%rdi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1080 <nthohs@plt>
jmp    129d <main+0xed>
mov    (%rax),%rbx
mov    0x8(%rsp),%edi
call   10a0 <ntohs@plt>
lea    0xd74(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <printf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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