#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int rc;
  int s;
  char buf[1024 + 1];
  char *pc;
  struct sockaddr_in sa;
  struct hostent *hen;
  if (argc < 2) {
    fprintf(stderr, "Missing host name\n");
    exit(1);
  }
  hen = gethostbyname(argv[1]);
  if (!hen) {
    perror("couldn't resolve host name");
  }
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(13);
  memcpy(&sa.sin_addr.s_addr, hen->h_addr_list[0], hen->h_length);
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    perror("socket: allocation failed");
  }
  rc = connect(s, (struct sockaddr *)&sa, sizeof(sa));
  if (rc) {
    perror("connect");
  }
  pc = buf;
  while (rc = read(s, pc, 1024 - (pc - buf))) {
    pc += rc;
  }
  close(s);
  *pc = ' ';
  printf("Time: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1202 <main+0x42>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    1230 <main+0x70>
lea    0xde8(%rip),%rdi
call   10b0 <perror@plt>
lea    -0x438(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,-0x434(%rbp)
lea    -0x438(%rbp),%rdi
mov    -0x440(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x440(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12b6 <main+0xf6>
lea    0xd7d(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x438(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12e3 <main+0x123>
lea    0xd6a(%rip),%rdi
call   10b0 <perror@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x428(%rbp),%rsi
mov    -0x428(%rbp),%rcx
lea    -0x420(%rbp),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%edx
call   1070 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1343 <main+0x183>
mov    -0x14(%rbp),%ecx
mov    -0x428(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x428(%rbp)
jmp    12f1 <main+0x131>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x428(%rbp),%rax
movb   $0x20,(%rax)
lea    -0x420(%rbp),%rsi
lea    0xced(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
cmp    $0x1,%edi
jle    12fa <main+0x13a>
mov    0x8(%rsi),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,%rbx
test   %rax,%rax
je     12c7 <main+0x107>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2de6(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rbx),%rax
mov    (%rax),%esi
mov    (%rbx),%edx
mov    %r14,%rdi
call   10a0 <memcpy@plt>
mov    0x0(%rbp),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12d8 <main+0x118>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %eax,%eax
jne    12e9 <main+0x129>
lea    0x10(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
je     129e <main+0xde>
lea    0x10(%rsp),%r14
mov    %r14,%rbx
nopl   0x0(%rax,%rax,1)
cltq
add    %rax,%rbx
mov    %r14d,%edx
sub    %ebx,%edx
add    $0x400,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
test   %eax,%eax
jne    1280 <main+0xc0>
mov    %ebp,%edi
call   1060 <close@plt>
movb   $0x20,(%rbx)
lea    0xda1(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd45(%rip),%rdi
call   10b0 <perror@plt>
jmp    11e9 <main+0x29>
lea    0xd4f(%rip),%rdi
call   10b0 <perror@plt>
jmp    1244 <main+0x84>
lea    0xd58(%rip),%rdi
call   10b0 <perror@plt>
jmp    125b <main+0x9b>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0xcf6(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Missing host name\0A\00"
@.str.1 = [27 x i8] c"couldn't resolve host name\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [26 x i8] c"socket: allocation failed\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [10 x i8] c"Time: %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 [1025 x i8]
%9 = alloca i8*
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call %struct.hostent* @gethostbyname(i8* %21)
store %struct.hostent* %22, %struct.hostent** %11
%23 = load %struct.hostent*, %struct.hostent** %11
%24 = icmp ne %struct.hostent* %23, null
br i1 %24, label %27, label %25
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 13)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load %struct.hostent*, %struct.hostent** %11
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 1
%37 = load i32*, i32** %36
%38 = getelementptr inbounds i32, i32* %37, i64 0
%39 = load i32, i32* %38
%40 = load %struct.hostent*, %struct.hostent** %11
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(i32* %34, i32 %39, i32 %42)
%44 = load i32, i32* @AF_INET
%45 = load i32, i32* @SOCK_STREAM
%46 = call i32 @socket(i32 %44, i32 %45, i32 0)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %27
%50 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %27
%52 = load i32, i32* %7
%53 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%54 = call i32 @connect(i32 %52, %struct.sockaddr* %53, i32 12)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %51
%60 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
store i8* %60, i8** %9
br label %61
61: ; preds = %73, %59
%62 = load i32, i32* %7
%63 = load i8*, i8** %9
%64 = load i8*, i8** %9
%65 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%66 = ptrtoint i8* %64 to i64
%67 = ptrtoint i8* %65 to i64
%68 = sub i64 %66, %67
%69 = sub nsw i64 1024, %68
%70 = trunc i64 %69 to i32
%71 = call i32 @read(i32 %62, i8* %63, i32 %70)
store i32 %71, i32* %6
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %78
73: ; preds = %61
%74 = load i32, i32* %6
%75 = load i8*, i8** %9
%76 = sext i32 %74 to i64
%77 = getelementptr inbounds i8, i8* %75, i64 %76
store i8* %77, i8** %9
br label %61
78: ; preds = %61
%79 = load i32, i32* %7
%80 = call i32 @close(i32 %79)
%81 = load i8*, i8** %9
store i8 32, i8* %81
%82 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%83 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %82)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [19 x i8] c"Missing host name\0A\00"
@.str.1 = [27 x i8] c"couldn't resolve host name\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [26 x i8] c"socket: allocation failed\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [10 x i8] c"Time: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1025 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call %struct.hostent* @gethostbyname(i8* %14)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 13)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 1
%27 = load i32*, i32** %26
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @memcpy(i32* %25, i32 %28, i32 %30)
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_STREAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %19
%37 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %19
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = call i32 @connect(i32 %34, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
%45 = ptrtoint [1025 x i8]* %3 to i64
%46 = call i32 @read(i32 %34, i8* %5, i32 1024)
%47 = icmp eq i32 %46, 0
br i1 %47, label %59, label %48
48: ; preds = %44, %48
%49 = phi i32 [ %57, %48 ], [ %46, %44 ]
%50 = phi i8* [ %52, %48 ], [ %5, %44 ]
%51 = sext i32 %49 to i64
%52 = getelementptr inbounds i8, i8* %50, i64 %51
%53 = ptrtoint i8* %52 to i64
%54 = sub i64 %45, %53
%55 = trunc i64 %54 to i32
%56 = add i32 %55, 1024
%57 = call i32 @read(i32 %34, i8* %52, i32 %56)
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %48
59: ; preds = %48, %44
%60 = phi i8* [ %5, %44 ], [ %52, %48 ]
%61 = call i32 @close(i32 %34)
store i8 32, i8* %60
%62 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %5)
ret i32 0
}