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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct in_addr *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  printf("This is  daytime\n");
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <hostname>\n", argv[0]);
    exit(-1);
  }
  int socketfd;
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in servAddr;
  struct hostent *hostEntry;
  struct in_addr **pptr;
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family = AF_INET;
  servAddr.sin_port = htons(49999);
  hostEntry = gethostbyname(argv[1]);
  if (hostEntry == NULL) {
    herror("gethostbyname()");
    exit(1);
  }
  pptr = (struct in_addr **)hostEntry->h_addr_list;
  memcpy(&servAddr.sin_addr, *pptr, sizeof(struct in_addr));
  if (connect(socketfd, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
    perror("connect()");
    exit(1);
  }
  char socket_buffer[512];
  read(socketfd, socket_buffer, 512);
  printf("%s\n", socket_buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x2,-0x8(%rbp)
je     1217 <main+0x57>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe0a(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xc34f,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    128e <main+0xce>
lea    0xda9(%rip),%rdi
call   10a0 <herror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    12df <main+0x11f>
lea    0xd68(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
lea    -0x230(%rbp),%rsi
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%ebx
jne    1285 <main+0xc5>
mov    0x2dfc(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0xc34f,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%r14),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     12a7 <main+0xe7>
mov    (%rax),%rax
mov    (%rax),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x4,%edx
call   1090 <memcpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     12bd <main+0xfd>
lea    0x10(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd68(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xd68(%rip),%rdi
call   10a0 <herror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd62(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"This is daytime\0A\00"
@stderr = global i32 0
@.str.1 = [22 x i8] c"Usage: %s <hostname>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [16 x i8] c"gethostbyname()\00"
@.str.3 = [10 x i8] c"connect()\00"
@.str.4 = [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 i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca %struct.in_addr**
%10 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @htons(i32 49999)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call %struct.hostent* @gethostbyname(i8* %32)
store %struct.hostent* %33, %struct.hostent** %8
%34 = load %struct.hostent*, %struct.hostent** %8
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %39
36: ; preds = %21
%37 = call i32 @herror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %21
%40 = load %struct.hostent*, %struct.hostent** %8
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to %struct.in_addr**
store %struct.in_addr** %43, %struct.in_addr*** %9
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%45 = load %struct.in_addr**, %struct.in_addr*** %9
%46 = load %struct.in_addr*, %struct.in_addr** %45
%47 = call i32 @memcpy(i32* %44, %struct.in_addr* %46, i32 4)
%48 = load i32, i32* %6
%49 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%50 = call i64 @connect(i32 %48, %struct.sockaddr* %49, i32 12)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %39
%53 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %39
%56 = load i32, i32* %6
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 512)
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %59)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str.1 = [22 x i8] c"Usage: %s <hostname>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [16 x i8] c"gethostbyname()\00"
@.str.3 = [10 x i8] c"connect()\00"
@str = [17 x i8] c"This is daytime\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%6 = icmp eq i32 %0, 2
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = bitcast %struct.sockaddr_in* %3 to i8*
%17 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 49999)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = call %struct.hostent* @gethostbyname(i8* %23)
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %29
26: ; preds = %12
%27 = call i32 @herror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %12
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.in_addr**
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%34 = load %struct.in_addr*, %struct.in_addr** %32
%35 = call i32 @memcpy(i32* %33, %struct.in_addr* %34, i32 4)
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i64 @connect(i32 %15, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %29
%40 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%44 = call i32 @read(i32 %15, i8* %43, i32 512)
%45 = call i32 @puts(i8* %43)
ret i32 0
}