#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*/ sock_addr;
typedef int /*<<< orphan*/ dst_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ connect_nb(int, struct sockaddr *, int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int _unix_connect(int sock, const char *sockfile, int blocking) {
  struct sockaddr_un sock_addr;
  struct sockaddr_un dst_addr;
  memset(&sock_addr, 0x00, sizeof(sock_addr));
  memset(&dst_addr, 0x00, sizeof(dst_addr));
  dst_addr.sun_family = AF_LOCAL;
  snprintf(dst_addr.sun_path, sizeof(dst_addr.sun_path), "%s", sockfile);
  if (blocking) {
    if (connect(sock, (struct sockaddr *)&dst_addr, sizeof(dst_addr)) < 0) {
      close(sock);
      return -1;
    }
  } else {
    if (connect_nb(sock, (struct sockaddr *)&dst_addr, sizeof(dst_addr), 1) <=
        0) {
      close(sock);
      return -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<_unix_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe5f(%rip),%rdx
call   1030 <snprintf@plt>
cmpl   $0x0,-0x14(%rbp)
je     11e4 <_unix_connect+0x94>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    11df <_unix_connect+0x8f>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <_unix_connect+0xd3>
jmp    121d <_unix_connect+0xcd>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
mov    $0x1,%ecx
call   1060 <connect_nb@plt>
cmp    $0x0,%rax
jg     1218 <_unix_connect+0xc8>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <_unix_connect+0xd3>
jmp    121d <_unix_connect+0xcd>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_unix_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
lea    0xe69(%rip),%rdx
mov    $0x4,%esi
mov    %rbp,%rcx
call   1030 <snprintf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
test   %r14d,%r14d
je     11d4 <_unix_connect+0x84>
call   1070 <connect@plt>
test   %rax,%rax
jns    11c9 <_unix_connect+0x79>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x1,%ecx
call   1060 <connect_nb@plt>
test   %rax,%rax
jg     11c9 <_unix_connect+0x79>
jmp    11bd <_unix_connect+0x6d>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @_unix_connect(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
%9 = alloca %struct.sockaddr_un
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 8)
%11 = call i32 @memset(%struct.sockaddr_un* %9, i32 0, i32 8)
%12 = load i32, i32* @AF_LOCAL
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %6
%17 = call i32 @snprintf(i32 %15, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %3
%21 = load i32, i32* %5
%22 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%23 = call i64 @connect(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %4
br label %40
28: ; preds = %20
br label %38
29: ; preds = %3
%30 = load i32, i32* %5
%31 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%32 = call i64 @connect_nb(i32 %30, %struct.sockaddr* %31, i32 8, i32 1)
%33 = icmp sle i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
store i32 -1, i32* %4
br label %40
37: ; preds = %29
br label %38
38: ; preds = %37, %28
%39 = load i32, i32* %5
store i32 %39, i32* %4
br label %40
40: ; preds = %38, %34, %25
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @_unix_connect(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_un
%5 = alloca %struct.sockaddr_un
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = bitcast %struct.sockaddr_un* %5 to i8*
%8 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%9 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%10 = load i32, i32* @AF_LOCAL
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @snprintf(i32 %13, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%15 = icmp eq i32 %2, 0
%16 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
br i1 %15, label %20, label %17
17: ; preds = %3
%18 = call i64 @connect(i32 %0, %struct.sockaddr* %16, i32 8)
%19 = icmp slt i64 %18, 0
br i1 %19, label %23, label %25
20: ; preds = %3
%21 = call i64 @connect_nb(i32 %0, %struct.sockaddr* %16, i32 8, i32 1)
%22 = icmp slt i64 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %20, %17
%24 = call i32 @close(i32 %0)
br label %25
25: ; preds = %23, %17, %20
%26 = phi i32 [ %0, %20 ], [ %0, %17 ], [ -1, %23 ]
ret i32 %26
}