#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_UNIX;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int un__socket();
int un_connect(const char *path) {
  int fd;
  struct sockaddr_un sa;
  int rv;
  fd = un__socket();
  sa.sun_family = AF_UNIX;
  assert(strlen(path) < sizeof(sa.sun_path));
  strcpy(sa.sun_path, path);
  rv = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
  if (rv == -1) {
    close(fd);
    return rv;
  }
  return fd;
}


x86 assembly (clang, -O0):
<un_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <un__socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
cmp    $0x4,%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11db <un_connect+0x7b>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e1 <un_connect+0x81>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<un_connect>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
xor    %eax,%eax
call   1080 <un__socket@plt>
mov    %eax,%ebp
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
cmp    $0x4,%eax
setb   %dil
call   1060 <assert@plt>
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    11ba <un_connect+0x5a>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
define i32 @un_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i32 (...) @un__socket()
store i32 %7, i32* %4
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %8, i32* %9
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = sext i32 %11 to i64
%13 = icmp ult i64 %12, 4
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = call i32 @strcpy(i32 %17, i8* %18)
%20 = load i32, i32* %4
%21 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%22 = call i32 @connect(i32 %20, %struct.sockaddr* %21, i32 8)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %1
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %6
store i32 %28, i32* %2
br label %31
29: ; preds = %1
%30 = load i32, i32* %4
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
define i32 @un_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 (...) @un__socket()
%5 = load i32, i32* @AF_UNIX
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %5, i32* %6
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp ult i32 %7, 4
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = tail call i32 @strcpy(i32 undef, i8* %0)
%12 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%13 = call i32 @connect(i32 %4, %struct.sockaddr* %12, i32 8)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @close(i32 %4)
br label %17
17: ; preds = %1, %15
%18 = phi i32 [ -1, %15 ], [ %4, %1 ]
ret i32 %18
}