#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_storage {
  int /*<<< orphan*/ ss_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ EINPROGRESS;
int SOCK_DGRAM;
int SOCK_STREAM;
int /*<<< orphan*/ addrsize(struct sockaddr_storage const *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ make_async(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int connect_to(int dgram, const struct sockaddr_storage *ss) {
  int type = dgram ? SOCK_DGRAM : SOCK_STREAM;
  int s = socket(ss->ss_family, type, 0);
  if (s < 0) {
    perror("socket");
    return -1;
  }
  make_async(s);
  if (connect(s, (struct sockaddr *)ss, addrsize(ss)) < 0 &&
      errno != EINPROGRESS) {
    perror("connect");
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     118a <connect_to+0x2a>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1196 <connect_to+0x36>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11d1 <connect_to+0x71>
lea    0xe40(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <connect_to+0xe8>
mov    -0x18(%rbp),%edi
call   1030 <make_async@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <addrsize@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1242 <connect_to+0xe2>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    0x2dcf(%rip),%rcx
cmp    (%rcx),%rax
je     1242 <connect_to+0xe2>
lea    0xdde(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1248 <connect_to+0xe8>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
test   %edi,%edi
jne    1174 <connect_to+0x14>
mov    0x2e5e(%rip),%rax
jmp    117b <connect_to+0x1b>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    (%r14),%edi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11cd <connect_to+0x6d>
mov    %eax,%ebx
mov    %eax,%edi
call   1030 <make_async@plt>
mov    %r14,%rdi
call   1050 <addrsize@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <connect@plt>
test   %rax,%rax
jns    11c3 <connect_to+0x63>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2e2a(%rip),%rcx
cmp    (%rcx),%rax
jne    11db <connect_to+0x7b>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2c(%rip),%rdi
call   1060 <perror@plt>
jmp    11ee <connect_to+0x8e>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
jmp    11c3 <connect_to+0x63>

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

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