#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*/ uint16_t;
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;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sockconnect(char *name, uint16_t port) {
  int s;
  struct hostent *he;
  struct sockaddr_in addr;
  if ((he = gethostbyname(name)) == NULL)
    return -1;
  memcpy(&addr.sin_addr, he->h_addr, he->h_length);
  addr.sin_port = htons(port);
  addr.sin_family = AF_INET;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return -1;
  if (connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<sockconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1192 <sockconnect+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <sockconnect+0xc8>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    11f4 <sockconnect+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <sockconnect+0xc8>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    1222 <sockconnect+0xc2>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <sockconnect+0xc8>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockconnect>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
call   1050 <gethostbyname@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11d6 <sockconnect+0x76>
lea    0x10(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1060 <memcpy@plt>
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11d6 <sockconnect+0x76>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11cf <sockconnect+0x6f>
mov    %ebp,%ebx
jmp    11d6 <sockconnect+0x76>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @sockconnect(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 %27, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%10 = load i32, i32* %9
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @memcpy(i32* %8, i32 %10, i32 %12)
%14 = call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %16, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %27, label %21
21: ; preds = %7
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i32 @connect(i32 %19, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @close(i32 %19)
br label %27
27: ; preds = %21, %7, %2, %25
%28 = phi i32 [ -1, %25 ], [ -1, %2 ], [ -1, %7 ], [ %19, %21 ]
ret i32 %28
}