#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int in_port_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int,
                           char *);
scalar_t__ resolve(char const *, int, struct sockaddr_in *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int unix_connect(const char *host, in_port_t port) {
  int fd;
  struct sockaddr_in addr;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1)
    return -1;
  if (resolve(host, port, &addr)) {
    close(fd);
    return -1;
  }
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    fprintf(stderr, "Connect to %s:%d failed: %s\n", host, port,
            strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<unix_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11a1 <unix_connect+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <unix_connect+0xe5>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1030 <resolve@plt>
cmp    $0x0,%rax
je     11cf <unix_connect+0x6f>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <unix_connect+0xe5>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
je     123f <unix_connect+0xdf>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    %rax,%r8
lea    0xdda(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1245 <unix_connect+0xe5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11f9 <unix_connect+0x99>
mov    %eax,%ebx
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <resolve@plt>
test   %rax,%rax
jne    11f2 <unix_connect+0x92>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
je     1208 <unix_connect+0xa8>
mov    0x2e0f(%rip),%rax
mov    (%rax),%r12d
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe1f(%rip),%rsi
mov    %r12d,%edi
mov    %r15,%rdx
mov    %r14d,%ecx
mov    %rax,%r8
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%ebp
jmp    11f9 <unix_connect+0x99>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Connect to %s:%d failed: %s\0A\00"
@errno = global i32 0
define i32 @unix_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %38
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i64 @resolve(i8* %15, i32 %16, %struct.sockaddr_in* %7)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* %6
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %3
br label %38
22: ; preds = %14
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 4)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %4
%30 = load i32, i32* %5
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %29, i32 %30, i8* %32)
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %3
br label %38
36: ; preds = %22
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %27, %19, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [29 x i8] c"Connect to %s:%d failed: %s\0A\00"
@errno = global i32 0
define i32 @unix_connect(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 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = call i64 @resolve(i8* %0, i32 %1, %struct.sockaddr_in* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %9
%13 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%14 = call i64 @connect(i32 %7, %struct.sockaddr* %13, i32 4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i8* %19)
br label %21
21: ; preds = %9, %16
%22 = call i32 @close(i32 %7)
br label %23
23: ; preds = %21, %12, %2
%24 = phi i32 [ -1, %2 ], [ %7, %12 ], [ -1, %21 ]
ret i32 %24
}