#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_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
scalar_t__ EINPROGRESS;
int anetCreateSocket(char *, int /*<<< orphan*/);
scalar_t__ anetNonBlock(char *, int);
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int anetUnixGenericConnect(char *err, const char *path, int flags) {
  int s;
  struct sockaddr_un sa;
  if ((s = anetCreateSocket(err, AF_LOCAL)) == -1)
    return -1;
  sa.sun_family = AF_LOCAL;
  strncpy(sa.sun_path, path, sizeof(sa.sun_path) - 1);
  if (flags & 1) {
    if (anetNonBlock(err, s) != 0) {
      close(s);
      return -1;
    }
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    if (errno == EINPROGRESS && flags & 1)
      return s;
    anetSetError(err, "connect: %s", strerror(errno));
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<anetUnixGenericConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
call   1060 <anetCreateSocket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11ad <anetUnixGenericConnect+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <anetUnixGenericConnect+0x129>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1208 <anetUnixGenericConnect+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <anetNonBlock@plt>
cmp    $0x0,%rax
je     1203 <anetUnixGenericConnect+0x93>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <anetUnixGenericConnect+0x129>
jmp    1208 <anetUnixGenericConnect+0x98>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1293 <anetUnixGenericConnect+0x123>
mov    0x2da7(%rip),%rax
mov    (%rax),%rax
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
jne    1256 <anetUnixGenericConnect+0xe6>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1256 <anetUnixGenericConnect+0xe6>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1299 <anetUnixGenericConnect+0x129>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xd86(%rip),%rsi
call   1070 <anetSetError@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1299 <anetUnixGenericConnect+0x129>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetUnixGenericConnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e55(%rip),%r13
mov    0x0(%r13),%esi
call   1060 <anetCreateSocket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     121d <anetUnixGenericConnect+0xad>
mov    %eax,%ebp
mov    0x0(%r13),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rsi
mov    $0x3,%edx
call   1040 <strncpy@plt>
test   $0x1,%r15b
je     11cb <anetUnixGenericConnect+0x5b>
mov    %r12,%rdi
mov    %ebp,%esi
call   1030 <anetNonBlock@plt>
test   %rax,%rax
jne    1216 <anetUnixGenericConnect+0xa6>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    11fb <anetUnixGenericConnect+0x8b>
mov    0x2dea(%rip),%rax
mov    (%rax),%rdi
test   $0x1,%r15b
je     1200 <anetUnixGenericConnect+0x90>
mov    0x2df2(%rip),%rax
cmp    (%rax),%rdi
jne    1200 <anetUnixGenericConnect+0x90>
mov    %ebp,%r14d
jmp    121d <anetUnixGenericConnect+0xad>
call   1090 <strerror@plt>
lea    0xdf4(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   1070 <anetSetError@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [12 x i8] c"connect: %s\00"
define i32 @anetUnixGenericConnect(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @AF_LOCAL
%12 = call i32 @anetCreateSocket(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %58
15: ; preds = %3
%16 = load i32, i32* @AF_LOCAL
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i8*, i8** %6
%21 = call i32 @strncpy(i32 %19, i8* %20, i32 3)
%22 = load i32, i32* %7
%23 = and i32 %22, 1
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %15
%26 = load i8*, i8** %5
%27 = load i32, i32* %8
%28 = call i64 @anetNonBlock(i8* %26, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %4
br label %58
33: ; preds = %25
br label %34
34: ; preds = %33, %15
%35 = load i32, i32* %8
%36 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 8)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %56
39: ; preds = %34
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINPROGRESS
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i32, i32* %7
%45 = and i32 %44, 1
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43
%48 = load i32, i32* %8
store i32 %48, i32* %4
br label %58
49: ; preds = %43, %39
%50 = load i8*, i8** %5
%51 = load i64, i64* @errno
%52 = call i32 @strerror(i64 %51)
%53 = call i32 @anetSetError(i8* %50, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %4
br label %58
56: ; preds = %34
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %49, %47, %30, %14
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str = [12 x i8] c"connect: %s\00"
define i32 @anetUnixGenericConnect(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = bitcast %struct.sockaddr_un* %4 to i8*
%6 = load i32, i32* @AF_LOCAL
%7 = tail call i32 @anetCreateSocket(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %33, label %9
9: ; preds = %3
%10 = load i32, i32* @AF_LOCAL
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @strncpy(i32 undef, i8* %1, i32 3)
%13 = and i32 %2, 1
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %9
%16 = tail call i64 @anetNonBlock(i8* %0, i32 %7)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @close(i32 %7)
br label %33
20: ; preds = %15, %9
%21 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%22 = call i32 @connect(i32 %7, %struct.sockaddr* %21, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINPROGRESS
%27 = icmp ne i64 %25, %26
%28 = or i1 %14, %27
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = call i32 @strerror(i64 %25)
%31 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %30)
%32 = call i32 @close(i32 %7)
br label %33
33: ; preds = %20, %24, %3, %29, %18
%34 = phi i32 [ -1, %18 ], [ -1, %29 ], [ -1, %3 ], [ %7, %24 ], [ %7, %20 ]
ret i32 %34
}