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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int vhost_user_connect(const char *path) {
  int sock;
  struct sockaddr_un un;
  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    return -1;
  }
  un.sun_family = AF_UNIX;
  strncpy(un.sun_path, path, sizeof(un.sun_path) - 1);
  if (connect(sock, (struct sockaddr *)&un, sizeof(un)) == -1) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<vhost_user_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1199 <vhost_user_connect+0x49>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <vhost_user_connect+0x9a>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11e4 <vhost_user_connect+0x94>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ea <vhost_user_connect+0x9a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vhost_user_connect>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e85(%rip),%r15
mov    (%r15),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11bf <vhost_user_connect+0x6f>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11b3 <vhost_user_connect+0x63>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
jmp    11ae <vhost_user_connect+0x5e>

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

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
define i32 @vhost_user_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
10: ; preds = %1
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %11, i32* %12
%13 = tail call i32 @strncpy(i32 undef, i8* %0, i32 3)
%14 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%15 = call i32 @connect(i32 %6, %struct.sockaddr* %14, i32 8)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @close(i32 %6)
br label %19
19: ; preds = %10, %17, %8
%20 = phi i32 [ -1, %8 ], [ -1, %17 ], [ %6, %10 ]
ret i32 %20
}