#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ isdigit(char);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ 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(int argc, char **argv) {
  char *hostname, *command;
  int port, sd;
  hostname = argv[1];
  port = atoi(argv[2]);
  command = argv[3];
  if (!isdigit(hostname[0])) {
    struct hostent *addr;
    if (!(addr = gethostbyname(hostname))) {
      herror(" ");
      return -1;
    }
    hostname = inet_ntoa(*((struct in_addr *)addr->h_addr));
  }
  sd = socket(PF_INET, SOCK_DGRAM, 0);
  if (sd < 0) {
    perror("socket error");
    return -1;
  }
  struct sockaddr_in sockaddr;
  sockaddr.sin_family = PF_INET;
  sockaddr.sin_addr.s_addr = inet_addr(hostname);
  sockaddr.sin_port = htons(port);
  int wc, len = sizeof sockaddr;
  wc =
      sendto(sd, command, sizeof command, 0, (struct sockaddr *)&sockaddr, len);
  if (wc < 0)
    perror("send error");
  char *s;
  s = (char *)malloc(512);
  if (s == NULL) {
    perror("malloc");
    return -1;
  }
  if ((recvfrom(sd, s, sizeof s, 0, (struct sockaddr *)&sockaddr, &len)) < 0)
    perror("recieve error");
  printf("%s\n", s);
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    1274 <main+0x94>
mov    -0x18(%rbp),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1262 <main+0x82>
lea    0xdaf(%rip),%rdi
call   10c0 <herror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <main+0x1b1>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x18(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12b2 <main+0xd2>
lea    0xd61(%rip),%rdi
call   10e0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <main+0x1b1>
mov    0x2d1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x40(%rbp)
movl   $0xc,-0x48(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%r8
mov    -0x48(%rbp),%r9d
mov    $0x8,%edx
xor    %ecx,%ecx
call   1070 <sendto@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1310 <main+0x130>
lea    0xd04(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x200,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1341 <main+0x161>
lea    0xcea(%rip),%rdi
call   10e0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <main+0x1b1>
mov    -0x28(%rbp),%edi
mov    -0x50(%rbp),%rsi
lea    -0x40(%rbp),%r8
mov    $0x8,%edx
xor    %ecx,%ecx
lea    -0x48(%rbp),%r9
call   10d0 <recvfrom@plt>
cmp    $0x0,%rax
jge    1372 <main+0x192>
lea    0xcb4(%rip),%rdi
call   10e0 <perror@plt>
mov    -0x50(%rbp),%rsi
lea    0xcb2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rbp
mov    0x10(%rsi),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r15d
mov    0x18(%rbx),%r14
movsbl 0x0(%rbp),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
jne    122e <main+0x4e>
mov    %rbp,%rdi
call   10a0 <gethostbyname@plt>
test   %rax,%rax
je     12f6 <main+0x116>
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x2da3(%rip),%r12
mov    (%r12),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     130a <main+0x12a>
mov    %eax,%ebx
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %rbp,%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
test   %eax,%eax
js     1313 <main+0x133>
mov    $0x200,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     1324 <main+0x144>
mov    %rax,%rbp
xor    %r14d,%r14d
lea    0x10(%rsp),%r8
lea    0xc(%rsp),%r9
mov    %ebx,%edi
mov    %rax,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   10d0 <recvfrom@plt>
test   %rax,%rax
js     1338 <main+0x158>
lea    0xd58(%rip),%rdi
mov    %rbp,%rsi
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd03(%rip),%rdi
call   10c0 <herror@plt>
mov    $0xffffffff,%r14d
jmp    12e6 <main+0x106>
lea    0xcf1(%rip),%rdi
jmp    132b <main+0x14b>
lea    0xcf5(%rip),%rdi
call   10e0 <perror@plt>
jmp    129b <main+0xbb>
lea    0xcef(%rip),%rdi
call   10e0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12e6 <main+0x106>
lea    0xce2(%rip),%rdi
call   10e0 <perror@plt>
jmp    12d0 <main+0xf0>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c" \00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [11 x i8] c"send error\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [14 x i8] c"recieve error\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.hostent*
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %8
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 3
%24 = load i8*, i8** %23
store i8* %24, i8** %7
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = call i32 @isdigit(i8 signext %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %44, label %30
30: ; preds = %2
%31 = load i8*, i8** %6
%32 = call %struct.hostent* @gethostbyname(i8* %31)
store %struct.hostent* %32, %struct.hostent** %10
%33 = icmp ne %struct.hostent* %32, null
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = call i32 @herror(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %91
36: ; preds = %30
%37 = load %struct.hostent*, %struct.hostent** %10
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to %struct.in_addr*
%41 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i8* @inet_ntoa(i32 %42)
store i8* %43, i8** %6
br label %44
44: ; preds = %36, %2
%45 = load i32, i32* @PF_INET
%46 = load i32, i32* @SOCK_DGRAM
%47 = call i32 @socket(i32 %45, i32 %46, i32 0)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %91
52: ; preds = %44
%53 = load i32, i32* @PF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %53, i32* %54
%55 = load i8*, i8** %6
%56 = call i32 @inet_addr(i8* %55)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %57, i32 0, i32 0
store i32 %56, i32* %58
%59 = load i32, i32* %8
%60 = call i32 @htons(i32 %59)
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %60, i32* %61
store i32 12, i32* %13
%62 = load i32, i32* %9
%63 = load i8*, i8** %7
%64 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%65 = load i32, i32* %13
%66 = call i32 @sendto(i32 %62, i8* %63, i32 8, i32 0, %struct.sockaddr* %64, i32 %65)
store i32 %66, i32* %12
%67 = load i32, i32* %12
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %52
%70 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %71
71: ; preds = %69, %52
%72 = call i64 @malloc(i32 512)
%73 = inttoptr i64 %72 to i8*
store i8* %73, i8** %14
%74 = load i8*, i8** %14
%75 = icmp eq i8* %74, null
br i1 %75, label %76, label %78
76: ; preds = %71
%77 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %91
78: ; preds = %71
%79 = load i32, i32* %9
%80 = load i8*, i8** %14
%81 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%82 = call i64 @recvfrom(i32 %79, i8* %80, i32 8, i32 0, %struct.sockaddr* %81, i32* %13)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %78
%85 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %86
86: ; preds = %84, %78
%87 = load i8*, i8** %14
%88 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %87)
%89 = load i32, i32* %9
%90 = call i32 @close(i32 %89)
store i32 0, i32* %3
br label %91
91: ; preds = %86, %76, %50, %34
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c" \00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [11 x i8] c"send error\00"
@.str.3 = [7 x i8] c"malloc\00"
@.str.4 = [14 x i8] c"recieve error\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 3
%11 = load i8*, i8** %10
%12 = load i8, i8* %6
%13 = tail call i32 @isdigit(i8 signext %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %27
15: ; preds = %2
%16 = tail call %struct.hostent* @gethostbyname(i8* %6)
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @herror(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %65
20: ; preds = %15
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to %struct.in_addr*
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %23, i64 0, i32 0
%25 = load i32, i32* %24
%26 = tail call i8* @inet_ntoa(i32 %25)
br label %27
27: ; preds = %20, %2
%28 = phi i8* [ %6, %2 ], [ %26, %20 ]
%29 = load i32, i32* @PF_INET
%30 = load i32, i32* @SOCK_DGRAM
%31 = tail call i32 @socket(i32 %29, i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %65
35: ; preds = %27
%36 = bitcast %struct.sockaddr_in* %3 to i8*
%37 = load i32, i32* @PF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %37, i32* %38
%39 = tail call i32 @inet_addr(i8* %28)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %39, i32* %40
%41 = tail call i32 @htons(i32 %9)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %41, i32* %42
%43 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%44 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%45 = call i32 @sendto(i32 %31, i8* %11, i32 8, i32 0, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %35
%48 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %35
%50 = call i64 @malloc(i32 512)
%51 = inttoptr i64 %50 to i8*
%52 = icmp eq i64 %50, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %63
55: ; preds = %49
%56 = call i64 @recvfrom(i32 %31, i8* %51, i32 8, i32 0, %struct.sockaddr* %44, i32* %4)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %51)
%62 = call i32 @close(i32 %31)
br label %63
63: ; preds = %60, %53
%64 = phi i32 [ -1, %53 ], [ 0, %60 ]
br label %65
65: ; preds = %18, %63, %33
%66 = phi i32 [ -1, %33 ], [ %64, %63 ], [ -1, %18 ]
ret i32 %66
}