#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*/);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
scalar_t__ kr_net_nonblock(char *, int);
int /*<<< orphan*/ kr_net_set_error(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int anetUnixGenericConnect(char *err, 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 (kr_net_nonblock(err, s) != 0)
      return -1;
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    if (errno == EINPROGRESS && flags & 1)
      return s;
    kr_net_set_error(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   1050 <anetCreateSocket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11ad <anetUnixGenericConnect+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <anetUnixGenericConnect+0x121>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1200 <anetUnixGenericConnect+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <kr_net_nonblock@plt>
cmp    $0x0,%rax
je     11fb <anetUnixGenericConnect+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <anetUnixGenericConnect+0x121>
jmp    1200 <anetUnixGenericConnect+0x90>
mov    -0x20(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    128b <anetUnixGenericConnect+0x11b>
mov    0x2daf(%rip),%rax
mov    (%rax),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
jne    124e <anetUnixGenericConnect+0xde>
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     124e <anetUnixGenericConnect+0xde>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1291 <anetUnixGenericConnect+0x121>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xd8e(%rip),%rsi
call   1070 <kr_net_set_error@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <anetUnixGenericConnect+0x121>
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   1050 <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   1030 <strncpy@plt>
test   $0x1,%r15b
je     11cb <anetUnixGenericConnect+0x5b>
mov    %r12,%rdi
mov    %ebp,%esi
call   1060 <kr_net_nonblock@plt>
test   %rax,%rax
jne    121d <anetUnixGenericConnect+0xad>
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 <kr_net_set_error@plt>
mov    %ebp,%edi
call   1040 <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 %56
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 %32
25: ; preds = %15
%26 = load i8*, i8** %5
%27 = load i32, i32* %8
%28 = call i64 @kr_net_nonblock(i8* %26, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %25
store i32 -1, i32* %4
br label %56
31: ; preds = %25
br label %32
32: ; preds = %31, %15
%33 = load i32, i32* %8
%34 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%35 = call i32 @connect(i32 %33, %struct.sockaddr* %34, i32 8)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %54
37: ; preds = %32
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINPROGRESS
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %47
41: ; preds = %37
%42 = load i32, i32* %7
%43 = and i32 %42, 1
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = load i32, i32* %8
store i32 %46, i32* %4
br label %56
47: ; preds = %41, %37
%48 = load i8*, i8** %5
%49 = load i64, i64* @errno
%50 = call i32 @strerror(i64 %49)
%51 = call i32 @kr_net_set_error(i8* %48, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
store i32 -1, i32* %4
br label %56
54: ; preds = %32
%55 = load i32, i32* %8
store i32 %55, i32* %4
br label %56
56: ; preds = %54, %47, %45, %30, %14
%57 = load i32, i32* %4
ret i32 %57
}

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 %31, 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 %18, label %15
15: ; preds = %9
%16 = tail call i64 @kr_net_nonblock(i8* %0, i32 %7)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %15, %9
%19 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%20 = call i32 @connect(i32 %7, %struct.sockaddr* %19, i32 8)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINPROGRESS
%25 = icmp ne i64 %23, %24
%26 = or i1 %14, %25
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = call i32 @strerror(i64 %23)
%29 = call i32 @kr_net_set_error(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = call i32 @close(i32 %7)
br label %31
31: ; preds = %18, %22, %15, %3, %27
%32 = phi i32 [ -1, %27 ], [ -1, %3 ], [ -1, %15 ], [ %7, %22 ], [ %7, %18 ]
ret i32 %32
}