#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*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, void *, int);
int /*<<< orphan*/ errno;
int gen_address(int, char *, char const *, int, int *);
int /*<<< orphan*/ log_printf(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int connect_to(const char *addr, int port, int netproto) {
  int fd, sinsize, subproto;
  char buffer[256];
  memset(buffer, 0, sizeof(buffer));
  sinsize = gen_address(netproto, buffer, addr, port, &subproto);
  if (sinsize < 0)
    return -1;
  fd = socket(netproto, SOCK_STREAM, subproto);
  if (fd <= 0)
    goto fail;
  if (connect(fd, (void *)buffer, sinsize) != 0)
    goto fail;
  return fd;
fail:
  log_printf(0, "connect_to: %s", strerror(errno));
  if (fd >= 0)
    close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    -0x24(%rbp),%r8
call   1060 <gen_address@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11cb <connect_to+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <connect_to+0xe4>
mov    -0x18(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11f1 <connect_to+0x81>
jmp    121d <connect_to+0xad>
mov    -0x1c(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     1212 <connect_to+0xa2>
jmp    121d <connect_to+0xad>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1254 <connect_to+0xe4>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
xor    %edi,%edi
lea    0xdca(%rip),%rsi
call   1050 <log_printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jl     124d <connect_to+0xdd>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
lea    0xc(%rsp),%r8
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %ebx,%ecx
call   1060 <gen_address@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1216 <connect_to+0xa6>
mov    %eax,%ebx
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0xc(%rsp),%edx
mov    %r15d,%edi
call   1090 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11e8 <connect_to+0x78>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1070 <connect@plt>
test   %rax,%rax
je     1213 <connect_to+0xa3>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe03(%rip),%rsi
xor    %edi,%edi
mov    %eax,%edx
call   1050 <log_printf@plt>
test   %ebp,%ebp
js     1216 <connect_to+0xa6>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1216 <connect_to+0xa6>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"connect_to: %s\00"
@errno = global i32 0
define i32 @connect_to(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [256 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 256)
%14 = load i32, i32* %7
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = call i32 @gen_address(i32 %14, i8* %15, i8* %16, i32 %17, i32* %10)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %49
22: ; preds = %3
%23 = load i32, i32* %7
%24 = load i32, i32* @SOCK_STREAM
%25 = load i32, i32* %10
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %22
br label %39
30: ; preds = %22
%31 = load i32, i32* %8
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%33 = load i32, i32* %9
%34 = call i64 @connect(i32 %31, i8* %32, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %30
br label %39
37: ; preds = %30
%38 = load i32, i32* %8
store i32 %38, i32* %4
br label %49
39: ; preds = %36, %29
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %8
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45, %39
store i32 -1, i32* %4
br label %49
49: ; preds = %48, %37, %21
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"connect_to: %s\00"
@errno = global i32 0
define i32 @connect_to(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 256)
%9 = call i32 @gen_address(i32 %2, i8* %7, i8* %0, i32 %1, i32* %4)
%10 = icmp slt i32 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %3
%12 = load i32, i32* @SOCK_STREAM
%13 = load i32, i32* %4
%14 = call i32 @socket(i32 %2, i32 %12, i32 %13)
%15 = icmp slt i32 %14, 1
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i64 @connect(i32 %14, i8* %7, i32 %9)
%18 = icmp eq i64 %17, 0
br i1 %18, label %26, label %19
19: ; preds = %16, %11
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 @log_printf(i32 0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = icmp sgt i32 %14, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @close(i32 %14)
br label %26
26: ; preds = %19, %24, %16, %3
%27 = phi i32 [ -1, %3 ], [ %14, %16 ], [ -1, %24 ], [ -1, %19 ]
ret i32 %27
}