#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*/ sun;
struct timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ error;

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ ETIMEDOUT;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sock_connect(const char *sockpath, int tout) {
  struct timeval timeout = {tout, 0};
  int sock, flags, error, ret;
  socklen_t len;
  struct sockaddr_un sun;
  fd_set rfds, wfds;
  sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (sock < 0)
    return -1;
  sun.sun_family = PF_LOCAL;
  snprintf(sun.sun_path, (sizeof(sun.sun_path)), "%s", sockpath);
  flags = fcntl(sock, F_GETFL, 0);
  fcntl(sock, F_SETFL, flags | O_NONBLOCK);
  ret = connect(sock, (struct sockaddr *)&sun, sizeof(sun));
  if (ret < 0 && (errno != EINPROGRESS)) {
    close(sock);
    return -1;
  }
  if (ret == 0)
    goto done;
  FD_ZERO(&rfds);
  FD_SET(sock, &rfds);
  wfds = rfds;
  ret = select(sock + 1, &rfds, &wfds, NULL, &timeout);
  if (ret == 0) {
    close(sock);
    errno = ETIMEDOUT;
    return -1;
  }
  if (FD_ISSET(sock, &rfds) || FD_ISSET(sock, &wfds)) {
    len = sizeof(error);
    if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
      close(sock);
      return -1;
    }
  } else {
    close(sock);
    return -1;
  }
done:
  return sock;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ee <sock_connect+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <sock_connect+0x202>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xdf3(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x24(%rbp),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1291 <sock_connect+0xf1>
mov    0x2d4e(%rip),%rax
mov    (%rax),%rax
mov    0x2d64(%rip),%rcx
cmp    (%rcx),%rax
je     1291 <sock_connect+0xf1>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <sock_connect+0x202>
cmpl   $0x0,-0x30(%rbp)
jne    12a0 <sock_connect+0x100>
jmp    139c <sock_connect+0x1fc>
lea    -0x44(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x24(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
lea    -0x44(%rbp),%rsi
lea    -0x48(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
call   1090 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    130b <sock_connect+0x16b>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%rcx
mov    0x2cbc(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <sock_connect+0x202>
mov    -0x24(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
jne    1337 <sock_connect+0x197>
mov    -0x24(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1383 <sock_connect+0x1e3>
movl   $0x4,-0x34(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2ca0(%rip),%rax
mov    (%rax),%esi
mov    0x2c5f(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
lea    -0x34(%rbp),%r8
call   1070 <getsockopt@plt>
cmp    $0x0,%rax
jge    137e <sock_connect+0x1de>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <sock_connect+0x202>
jmp    1397 <sock_connect+0x1f7>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a2 <sock_connect+0x202>
jmp    139c <sock_connect+0x1fc>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
mov    %esi,0x10(%rsp)
movl   $0x0,0x14(%rsp)
mov    0x2dd8(%rip),%r15
mov    (%r15),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1318 <sock_connect+0x178>
mov    %eax,%ebp
mov    (%r15),%eax
mov    %eax,0x1c(%rsp)
lea    0xe0f(%rip),%rdx
mov    $0x4,%esi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2db1(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dc0(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     1247 <sock_connect+0xa7>
jne    1261 <sock_connect+0xc1>
jmp    12ef <sock_connect+0x14f>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
jne    12f4 <sock_connect+0x154>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
mov    0x8(%rsp),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
add    $0x1,%edi
lea    0x4(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1090 <select@plt>
test   %eax,%eax
je     12fd <sock_connect+0x15d>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    12bf <sock_connect+0x11f>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12f4 <sock_connect+0x154>
movl   $0x4,0xc(%rsp)
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
mov    0x2cd9(%rip),%rax
mov    (%rax),%edx
lea    0x24(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebp,%edi
call   1070 <getsockopt@plt>
test   %rax,%rax
js     12f4 <sock_connect+0x154>
mov    %ebp,%r14d
jmp    1318 <sock_connect+0x178>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1318 <sock_connect+0x178>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%rax
mov    0x2ca3(%rip),%rcx
mov    %rax,(%rcx)
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@ETIMEDOUT = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @sock_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_un
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%16 = load i32, i32* %5
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 0, i32* %17
%18 = load i32, i32* @PF_LOCAL
%19 = load i32, i32* @SOCK_STREAM
%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 %24
23: ; preds = %2
store i32 -1, i32* %3
br label %94
24: ; preds = %2
%25 = load i32, i32* @PF_LOCAL
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i8*, i8** %4
%30 = call i32 @snprintf(i32 %28, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = load i32, i32* %7
%32 = load i32, i32* @F_GETFL
%33 = call i32 @fcntl(i32 %31, i32 %32, i32 0)
store i32 %33, i32* %8
%34 = load i32, i32* %7
%35 = load i32, i32* @F_SETFL
%36 = load i32, i32* %8
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %36, %37
%39 = call i32 @fcntl(i32 %34, i32 %35, i32 %38)
%40 = load i32, i32* %7
%41 = bitcast %struct.sockaddr_un* %12 to %struct.sockaddr*
%42 = call i32 @connect(i32 %40, %struct.sockaddr* %41, i32 8)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %24
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINPROGRESS
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %3
br label %94
52: ; preds = %45, %24
%53 = load i32, i32* %10
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %52
br label %92
56: ; preds = %52
%57 = call i32 @FD_ZERO(i32* %13)
%58 = load i32, i32* %7
%59 = call i32 @FD_SET(i32 %58, i32* %13)
%60 = load i32, i32* %13
store i32 %60, i32* %14
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
%63 = call i32 @select(i32 %62, i32* %13, i32* %14, i32* null, %struct.timeval* %6)
store i32 %63, i32* %10
%64 = load i32, i32* %10
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %56
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
%69 = load i64, i64* @ETIMEDOUT
store i64 %69, i64* @errno
store i32 -1, i32* %3
br label %94
70: ; preds = %56
%71 = load i32, i32* %7
%72 = call i64 @FD_ISSET(i32 %71, i32* %13)
%73 = icmp ne i64 %72, 0
br i1 %73, label %78, label %74
74: ; preds = %70
%75 = load i32, i32* %7
%76 = call i64 @FD_ISSET(i32 %75, i32* %14)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %88
78: ; preds = %74, %70
store i32 4, i32* %11
%79 = load i32, i32* %7
%80 = load i32, i32* @SOL_SOCKET
%81 = load i32, i32* @SO_ERROR
%82 = call i64 @getsockopt(i32 %79, i32 %80, i32 %81, i32* %9, i32* %11)
%83 = icmp slt i64 %82, 0
br i1 %83, label %84, label %87
84: ; preds = %78
%85 = load i32, i32* %7
%86 = call i32 @close(i32 %85)
store i32 -1, i32* %3
br label %94
87: ; preds = %78
br label %91
88: ; preds = %74
%89 = load i32, i32* %7
%90 = call i32 @close(i32 %89)
store i32 -1, i32* %3
br label %94
91: ; preds = %87
br label %92
92: ; preds = %91, %55
%93 = load i32, i32* %7
store i32 %93, i32* %3
br label %94
94: ; preds = %92, %88, %84, %66, %49, %23
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%s\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@ETIMEDOUT = global i64 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @sock_connect(i8* %0, i32 %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
%9 = bitcast %struct.timeval* %3 to i8*
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 %1, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i32 0, i32* %11
%12 = bitcast i32* %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = bitcast %struct.sockaddr_un* %6 to i8*
%15 = bitcast i32* %7 to i8*
%16 = bitcast i32* %8 to i8*
%17 = load i32, i32* @PF_LOCAL
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %67, label %21
21: ; preds = %2
%22 = load i32, i32* @PF_LOCAL
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i64 0, i32 1
store i32 %22, i32* %23
%24 = tail call i32 @snprintf(i32 undef, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%25 = load i32, i32* @F_GETFL
%26 = tail call i32 @fcntl(i32 %19, i32 %25, i32 0)
%27 = load i32, i32* @F_SETFL
%28 = load i32, i32* @O_NONBLOCK
%29 = or i32 %28, %26
%30 = tail call i32 @fcntl(i32 %19, i32 %27, i32 %29)
%31 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%32 = call i32 @connect(i32 %19, %struct.sockaddr* %31, i32 8)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %21
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINPROGRESS
%37 = icmp eq i64 %35, %36
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = call i32 @close(i32 %19)
br label %67
40: ; preds = %21
%41 = icmp eq i32 %32, 0
br i1 %41, label %67, label %42
42: ; preds = %34, %40
%43 = call i32 @FD_ZERO(i32* %7)
%44 = call i32 @FD_SET(i32 %19, i32* %7)
%45 = load i32, i32* %7
store i32 %45, i32* %8
%46 = add nuw nsw i32 %19, 1
%47 = call i32 @select(i32 %46, i32* %7, i32* %8, i32* null, %struct.timeval* %3)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = call i32 @close(i32 %19)
%51 = load i64, i64* @ETIMEDOUT
store i64 %51, i64* @errno
br label %67
52: ; preds = %42
%53 = call i64 @FD_ISSET(i32 %19, i32* %7)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = call i64 @FD_ISSET(i32 %19, i32* %8)
%57 = icmp eq i64 %56, 0
br i1 %57, label %65, label %58
58: ; preds = %55, %52
store i32 4, i32* %5
%59 = load i32, i32* @SOL_SOCKET
%60 = load i32, i32* @SO_ERROR
%61 = call i64 @getsockopt(i32 %19, i32 %59, i32 %60, i32* %4, i32* %5)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %58
%64 = call i32 @close(i32 %19)
br label %67
65: ; preds = %55
%66 = call i32 @close(i32 %19)
br label %67
67: ; preds = %40, %58, %2, %65, %63, %49, %38
%68 = phi i32 [ -1, %38 ], [ -1, %49 ], [ -1, %63 ], [ -1, %65 ], [ -1, %2 ], [ %19, %58 ], [ %19, %40 ]
ret i32 %68
}