#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_family;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ server_add;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  struct sockaddr_in server_add;
  struct hostent *hp;
  int socket_id;
  char message[BUFSIZ];
  int messlen;
  socket_id = socket(AF_INET, SOCK_STREAM, 0);
  memset(&server_add, 0, sizeof(server_add));
  hp = gethostbyname(argv[1]);
  if (hp == NULL) {
    return 1;
  }
  memcpy(&server_add.sin_addr, hp->h_addr, hp->h_length);
  server_add.sin_port = htons(atoi(argv[2]));
  server_add.sin_family = AF_INET;
  if (connect(socket_id, (struct sockaddr *)&server_add, sizeof(server_add)) !=
      0) {
    return 2;
  }
  messlen = read(socket_id, message, BUFSIZ);
  if (messlen == -1) {
    return 3;
  }
  write(1, message, messlen);
  close(socket_id);
  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    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123d <main+0x9d>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1304 <main+0x164>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1090 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
je     12a8 <main+0x108>
movl   $0x2,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1304 <main+0x164>
mov    -0x50(%rbp),%rsi
mov    -0x2c(%rbp),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    12dd <main+0x13d>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    1304 <main+0x164>
mov    -0x50(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rsp,%r12
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e10(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x8(%r15),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1287 <main+0xe7>
lea    -0x30(%rbp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1090 <memcpy@plt>
mov    0x10(%r15),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x0(%r13),%eax
mov    %eax,-0x38(%rbp)
lea    -0x38(%rbp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %rax,%rcx
mov    $0x2,%eax
test   %rcx,%rcx
jne    1293 <main+0xf3>
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     128e <main+0xee>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    1293 <main+0xf3>
mov    $0x1,%eax
jmp    1293 <main+0xf3>
mov    $0x3,%eax
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %8
%20 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call %struct.hostent* @gethostbyname(i8* %23)
store %struct.hostent* %24, %struct.hostent** %7
%25 = load %struct.hostent*, %struct.hostent** %7
%26 = icmp eq %struct.hostent* %25, null
br i1 %26, label %27, label %28
27: ; preds = %2
store i32 1, i32* %3
store i32 1, i32* %12
br label %62
28: ; preds = %2
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%30 = load %struct.hostent*, %struct.hostent** %7
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.hostent*, %struct.hostent** %7
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i32* %29, i32 %32, i32 %35)
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %41, i32* %42
%43 = load i32, i32* @AF_INET
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* %8
%46 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%47 = call i64 @connect(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %28
store i32 2, i32* %3
store i32 1, i32* %12
br label %62
50: ; preds = %28
%51 = load i32, i32* %8
%52 = load i32, i32* @BUFSIZ
%53 = call i32 @read(i32 %51, i8* %16, i32 %52)
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %57
56: ; preds = %50
store i32 3, i32* %3
store i32 1, i32* %12
br label %62
57: ; preds = %50
%58 = load i32, i32* %11
%59 = call i32 @write(i32 1, i8* %16, i32 %58)
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
store i32 1, i32* %12
br label %62
62: ; preds = %57, %56, %49, %27
%63 = load i8*, i8** %9
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call %struct.hostent* @gethostbyname(i8* %14)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %41, label %17
17: ; preds = %2
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @memcpy(i32* %18, i32 %20, i32 %22)
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @connect(i32 %11, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %17
%35 = load i32, i32* @BUFSIZ
%36 = call i32 @read(i32 %11, i8* %8, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %41, label %38
38: ; preds = %34
%39 = call i32 @write(i32 1, i8* %8, i32 %36)
%40 = call i32 @close(i32 %11)
br label %41
41: ; preds = %34, %17, %2, %38
%42 = phi i32 [ 0, %38 ], [ 1, %2 ], [ 2, %17 ], [ 3, %34 ]
ret i32 %42
}