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

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ h_errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
char *sock_herrstr(int /*<<< orphan*/);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int sock_open(const char *host, int port) {
  struct hostent *hp;
  struct sockaddr_in server;
  int fd;
  hp = gethostbyname(host);
  if (hp) {
    server.sin_family = AF_INET;
    bzero((char *)&server, sizeof(server));
    server.sin_family = AF_INET;
    bcopy((char *)hp->h_addr, (char *)&server.sin_addr.s_addr, hp->h_length);
    server.sin_port = htons(port);
  }
  if (hp == NULL) {
    fprintf(stderr, "sock_open: host information for %s not found: %s\n", host,
            sock_herrstr(h_errno));
    return -1;
  }
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("sock_open: (err) socket");
    return -1;
  }
  if (connect(fd, (struct sockaddr *)&server, sizeof(struct sockaddr)) < 0) {
    perror("sock_open: (err) connect");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<sock_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1217 <sock_open+0x77>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x10,%esi
call   10a0 <bzero@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1266 <sock_open+0xc6>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1070 <sock_herrstr@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <sock_open+0x145>
mov    0x2d7b(%rip),%rax
mov    (%rax),%rdi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    12a4 <sock_open+0x104>
lea    0xd9f(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <sock_open+0x145>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    12df <sock_open+0x13f>
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e5 <sock_open+0x145>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1240 <sock_open+0xa0>
mov    %rax,%rbp
mov    0x2e20(%rip),%r15
mov    (%r15),%rbx
lea    0x8(%rsp),%rdi
mov    $0x10,%esi
call   10a0 <bzero@plt>
mov    %rbx,0x10(%rsp)
mov    0x8(%rbp),%rsi
lea    0xc(%rsp),%rdi
mov    0x0(%rbp),%edx
call   1080 <memmove@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r15),%rdi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     127d <sock_open+0xdd>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   10b0 <connect@plt>
test   %rax,%rax
jns    1270 <sock_open+0xd0>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    126b <sock_open+0xcb>
mov    0x2d89(%rip),%rax
mov    (%rax),%ebp
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1070 <sock_herrstr@plt>
lea    0xda2(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdae(%rip),%rdi
call   1090 <perror@plt>
jmp    126b <sock_open+0xcb>

LLVM IR (-O0):
%struct.hostent = type { i32, i64 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@stderr = global i32 0
@.str = [50 x i8] c"sock_open: host information for %s not found: %s\0A\00"
@h_errno = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"sock_open: (err) socket\00"
@.str.2 = [25 x i8] c"sock_open: (err) connect\00"
define i32 @sock_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %6
%11 = load %struct.hostent*, %struct.hostent** %6
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %13, label %34
13: ; preds = %2
%14 = load i8*, i8** @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i8* %14, i8** %15
%16 = bitcast %struct.sockaddr_in* %7 to i8*
%17 = call i32 @bzero(i8* %16, i32 16)
%18 = load i8*, i8** @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i8* %18, i8** %19
%20 = load %struct.hostent*, %struct.hostent** %6
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 1
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = bitcast i32* %25 to i8*
%27 = load %struct.hostent*, %struct.hostent** %6
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @bcopy(i8* %23, i8* %26, i32 %29)
%31 = load i32, i32* %5
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %32, i32* %33
br label %34
34: ; preds = %13, %2
%35 = load %struct.hostent*, %struct.hostent** %6
%36 = icmp eq %struct.hostent* %35, null
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %4
%40 = load i32, i32* @h_errno
%41 = call i8* @sock_herrstr(i32 %40)
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %39, i8* %41)
store i32 -1, i32* %3
br label %61
43: ; preds = %34
%44 = load i8*, i8** @AF_INET
%45 = load i32, i32* @SOCK_STREAM
%46 = call i32 @socket(i8* %44, i32 %45, i32 0)
store i32 %46, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %61
50: ; preds = %43
%51 = load i32, i32* %8
%52 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%53 = call i64 @connect(i32 %51, %struct.sockaddr* %52, i32 4)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %3
br label %61
59: ; preds = %50
%60 = load i32, i32* %8
store i32 %60, i32* %3
br label %61
61: ; preds = %59, %55, %48, %37
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i8* }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i8* null
@stderr = global i32 0
@.str = [50 x i8] c"sock_open: host information for %s not found: %s\0A\00"
@h_errno = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"sock_open: (err) socket\00"
@.str.2 = [25 x i8] c"sock_open: (err) connect\00"
define i32 @sock_open(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = load i8*, i8** @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%10 = call i32 @bzero(i8* %4, i32 16)
store i8* %8, i8** %9
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%12 = load i64, i64* %11
%13 = inttoptr i64 %12 to i8*
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%15 = bitcast i32* %14 to i8*
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @htons(i32 %1)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i8*, i8** @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = tail call i32 @socket(i8* %20, i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %29, label %31
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = load i32, i32* @h_errno
%27 = tail call i8* @sock_herrstr(i32 %26)
%28 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %27)
br label %38
29: ; preds = %7
%30 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %38
31: ; preds = %7
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i64 @connect(i32 %22, %struct.sockaddr* %32, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @close(i32 %22)
br label %38
38: ; preds = %31, %35, %29, %24
%39 = phi i32 [ -1, %24 ], [ -1, %29 ], [ -1, %35 ], [ %22, %31 ]
ret i32 %39
}