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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, size_t);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int __mpath_connect(int nonblocking) {
  int fd;
  size_t len;
  struct sockaddr_un addr;
  int flags = 0;
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_LOCAL;
  addr.sun_path[0] = '\0';
  strncpy(&addr.sun_path[1], "/org/kernel/linux/storage/multipathd",
          sizeof(addr.sun_path) - 1);
  len =
      strlen("/org/kernel/linux/storage/multipathd") + 1 + sizeof(sa_family_t);
  if (len > sizeof(struct sockaddr_un))
    len = sizeof(struct sockaddr_un);
  fd = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (fd == -1)
    return -1;
  if (nonblocking) {
    flags = fcntl(fd, F_GETFL, 0);
    if (flags != -1)
      (void)fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  }
  if (connect(fd, (struct sockaddr *)&addr, len) == -1) {
    int err = errno;
    close(fd);
    errno = err;
    return -1;
  }
  if (nonblocking && flags != -1)
    (void)fcntl(fd, F_SETFL, flags);
  return fd;
}


x86 assembly (clang, -O0):
<__mpath_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
lea    0xe4c(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
lea    0xe3b(%rip),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x10,-0x18(%rbp)
jbe    11ea <__mpath_connect+0x7a>
movq   $0x10,-0x18(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    121c <__mpath_connect+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <__mpath_connect+0x16b>
cmpl   $0x0,-0x8(%rbp)
je     1268 <__mpath_connect+0xf8>
mov    -0xc(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     1263 <__mpath_connect+0xf3>
mov    -0xc(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2d8c(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
jmp    1268 <__mpath_connect+0xf8>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ad <__mpath_connect+0x13d>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x30(%rbp),%ecx
mov    0x2d21(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <__mpath_connect+0x16b>
cmpl   $0x0,-0x8(%rbp)
je     12d5 <__mpath_connect+0x165>
cmpl   $0xffffffff,-0x2c(%rbp)
je     12d5 <__mpath_connect+0x165>
mov    -0xc(%rbp),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
call   1040 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__mpath_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
mov    %rsp,%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e48(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rax
movb   $0x0,(%rax)
mov    (%rsp),%rdi
add    $0x1,%rdi
lea    0xe4a(%rip),%rbx
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
add    $0x5,%rax
cmp    $0x10,%rax
mov    $0x10,%ebp
cmovb  %rax,%rbp
mov    (%r12),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     128f <__mpath_connect+0x11f>
mov    %eax,%ebx
test   %r14d,%r14d
je     1246 <__mpath_connect+0xd6>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1240 <__mpath_connect+0xd0>
mov    %eax,%r15d
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
or     %r15d,%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
jmp    1246 <__mpath_connect+0xd6>
mov    $0xffffffff,%r15d
mov    %rsp,%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     127b <__mpath_connect+0x10b>
test   %r14d,%r14d
je     1276 <__mpath_connect+0x106>
cmp    $0xffffffff,%r15d
je     1276 <__mpath_connect+0x106>
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15d,%edx
call   1040 <fcntl@plt>
mov    %ebx,%r12d
jmp    128f <__mpath_connect+0x11f>
mov    0x2d3e(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,(%r14)
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@.str = [37 x i8] c"/org/kernel/linux/storage/multipathd\00"
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
define i32 @__mpath_connect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %7
%9 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 16)
%10 = load i32, i32* @AF_LOCAL
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8, i8* %13, i64 0
store i8 0, i8* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = getelementptr inbounds i8, i8* %16, i64 1
%18 = call i32 @strncpy(i8* %17, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 7)
%19 = call i32 @strlen(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = add i64 %21, 4
store i64 %22, i64* %5
%23 = load i64, i64* %5
%24 = icmp ugt i64 %23, 16
br i1 %24, label %25, label %26
25: ; preds = %1
store i64 16, i64* %5
br label %26
26: ; preds = %25, %1
%27 = load i32, i32* @AF_LOCAL
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 -1, i32* %2
br label %74
33: ; preds = %26
%34 = load i32, i32* %3
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %33
%37 = load i32, i32* %4
%38 = load i32, i32* @F_GETFL
%39 = call i32 @fcntl(i32 %37, i32 %38, i32 0)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp ne i32 %40, -1
br i1 %41, label %42, label %49
42: ; preds = %36
%43 = load i32, i32* %4
%44 = load i32, i32* @F_SETFL
%45 = load i32, i32* %7
%46 = load i32, i32* @O_NONBLOCK
%47 = or i32 %45, %46
%48 = call i32 @fcntl(i32 %43, i32 %44, i32 %47)
br label %49
49: ; preds = %42, %36
br label %50
50: ; preds = %49, %33
%51 = load i32, i32* %4
%52 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%53 = load i64, i64* %5
%54 = call i32 @connect(i32 %51, %struct.sockaddr* %52, i64 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %61
56: ; preds = %50
%57 = load i32, i32* @errno
store i32 %57, i32* %8
%58 = load i32, i32* %4
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %8
store i32 %60, i32* @errno
store i32 -1, i32* %2
br label %74
61: ; preds = %50
%62 = load i32, i32* %3
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %72
64: ; preds = %61
%65 = load i32, i32* %7
%66 = icmp ne i32 %65, -1
br i1 %66, label %67, label %72
67: ; preds = %64
%68 = load i32, i32* %4
%69 = load i32, i32* @F_SETFL
%70 = load i32, i32* %7
%71 = call i32 @fcntl(i32 %68, i32 %69, i32 %70)
br label %72
72: ; preds = %67, %64, %61
%73 = load i32, i32* %4
store i32 %73, i32* %2
br label %74
74: ; preds = %72, %56, %32
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@.str = [37 x i8] c"/org/kernel/linux/storage/multipathd\00"
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
define i32 @__mpath_connect(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%5 = load i32, i32* @AF_LOCAL
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %5, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%8 = load i8*, i8** %7
store i8 0, i8* %8
%9 = load i8*, i8** %7
%10 = getelementptr inbounds i8, i8* %9, i64 1
%11 = call i32 @strncpy(i8* %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 7)
%12 = call i32 @strlen(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%13 = sext i32 %12 to i64
%14 = add nsw i64 %13, 5
%15 = icmp ult i64 %14, 16
%16 = select i1 %15, i64 %14, i64 16
%17 = load i32, i32* @AF_LOCAL
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %46, label %21
21: ; preds = %1
%22 = icmp ne i32 %0, 0
br i1 %22, label %23, label %32
23: ; preds = %21
%24 = load i32, i32* @F_GETFL
%25 = call i32 @fcntl(i32 %19, i32 %24, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %29, %25
%31 = call i32 @fcntl(i32 %19, i32 %28, i32 %30)
br label %32
32: ; preds = %23, %27, %21
%33 = phi i32 [ %25, %27 ], [ -1, %23 ], [ 0, %21 ]
%34 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%35 = call i32 @connect(i32 %19, %struct.sockaddr* %34, i64 %16)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* @errno
%39 = call i32 @close(i32 %19)
store i32 %38, i32* @errno
br label %46
40: ; preds = %32
%41 = icmp ne i32 %33, -1
%42 = select i1 %22, i1 %41, i1 false
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* @F_SETFL
%45 = call i32 @fcntl(i32 %19, i32 %44, i32 %33)
br label %46
46: ; preds = %40, %43, %1, %37
%47 = phi i32 [ -1, %37 ], [ -1, %1 ], [ %19, %43 ], [ %19, %40 ]
ret i32 %47
}