#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 */
typedef int /*<<< orphan*/ u_short;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct protoent {
  int /*<<< orphan*/ p_proto;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ socketAddress;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_client_socket(char *host, int port) {
  struct sockaddr_in socketAddress;
  memset((char *)&socketAddress, 0, sizeof(socketAddress));
  socketAddress.sin_family = AF_INET;
  socketAddress.sin_port = htons((u_short)port);
  struct hostent *ptrh = gethostbyname(host);
  if (ptrh == NULL) {
    perror("gethostbyname");
    exit(1);
  }
  memcpy(&socketAddress.sin_addr, ptrh->h_addr, ptrh->h_length);
  struct protoent *ptrp = getprotobyname("tcp");
  if (ptrp == NULL) {
    perror("getprotobyname");
    exit(1);
  }
  int sock = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
  if (sock < 0) {
    perror("socket");
    exit(1);
  }
  if (connect(sock, (struct sockaddr *)&socketAddress, sizeof(socketAddress)) <
      0) {
    perror("connect");
    exit(1);
  }
  return sock;
}


x86 assembly (clang, -O0):
<open_client_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f4 <open_client_socket+0x64>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
lea    0xdfd(%rip),%rdi
call   1070 <getprotobyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123b <open_client_socket+0xab>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    127b <open_client_socket+0xeb>
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12ac <open_client_socket+0x11c>
lea    0xd8b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_client_socket>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1231 <open_client_socket+0xa1>
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    0x8(%rsp),%rdi
call   1060 <memcpy@plt>
lea    0xe26(%rip),%rdi
call   1070 <getprotobyname@plt>
test   %rax,%rax
je     123a <open_client_socket+0xaa>
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%edi
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1243 <open_client_socket+0xb3>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     124c <open_client_socket+0xbc>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdc8(%rip),%rdi
jmp    1253 <open_client_socket+0xc3>
lea    0xdd1(%rip),%rdi
jmp    1253 <open_client_socket+0xc3>
lea    0xdd7(%rip),%rdi
jmp    1253 <open_client_socket+0xc3>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [14 x i8] c"gethostbyname\00"
@.str.1 = [4 x i8] c"tcp\00"
@.str.2 = [15 x i8] c"getprotobyname\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [7 x i8] c"socket\00"
@.str.4 = [8 x i8] c"connect\00"
define i32 @open_client_socket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @memset(i8* %4, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = call i32 @htons(i32 %1)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = call %struct.hostent* @gethostbyname(i8* %0)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i32* %16, i32 %18, i32 %20)
%22 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq %struct.protoent* %22, null
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %15
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = getelementptr inbounds %struct.protoent, %struct.protoent* %22, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %28, i32 %29, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i64 @connect(i32 %32, %struct.sockaddr* %38, i32 12)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
ret i32 %32
}