#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;
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(void *, void const *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(void *, 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 saddr;
  int sock_id = socket(AF_INET, SOCK_STREAM, 0);
  struct hostent *hp = gethostbyname(argv[1]);
  memset((void *)&saddr, 0, sizeof(saddr));
  memcpy((void *)&saddr.sin_addr, (const void *)hp->h_addr_list[0],
         hp->h_length);
  saddr.sin_port = htons(atoi(argv[2]));
  saddr.sin_family = AF_INET;
  connect(sock_id, (struct sockaddr *)&saddr, sizeof(saddr));
  char message[BUFSIZ];
  int messlen = read(sock_id, message, BUFSIZ);
  write(1, message, messlen);
  close(sock_id);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    %edx,-0x6c(%rbp)
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    -0x6c(%rbp),%esi
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
mov    %rdi,-0x68(%rbp)
mov    $0xc,%edx
mov    %edx,-0x5c(%rbp)
call   1050 <memset@plt>
lea    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rcx
mov    (%rcx),%rsi
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    -0x68(%rbp),%rsi
mov    -0x5c(%rbp),%edx
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,-0x1c(%rbp)
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   10b0 <connect@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x50(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%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,%r12
mov    0x2e2d(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
mov    0x8(%r12),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,%rbx
lea    -0x38(%rbp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    0x8(%rbx),%rax
mov    (%rax),%rsi
mov    (%rbx),%edx
call   1090 <memcpy@plt>
mov    0x10(%r12),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x0(%r13),%eax
mov    %eax,-0x38(%rbp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %rsp,%r15
mov    0x2d97(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %r15,%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, i64* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@BUFSIZ = global i32 0
@EXIT_SUCCESS = 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 i32
%8 = alloca %struct.hostent*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %7
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %8
%19 = bitcast %struct.sockaddr_in* %6 to i8*
%20 = call i32 @memset(i8* %19, i32 0, i32 12)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%22 = bitcast i32* %21 to i8*
%23 = load %struct.hostent*, %struct.hostent** %8
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 1
%25 = load i64*, i64** %24
%26 = getelementptr inbounds i64, i64* %25, i64 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i8*
%29 = load %struct.hostent*, %struct.hostent** %8
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @memcpy(i8* %22, i8* %28, i32 %31)
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %7
%42 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%43 = call i32 @connect(i32 %41, %struct.sockaddr* %42, i32 12)
%44 = load i32, i32* @BUFSIZ
%45 = zext i32 %44 to i64
%46 = call i8* @llvm.stacksave()
store i8* %46, i8** %9
%47 = alloca i8, i64 %45
store i64 %45, i64* %10
%48 = load i32, i32* %7
%49 = load i32, i32* @BUFSIZ
%50 = call i32 @read(i32 %48, i8* %47, i32 %49)
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = call i32 @write(i32 1, i8* %47, i32 %51)
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @EXIT_SUCCESS
store i32 %55, i32* %3
%56 = load i8*, i8** %9
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@BUFSIZ = global i32 0
@EXIT_SUCCESS = 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* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call %struct.hostent* @gethostbyname(i8* %9)
%11 = call i32 @memset(i8* %4, i32 0, i32 12)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%13 = bitcast i32* %12 to i8*
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%15 = load i64*, i64** %14
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i8*
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i8* %13, i8* %17, i32 %19)
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @connect(i32 %7, %struct.sockaddr* %28, i32 12)
%30 = load i32, i32* @BUFSIZ
%31 = zext i32 %30 to i64
%32 = call i8* @llvm.stacksave()
%33 = alloca i8, i64 %31
%34 = load i32, i32* @BUFSIZ
%35 = call i32 @read(i32 %7, i8* %33, i32 %34)
%36 = call i32 @write(i32 1, i8* %33, i32 %35)
%37 = call i32 @close(i32 %7)
%38 = load i32, i32* @EXIT_SUCCESS
ret i32 %38
}