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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ free(int *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int *skbind(int /*<<< orphan*/);
int /*<<< orphan*/ skclose(int *);
int *skconnect(char *remote, short port) {
  int *sd;
  struct sockaddr_in addr;
  struct hostent *host;
  sd = skbind(0);
  if (sd == NULL)
    return NULL;
  if ((host = gethostbyname(remote)) == NULL) {
    free(sd);
    return NULL;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  memcpy(&addr.sin_addr, host->h_addr_list[0], host->h_length);
  if (connect(*sd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) ==
      -1) {
    skclose(sd);
    sd = NULL;
  }
  return sd;
}


x86 assembly (clang, -O0):
<skconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
xor    %edi,%edi
call   1090 <skbind@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b6 <skconnect+0x36>
movq   $0x0,-0x8(%rbp)
jmp    125a <skconnect+0xda>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e3 <skconnect+0x63>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    125a <skconnect+0xda>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movswl -0x12(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1252 <skconnect+0xd2>
mov    -0x20(%rbp),%rdi
call   1080 <skclose@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skconnect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
xor    %edi,%edi
call   1090 <skbind@plt>
test   %rax,%rax
je     1214 <skconnect+0x94>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     120c <skconnect+0x8c>
mov    %rax,%rbp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movswl %r14w,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x8(%rbp),%rax
mov    (%rax),%esi
mov    0x0(%rbp),%edx
mov    %r15,%rdi
call   1070 <memcpy@plt>
mov    (%rbx),%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1216 <skconnect+0x96>
mov    %rbx,%rdi
call   1080 <skclose@plt>
jmp    1214 <skconnect+0x94>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
define i32* @skconnect(i8* %0, i16 signext %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i16
%6 = alloca i32*
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
store i8* %0, i8** %4
store i16 %1, i16* %5
%9 = call i32* @skbind(i32 0)
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32* null, i32** %3
br label %47
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call %struct.hostent* @gethostbyname(i8* %14)
store %struct.hostent* %15, %struct.hostent** %8
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32*, i32** %6
%19 = call i32 @free(i32* %18)
store i32* null, i32** %3
br label %47
20: ; preds = %13
%21 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i16, i16* %5
%25 = call i32 @htons(i16 signext %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%28 = load %struct.hostent*, %struct.hostent** %8
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 1
%30 = load i32*, i32** %29
%31 = getelementptr inbounds i32, i32* %30, i64 0
%32 = load i32, i32* %31
%33 = load %struct.hostent*, %struct.hostent** %8
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i32* %27, i32 %32, i32 %35)
%37 = load i32*, i32** %6
%38 = load i32, i32* %37
%39 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%40 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %20
%43 = load i32*, i32** %6
%44 = call i32 @skclose(i32* %43)
store i32* null, i32** %6
br label %45
45: ; preds = %42, %20
%46 = load i32*, i32** %6
store i32* %46, i32** %3
br label %47
47: ; preds = %45, %17, %12
%48 = load i32*, i32** %3
ret i32* %48
}

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