#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 sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
int EINPROGRESS;
int EINTR;
int EINVAL;
int ENAMETOOLONG;
int EPIPE;
int ETIMEDOUT;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ PF_LOCAL;
int POLLERR;
int POLLHUP;
int POLLIN;
int POLLNVAL;
int POLLOUT;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int sock_connect(const char *path, int timeout_ms) {
  int ret;
  int sock;
  int flags;
  struct pollfd pfd;
  struct sockaddr_un sun;
  int saved_errno;
  if (!path) {
    errno = EINVAL;
    return -1;
  }
  if (strlen(path) >= sizeof(sun.sun_path)) {
    errno = ENAMETOOLONG;
    return -1;
  }
  sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (sock < 0)
    return -1;
  sun.sun_family = PF_LOCAL;
  strcpy(sun.sun_path, path);
  do {
    flags = fcntl(sock, F_GETFL, 0);
  } while (flags < 0 && errno == EINTR);
  if (flags < 0) {
    saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -1;
  }
  do {
    ret = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
  } while (ret < 0 && errno == EINTR);
  if (ret < 0) {
    saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -1;
  }
  do {
    ret = connect(sock, (struct sockaddr *)&sun, sizeof(sun));
  } while (ret < 0 && errno == EINTR);
  if (ret < 0 && errno != EINPROGRESS) {
    saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -1;
  }
  if (ret == 0)
    return sock;
  pfd.fd = sock;
  pfd.events = POLLIN | POLLOUT;
  do {
    ret = poll(&pfd, 1, timeout_ms);
  } while (ret < 0 && errno == EINTR);
  if (ret < 0) {
    saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -1;
  }
  if (pfd.revents & (POLLHUP | POLLNVAL | POLLERR)) {
    close(sock);
    errno = EPIPE;
    return -1;
  }
  if (ret == 0) {
    close(sock);
    errno = ETIMEDOUT;
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a8 <sock_connect+0x38>
mov    0x2ddf(%rip),%rax
mov    (%rax),%ecx
mov    0x2df6(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     11db <sock_connect+0x6b>
mov    0x2da4(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    120d <sock_connect+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x3d(%rbp)
jge    1262 <sock_connect+0xf2>
mov    0x2d3f(%rip),%rax
mov    (%rax),%eax
mov    0x2d1e(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    1225 <sock_connect+0xb5>
cmpl   $0x0,-0x20(%rbp)
jge    12a3 <sock_connect+0x133>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2cfb(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
jmp    12a8 <sock_connect+0x138>
mov    -0x1c(%rbp),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2d1a(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x3e(%rbp)
jge    12ef <sock_connect+0x17f>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    0x2c91(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x3e(%rbp)
mov    -0x3e(%rbp),%al
test   $0x1,%al
jne    12a8 <sock_connect+0x138>
cmpl   $0x0,-0x18(%rbp)
jge    1330 <sock_connect+0x1c0>
mov    0x2c85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2c6e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
jmp    1335 <sock_connect+0x1c5>
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x3f(%rbp)
jge    1370 <sock_connect+0x200>
mov    0x2c31(%rip),%rax
mov    (%rax),%eax
mov    0x2c10(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x3f(%rbp)
mov    -0x3f(%rbp),%al
test   $0x1,%al
jne    1335 <sock_connect+0x1c5>
cmpl   $0x0,-0x18(%rbp)
jge    13c9 <sock_connect+0x259>
mov    0x2c04(%rip),%rax
mov    (%rax),%eax
mov    0x2c3b(%rip),%rcx
cmp    (%rcx),%eax
je     13c9 <sock_connect+0x259>
mov    0x2bec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2bd5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
cmpl   $0x0,-0x18(%rbp)
jne    13de <sock_connect+0x26e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2bcd(%rip),%rax
mov    (%rax),%eax
mov    0x2c04(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edx
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1070 <poll@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x40(%rbp)
jge    1434 <sock_connect+0x2c4>
mov    0x2b6d(%rip),%rax
mov    (%rax),%eax
mov    0x2b4c(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x40(%rbp)
mov    -0x40(%rbp),%al
test   $0x1,%al
jne    13f9 <sock_connect+0x289>
cmpl   $0x0,-0x18(%rbp)
jge    1475 <sock_connect+0x305>
mov    0x2b40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2b29(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
mov    -0x28(%rbp),%eax
mov    0x2b61(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2b38(%rip),%rdx
or     (%rdx),%ecx
mov    0x2b37(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     14c4 <sock_connect+0x354>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0x2afb(%rip),%rax
mov    (%rax),%ecx
mov    0x2ada(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
cmpl   $0x0,-0x18(%rbp)
jne    14f4 <sock_connect+0x384>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    0x2aa3(%rip),%rax
mov    (%rax),%ecx
mov    0x2aaa(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    14fa <sock_connect+0x38a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     119b <sock_connect+0x2b>
mov    %esi,%ebx
mov    %rdi,%rbp
call   1050 <strlen@plt>
cmp    $0x4,%eax
jb     11c5 <sock_connect+0x55>
mov    0x2dcf(%rip),%rax
jmp    11a2 <sock_connect+0x32>
mov    0x2dce(%rip),%rax
mov    (%rax),%ebx
mov    0x2de5(%rip),%rax
mov    %ebx,(%rax)
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d94(%rip),%r14
mov    (%r14),%edi
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11ad <sock_connect+0x3d>
mov    %eax,%r13d
mov    %ebx,0xc(%rsp)
mov    (%r14),%eax
mov    %eax,0x24(%rsp)
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    0x2d88(%rip),%r14
mov    0x2d89(%rip),%r15
mov    0x2d6a(%rip),%r12
xchg   %ax,%ax
mov    (%r14),%esi
mov    %r13d,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%ebp
mov    (%r15),%ebx
test   %eax,%eax
jns    122c <sock_connect+0xbc>
cmp    (%r12),%ebx
je     1210 <sock_connect+0xa0>
test   %ebp,%ebp
js     12a0 <sock_connect+0x130>
mov    0x2da1(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    (%r14),%edx
or     %ebp,%edx
mov    %r13d,%edi
call   1040 <fcntl@plt>
mov    (%r15),%ebx
test   %eax,%eax
jns    1263 <sock_connect+0xf3>
cmp    (%r12),%ebx
je     1240 <sock_connect+0xd0>
test   %eax,%eax
js     12a0 <sock_connect+0x130>
lea    0x20(%rsp),%rbp
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
mov    (%r15),%ebx
test   %eax,%eax
jns    128d <sock_connect+0x11d>
cmp    (%r12),%ebx
je     1270 <sock_connect+0x100>
test   %eax,%eax
mov    0xc(%rsp),%ebp
jns    12ad <sock_connect+0x13d>
mov    0x2d34(%rip),%rcx
cmp    (%rcx),%ebx
je     12ad <sock_connect+0x13d>
mov    %r13d,%edi
call   1060 <close@plt>
jmp    11a4 <sock_connect+0x34>
test   %eax,%eax
je     11b3 <sock_connect+0x43>
mov    %r13d,0x10(%rsp)
mov    0x2cf7(%rip),%rax
mov    0x2d30(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x14(%rsp)
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   1070 <poll@plt>
mov    (%r15),%ebx
test   %eax,%eax
jns    12fc <sock_connect+0x18c>
cmp    (%r12),%ebx
je     12e0 <sock_connect+0x170>
test   %eax,%eax
js     12a0 <sock_connect+0x130>
mov    0x2cd9(%rip),%rcx
mov    0x2cb2(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2caf(%rip),%rcx
or     (%rcx),%edx
test   %edx,0x18(%rsp)
je     1335 <sock_connect+0x1c5>
mov    %r13d,%edi
call   1060 <close@plt>
mov    0x2c78(%rip),%rax
jmp    11a2 <sock_connect+0x32>
test   %eax,%eax
jne    11b3 <sock_connect+0x43>
mov    %r13d,%edi
call   1060 <close@plt>
mov    0x2c34(%rip),%rax
jmp    11a2 <sock_connect+0x32>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENAMETOOLONG = global i32 0
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@EINTR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@EINPROGRESS = global i32 0
@POLLIN = global i32 0
@POLLOUT = global i32 0
@POLLHUP = global i32 0
@POLLNVAL = global i32 0
@POLLERR = global i32 0
@EPIPE = global i32 0
@ETIMEDOUT = global i32 0
define i32 @sock_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.pollfd
%10 = alloca %struct.sockaddr_un
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i32 -1, i32* %3
br label %164
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
%20 = icmp uge i64 %19, 4
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i32, i32* @ENAMETOOLONG
store i32 %22, i32* @errno
store i32 -1, i32* %3
br label %164
23: ; preds = %16
%24 = load i32, i32* @PF_LOCAL
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 -1, i32* %3
br label %164
30: ; preds = %23
%31 = load i32, i32* @PF_LOCAL
%32 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %10, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %10, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i8*, i8** %4
%36 = call i32 @strcpy(i32 %34, i8* %35)
br label %37
37: ; preds = %48, %30
%38 = load i32, i32* %7
%39 = load i32, i32* @F_GETFL
%40 = call i32 @fcntl(i32 %38, i32 %39, i32 0)
store i32 %40, i32* %8
br label %41
41: ; preds = %37
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i32, i32* @errno
%46 = load i32, i32* @EINTR
%47 = icmp eq i32 %45, %46
br label %48
48: ; preds = %44, %41
%49 = phi i1 [ false, %41 ], [ %47, %44 ]
br i1 %49, label %37, label %50
50: ; preds = %48
%51 = load i32, i32* %8
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32, i32* @errno
store i32 %54, i32* %11
%55 = load i32, i32* %7
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %11
store i32 %57, i32* @errno
store i32 -1, i32* %3
br label %164
58: ; preds = %50
br label %59
59: ; preds = %73, %58
%60 = load i32, i32* %7
%61 = load i32, i32* @F_SETFL
%62 = load i32, i32* %8
%63 = load i32, i32* @O_NONBLOCK
%64 = or i32 %62, %63
%65 = call i32 @fcntl(i32 %60, i32 %61, i32 %64)
store i32 %65, i32* %6
br label %66
66: ; preds = %59
%67 = load i32, i32* %6
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %73
69: ; preds = %66
%70 = load i32, i32* @errno
%71 = load i32, i32* @EINTR
%72 = icmp eq i32 %70, %71
br label %73
73: ; preds = %69, %66
%74 = phi i1 [ false, %66 ], [ %72, %69 ]
br i1 %74, label %59, label %75
75: ; preds = %73
%76 = load i32, i32* %6
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %75
%79 = load i32, i32* @errno
store i32 %79, i32* %11
%80 = load i32, i32* %7
%81 = call i32 @close(i32 %80)
%82 = load i32, i32* %11
store i32 %82, i32* @errno
store i32 -1, i32* %3
br label %164
83: ; preds = %75
br label %84
84: ; preds = %95, %83
%85 = load i32, i32* %7
%86 = bitcast %struct.sockaddr_un* %10 to %struct.sockaddr*
%87 = call i32 @connect(i32 %85, %struct.sockaddr* %86, i32 8)
store i32 %87, i32* %6
br label %88
88: ; preds = %84
%89 = load i32, i32* %6
%90 = icmp slt i32 %89, 0
br i1 %90, label %91, label %95
91: ; preds = %88
%92 = load i32, i32* @errno
%93 = load i32, i32* @EINTR
%94 = icmp eq i32 %92, %93
br label %95
95: ; preds = %91, %88
%96 = phi i1 [ false, %88 ], [ %94, %91 ]
br i1 %96, label %84, label %97
97: ; preds = %95
%98 = load i32, i32* %6
%99 = icmp slt i32 %98, 0
br i1 %99, label %100, label %109
100: ; preds = %97
%101 = load i32, i32* @errno
%102 = load i32, i32* @EINPROGRESS
%103 = icmp ne i32 %101, %102
br i1 %103, label %104, label %109
104: ; preds = %100
%105 = load i32, i32* @errno
store i32 %105, i32* %11
%106 = load i32, i32* %7
%107 = call i32 @close(i32 %106)
%108 = load i32, i32* %11
store i32 %108, i32* @errno
store i32 -1, i32* %3
br label %164
109: ; preds = %100, %97
%110 = load i32, i32* %6
%111 = icmp eq i32 %110, 0
br i1 %111, label %112, label %114
112: ; preds = %109
%113 = load i32, i32* %7
store i32 %113, i32* %3
br label %164
114: ; preds = %109
%115 = load i32, i32* %7
%116 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %115, i32* %116
%117 = load i32, i32* @POLLIN
%118 = load i32, i32* @POLLOUT
%119 = or i32 %117, %118
%120 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %119, i32* %120
br label %121
121: ; preds = %131, %114
%122 = load i32, i32* %5
%123 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 %122)
store i32 %123, i32* %6
br label %124
124: ; preds = %121
%125 = load i32, i32* %6
%126 = icmp slt i32 %125, 0
br i1 %126, label %127, label %131
127: ; preds = %124
%128 = load i32, i32* @errno
%129 = load i32, i32* @EINTR
%130 = icmp eq i32 %128, %129
br label %131
131: ; preds = %127, %124
%132 = phi i1 [ false, %124 ], [ %130, %127 ]
br i1 %132, label %121, label %133
133: ; preds = %131
%134 = load i32, i32* %6
%135 = icmp slt i32 %134, 0
br i1 %135, label %136, label %141
136: ; preds = %133
%137 = load i32, i32* @errno
store i32 %137, i32* %11
%138 = load i32, i32* %7
%139 = call i32 @close(i32 %138)
%140 = load i32, i32* %11
store i32 %140, i32* @errno
store i32 -1, i32* %3
br label %164
141: ; preds = %133
%142 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%143 = load i32, i32* %142
%144 = load i32, i32* @POLLHUP
%145 = load i32, i32* @POLLNVAL
%146 = or i32 %144, %145
%147 = load i32, i32* @POLLERR
%148 = or i32 %146, %147
%149 = and i32 %143, %148
%150 = icmp ne i32 %149, 0
br i1 %150, label %151, label %155
151: ; preds = %141
%152 = load i32, i32* %7
%153 = call i32 @close(i32 %152)
%154 = load i32, i32* @EPIPE
store i32 %154, i32* @errno
store i32 -1, i32* %3
br label %164
155: ; preds = %141
%156 = load i32, i32* %6
%157 = icmp eq i32 %156, 0
br i1 %157, label %158, label %162
158: ; preds = %155
%159 = load i32, i32* %7
%160 = call i32 @close(i32 %159)
%161 = load i32, i32* @ETIMEDOUT
store i32 %161, i32* @errno
store i32 -1, i32* %3
br label %164
162: ; preds = %155
%163 = load i32, i32* %7
store i32 %163, i32* %3
br label %164
164: ; preds = %162, %158, %151, %136, %112, %104, %78, %53, %29, %21, %14
%165 = load i32, i32* %3
ret i32 %165
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENAMETOOLONG = global i32 0
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@EINTR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@EINPROGRESS = global i32 0
@POLLIN = global i32 0
@POLLOUT = global i32 0
@POLLHUP = global i32 0
@POLLNVAL = global i32 0
@POLLERR = global i32 0
@EPIPE = global i32 0
@ETIMEDOUT = global i32 0
define i32 @sock_connect(i8* %0, i32 %1) {
%3 = alloca %struct.pollfd
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.pollfd* %3 to i8*
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = icmp eq i8* %0, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @EINVAL
br label %100
10: ; preds = %2
%11 = tail call i32 @strlen(i8* %0)
%12 = icmp ugt i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = load i32, i32* @ENAMETOOLONG
br label %100
15: ; preds = %10
%16 = load i32, i32* @PF_LOCAL
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %102, label %20
20: ; preds = %15
%21 = load i32, i32* @PF_LOCAL
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %21, i32* %22
%23 = tail call i32 @strcpy(i32 undef, i8* %0)
br label %24
24: ; preds = %24, %20
%25 = load i32, i32* @F_GETFL
%26 = tail call i32 @fcntl(i32 %18, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
%28 = load i32, i32* @errno
%29 = load i32, i32* @EINTR
%30 = icmp eq i32 %28, %29
%31 = select i1 %27, i1 %30, i1 false
br i1 %31, label %24, label %32
32: ; preds = %24
br i1 %27, label %33, label %35
33: ; preds = %32
%34 = tail call i32 @close(i32 %18)
br label %100
35: ; preds = %32, %35
%36 = load i32, i32* @F_SETFL
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %37, %26
%39 = tail call i32 @fcntl(i32 %18, i32 %36, i32 %38)
%40 = icmp slt i32 %39, 0
%41 = load i32, i32* @errno
%42 = load i32, i32* @EINTR
%43 = icmp eq i32 %41, %42
%44 = select i1 %40, i1 %43, i1 false
br i1 %44, label %35, label %45
45: ; preds = %35
br i1 %40, label %48, label %46
46: ; preds = %45
%47 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
br label %50
48: ; preds = %45
%49 = tail call i32 @close(i32 %18)
br label %100
50: ; preds = %46, %50
%51 = call i32 @connect(i32 %18, %struct.sockaddr* %47, i32 8)
%52 = icmp slt i32 %51, 0
%53 = load i32, i32* @errno
%54 = load i32, i32* @EINTR
%55 = icmp eq i32 %53, %54
%56 = select i1 %52, i1 %55, i1 false
br i1 %56, label %50, label %57
57: ; preds = %50
%58 = xor i1 %52, true
%59 = load i32, i32* @EINPROGRESS
%60 = icmp eq i32 %53, %59
%61 = select i1 %58, i1 true, i1 %60
br i1 %61, label %64, label %62
62: ; preds = %57
%63 = call i32 @close(i32 %18)
br label %100
64: ; preds = %57
%65 = icmp eq i32 %51, 0
br i1 %65, label %102, label %66
66: ; preds = %64
%67 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %18, i32* %67
%68 = load i32, i32* @POLLIN
%69 = load i32, i32* @POLLOUT
%70 = or i32 %69, %68
%71 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i32 %70, i32* %71
br label %72
72: ; preds = %72, %66
%73 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 %1)
%74 = icmp slt i32 %73, 0
%75 = load i32, i32* @errno
%76 = load i32, i32* @EINTR
%77 = icmp eq i32 %75, %76
%78 = select i1 %74, i1 %77, i1 false
br i1 %78, label %72, label %79
79: ; preds = %72
br i1 %74, label %80, label %82
80: ; preds = %79
%81 = call i32 @close(i32 %18)
br label %100
82: ; preds = %79
%83 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
%84 = load i32, i32* %83
%85 = load i32, i32* @POLLHUP
%86 = load i32, i32* @POLLNVAL
%87 = or i32 %86, %85
%88 = load i32, i32* @POLLERR
%89 = or i32 %87, %88
%90 = and i32 %89, %84
%91 = icmp eq i32 %90, 0
br i1 %91, label %95, label %92
92: ; preds = %82
%93 = call i32 @close(i32 %18)
%94 = load i32, i32* @EPIPE
br label %100
95: ; preds = %82
%96 = icmp eq i32 %73, 0
br i1 %96, label %97, label %102
97: ; preds = %95
%98 = call i32 @close(i32 %18)
%99 = load i32, i32* @ETIMEDOUT
br label %100
100: ; preds = %8, %13, %33, %48, %62, %80, %92, %97
%101 = phi i32 [ %99, %97 ], [ %94, %92 ], [ %75, %80 ], [ %53, %62 ], [ %41, %48 ], [ %28, %33 ], [ %14, %13 ], [ %9, %8 ]
store i32 %101, i32* @errno
br label %102
102: ; preds = %100, %95, %64, %15
%103 = phi i32 [ -1, %15 ], [ %18, %64 ], [ %18, %95 ], [ -1, %100 ]
ret i32 %103
}