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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int Make_Connection(int port, const char *buf) {
  struct sockaddr_in myaddress;
  struct hostent *host;
  int v;
  host = gethostbyname(buf);
  if (host == NULL) {
    return (-1);
  }
  myaddress.sin_family = host->h_addrtype;
  myaddress.sin_addr.s_addr = *((long *)host->h_addr);
  myaddress.sin_port = htons(port);
  v = socket(AF_INET, SOCK_STREAM, 0);
  if (v == -1) {
    return (-1);
  }
  if (connect(v, (struct sockaddr *)&myaddress, sizeof(myaddress)) < 0) {
    close(v);
    return (-1);
  }
  return (v);
}


x86 assembly (clang, -O0):
<Make_Connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1183 <Make_Connection+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <Make_Connection+0xbd>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11d8 <Make_Connection+0x88>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <Make_Connection+0xbd>
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1207 <Make_Connection+0xb7>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <Make_Connection+0xbd>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Make_Connection>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    %rsi,%rdi
call   1050 <gethostbyname@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11c5 <Make_Connection+0x75>
mov    0x8(%rax),%ecx
mov    %ecx,0x10(%rsp)
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11c5 <Make_Connection+0x75>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x18,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11be <Make_Connection+0x6e>
mov    %ebp,%ebx
jmp    11c5 <Make_Connection+0x75>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @Make_Connection(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
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %7
%11 = load %struct.hostent*, %struct.hostent** %7
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %45
14: ; preds = %2
%15 = load %struct.hostent*, %struct.hostent** %7
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %17, i32* %18
%19 = load %struct.hostent*, %struct.hostent** %7
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i64*
%23 = load i64, i64* %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i64 %23, i64* %25
%26 = load i32, i32* %4
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %14
store i32 -1, i32* %3
br label %45
35: ; preds = %14
%36 = load i32, i32* %8
%37 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%38 = call i64 @connect(i32 %36, %struct.sockaddr* %37, i32 24)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
store i32 -1, i32* %3
br label %45
43: ; preds = %35
%44 = load i32, i32* %8
store i32 %44, i32* %3
br label %45
45: ; preds = %43, %40, %34, %13
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @Make_Connection(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %1)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %28, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%12 = load i64, i64* %11
%13 = inttoptr i64 %12 to i64*
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i64 %14, i64* %15
%16 = tail call i32 @htons(i32 %0)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %28, label %22
22: ; preds = %7
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i64 @connect(i32 %20, %struct.sockaddr* %23, i32 24)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @close(i32 %20)
br label %28
28: ; preds = %22, %7, %2, %26
%29 = phi i32 [ -1, %26 ], [ -1, %2 ], [ -1, %7 ], [ %20, %22 ]
ret i32 %29
}