#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 */
typedef int uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  char *h_name;
};
typedef int /*<<< orphan*/ ssize_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             unsigned int *);
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 sockfd, newsockfd;
  uint16_t portno;
  unsigned int clilen;
  char buffer[256];
  char *p = buffer;
  struct sockaddr_in serv_addr, cli_addr;
  ssize_t n;
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0) {
    perror("ERROR opening socket");
    exit(1);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = 5001;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR on binding");
    exit(1);
  }
  int rec = recvfrom(sockfd, &buffer[0], 255, 0, (struct sockaddr *)&cli_addr,
                     &clilen);
  if (rec < 0) {
    perror("ERROR recieving");
    exit(1);
  }
  struct hostent *hst;
  hst = gethostbyaddr((char *)&cli_addr.sin_addr, 4, AF_INET);
  printf("+%s [%s:%d] new DATAGRAM!\n", (hst) ? hst->h_name : "Unknown host",
         inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
  printf("C=>S:%s\n", &buffer[0]);
  sendto(sockfd, &buffer[0], 256, 0, (struct sockaddr *)&cli_addr,
         sizeof(cli_addr));
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1233 <main+0x63>
lea    0xddc(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x138(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
movl   $0x1389,-0x1c(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x130(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x134(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x138(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x138(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12ab <main+0xdb>
lea    0xd79(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x148(%rbp),%r8
mov    $0xff,%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x150(%rbp)
cmpl   $0x0,-0x150(%rbp)
jge    12f5 <main+0x125>
lea    0xd40(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x148(%rbp),%rdi
add    $0x4,%rdi
mov    0x2ce1(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   10d0 <gethostbyaddr@plt>
mov    %rax,-0x158(%rbp)
cmpq   $0x0,-0x158(%rbp)
je     133e <main+0x16e>
mov    -0x158(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x160(%rbp)
jmp    1351 <main+0x181>
lea    0xd0c(%rip),%rax
mov    %rax,-0x160(%rbp)
jmp    1351 <main+0x181>
mov    -0x160(%rbp),%rax
mov    %rax,-0x170(%rbp)
mov    -0x144(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x168(%rbp)
mov    -0x148(%rbp),%edi
call   1090 <ntohs@plt>
mov    -0x170(%rbp),%rsi
mov    -0x168(%rbp),%rdx
mov    %eax,%ecx
lea    0xca3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x120(%rbp),%rsi
lea    0xcb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
lea    -0x148(%rbp),%r8
mov    $0x100,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x120,%rsp
mov    0x2e06(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12fd <main+0x12d>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     1306 <main+0x136>
lea    0x20(%rsp),%rsi
mov    %rsp,%r8
lea    0x1c(%rsp),%r9
mov    %ebp,%edi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1080 <recvfrom@plt>
test   %eax,%eax
js     130f <main+0x13f>
lea    0x4(%rsp),%rdi
mov    (%rbx),%edx
mov    $0x4,%esi
call   10d0 <gethostbyaddr@plt>
test   %rax,%rax
je     1287 <main+0xb7>
mov    (%rax),%r14
jmp    128e <main+0xbe>
lea    0xdc3(%rip),%r14
mov    0x4(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rbx
mov    (%rsp),%edi
call   1090 <ntohs@plt>
lea    0xd8d(%rip),%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd9f(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%r8
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xcfc(%rip),%rdi
jmp    1316 <main+0x146>
lea    0xd08(%rip),%rdi
jmp    1316 <main+0x146>
lea    0xd10(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i8* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [16 x i8] c"ERROR recieving\00"
@.str.3 = [27 x i8] c"+%s [%s:%d] new DATAGRAM!\0A\00"
@.str.4 = [13 x i8] c"Unknown host\00"
@.str.5 = [9 x i8] c"C=>S:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca i8*
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr_in
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
store i8* %17, i8** %11
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = bitcast %struct.sockaddr_in* %12 to i8*
%28 = call i32 @bzero(i8* %27, i32 12)
store i32 5001, i32* %8
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %8
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%39 = call i64 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %26
%42 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %26
%45 = load i32, i32* %6
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%47 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%48 = call i32 @recvfrom(i32 %45, i8* %46, i32 255, i32 0, %struct.sockaddr* %47, i32* %9)
store i32 %48, i32* %15
%49 = load i32, i32* %15
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %44
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%56 = bitcast %struct.TYPE_2__* %55 to i8*
%57 = load i32, i32* @AF_INET
%58 = call %struct.hostent* @gethostbyaddr(i8* %56, i32 4, i32 %57)
store %struct.hostent* %58, %struct.hostent** %16
%59 = load %struct.hostent*, %struct.hostent** %16
%60 = icmp ne %struct.hostent* %59, null
br i1 %60, label %61, label %65
61: ; preds = %54
%62 = load %struct.hostent*, %struct.hostent** %16
%63 = getelementptr inbounds %struct.hostent, %struct.hostent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
br label %66
65: ; preds = %54
br label %66
66: ; preds = %65, %61
%67 = phi i8* [ %64, %61 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %65 ]
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%69 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %68, i32 0, i32 0
%70 = load i32, i32* %69
%71 = call i8* @inet_ntoa(i32 %70)
%72 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @ntohs(i32 %73)
%75 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %67, i8* %71, i32 %74)
%76 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%77 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %76)
%78 = load i32, i32* %6
%79 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%80 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%81 = call i32 @sendto(i32 %78, i8* %79, i32 256, i32 0, %struct.sockaddr* %80, i32 12)
%82 = load i32, i32* %6
%83 = call i32 @close(i32 %82)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.hostent = type { i8* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [16 x i8] c"ERROR recieving\00"
@.str.3 = [27 x i8] c"+%s [%s:%d] new DATAGRAM!\0A\00"
@.str.4 = [13 x i8] c"Unknown host\00"
@.str.5 = [9 x i8] c"C=>S:%s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = call i32 @bzero(i8* %9, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = tail call i32 @htons(i32 5001)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i64 @bind(i32 %13, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%34 = call i32 @recvfrom(i32 %13, i8* %8, i32 255, i32 0, %struct.sockaddr* %33, i32* %3)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%41 = bitcast %struct.TYPE_2__* %40 to i8*
%42 = load i32, i32* @AF_INET
%43 = call %struct.hostent* @gethostbyaddr(i8* %41, i32 4, i32 %42)
%44 = icmp eq %struct.hostent* %43, null
br i1 %44, label %48, label %45
45: ; preds = %39
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i64 0, i32 0
%47 = load i8*, i8** %46
br label %48
48: ; preds = %39, %45
%49 = phi i8* [ %47, %45 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %39 ]
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i64 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @inet_ntoa(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @ntohs(i32 %54)
%56 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %49, i8* %52, i32 %55)
%57 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %8)
%58 = call i32 @sendto(i32 %13, i8* %8, i32 256, i32 0, %struct.sockaddr* %33, i32 12)
%59 = call i32 @close(i32 %13)
ret i32 0
}