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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ cst_errmsg(char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int inet_addr(char const *);
int /*<<< orphan*/ memmove(TYPE_1__ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int cst_socket_open(const char *host, int port) {
  struct sockaddr_in serv_addr;
  struct hostent *serverhost;
  int fd;
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (fd < 0) {
    cst_errmsg("cst_socket: can't get socket\n");
    return -1;
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  if ((serv_addr.sin_addr.s_addr = inet_addr(host)) == -1) {
    serverhost = gethostbyname(host);
    if (serverhost == (struct hostent *)0) {
      cst_errmsg("cst_socket: gethostbyname failed\n");
      return -1;
    }
    memmove(&serv_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
  }
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != 0) {
    cst_errmsg("cst_socket: connect to server failed\n");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<cst_socket_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d4 <cst_socket_open+0x54>
lea    0xe3d(%rip),%rdi
call   1070 <cst_errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <cst_socket_open+0x113>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1243 <cst_socket_open+0xc3>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1229 <cst_socket_open+0xa9>
lea    0xe06(%rip),%rdi
call   1070 <cst_errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <cst_socket_open+0x113>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1080 <memmove@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     128d <cst_socket_open+0x10d>
lea    0xdc4(%rip),%rdi
call   1070 <cst_errmsg@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <cst_socket_open+0x113>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cst_socket_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e51(%rip),%r15
mov    (%r15),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1227 <cst_socket_open+0xa7>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    %rbp,%rdi
call   1050 <inet_addr@plt>
mov    %eax,0x10(%rsp)
cmp    $0xffffffff,%eax
jne    11f5 <cst_socket_open+0x75>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1230 <cst_socket_open+0xb0>
lea    0x10(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1080 <memmove@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
je     1241 <cst_socket_open+0xc1>
lea    0xe1b(%rip),%rdi
jmp    1237 <cst_socket_open+0xb7>
lea    0xdd2(%rip),%rdi
jmp    1237 <cst_socket_open+0xb7>
lea    0xde7(%rip),%rdi
call   1070 <cst_errmsg@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [30 x i8] c"cst_socket: can't get socket\0A\00"
@.str.1 = [34 x i8] c"cst_socket: gethostbyname failed\0A\00"
@.str.2 = [38 x i8] c"cst_socket: connect to server failed\0A\00"
define i32 @cst_socket_open(i8* %0, i32 %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 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @cst_errmsg(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %39
12: ; preds = %2
%13 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%14 = call i32 @inet_addr(i8* %0)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i64 0, i32 0
store i32 %14, i32* %16
%17 = icmp eq i32 %14, -1
br i1 %17, label %18, label %29
18: ; preds = %12
%19 = call %struct.hostent* @gethostbyname(i8* %0)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @cst_errmsg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %39
23: ; preds = %18
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memmove(%struct.TYPE_2__* %15, i32 %25, i32 %27)
br label %29
29: ; preds = %23, %12
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %30, i32* %31
%32 = call i32 @htons(i32 %1)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i64 @connect(i32 %8, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = call i32 @cst_errmsg(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %29, %37, %21, %10
%40 = phi i32 [ -1, %10 ], [ -1, %21 ], [ -1, %37 ], [ %8, %29 ]
ret i32 %40
}