#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 socklen_t;
typedef int /*<<< orphan*/ sockaddr;
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_PASSCRED;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
size_t strlen(char const *);
int serv_connect(const char *addr) {
  struct sockaddr_un sockaddr;
  size_t addr_strlen = strlen(addr);
  socklen_t sockaddr_len;
  sockaddr.sun_family = AF_UNIX;
  if (addr_strlen + 1 > sizeof(sockaddr.sun_path)) {
    errno = ENAMETOOLONG;
    return -1;
  }
  strcpy(sockaddr.sun_path, addr);
  sockaddr_len = sizeof(sockaddr) - sizeof(sockaddr.sun_path) + addr_strlen;
  int sockfd;
  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  if (connect(sockfd, (struct sockaddr *)&sockaddr, sockaddr_len) < 0)
    goto errout;
  int optval = 1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval)) < 0)
    goto errout;
  return sockfd;
errout:
  close(sockfd);
  return -1;
}


x86 assembly (clang, -O0):
<serv_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
cmp    $0x4,%rax
jbe    11b5 <serv_connect+0x55>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
mov    0x2e19(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <serv_connect+0x10f>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x24(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11fd <serv_connect+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <serv_connect+0x10f>
mov    -0x28(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    121b <serv_connect+0xbb>
jmp    1260 <serv_connect+0x100>
movl   $0x1,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0x0,%rax
jge    1255 <serv_connect+0xf5>
jmp    1260 <serv_connect+0x100>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <serv_connect+0x10f>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serv_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %rax,%rbp
mov    0x2e66(%rip),%r14
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    -0x4(%rbp),%rax
cmp    $0xfffffffffffffffa,%rax
ja     11a4 <serv_connect+0x44>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    0x2e25(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
jmp    1220 <serv_connect+0xc0>
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    (%r14),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     1220 <serv_connect+0xc0>
mov    %eax,%r14d
add    $0x4,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     1218 <serv_connect+0xb8>
movl   $0x1,0x4(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
js     1218 <serv_connect+0xb8>
mov    %r14d,%ebx
jmp    1220 <serv_connect+0xc0>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_PASSCRED = global i32 0
define i32 @serv_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %5
%11 = load i32, i32* @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %11, i32* %12
%13 = load i64, i64* %5
%14 = add i64 %13, 1
%15 = icmp ugt i64 %14, 4
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = load i32, i32* @ENAMETOOLONG
store i32 %17, i32* @errno
store i32 -1, i32* %2
br label %50
18: ; preds = %1
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i8*, i8** %3
%22 = call i32 @strcpy(i32 %20, i8* %21)
%23 = load i64, i64* %5
%24 = add i64 4, %23
%25 = trunc i64 %24 to i32
store i32 %25, i32* %6
%26 = load i32, i32* @AF_UNIX
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %18
store i32 -1, i32* %2
br label %50
31: ; preds = %18
%32 = load i32, i32* %7
%33 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%34 = load i32, i32* %6
%35 = call i64 @connect(i32 %32, %struct.sockaddr* %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
br label %47
38: ; preds = %31
store i32 1, i32* %8
%39 = load i32, i32* %7
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_PASSCRED
%42 = call i64 @setsockopt(i32 %39, i32 %40, i32 %41, i32* %8, i32 4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38
br label %47
45: ; preds = %38
%46 = load i32, i32* %7
store i32 %46, i32* %2
br label %50
47: ; preds = %44, %37
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %2
br label %50
50: ; preds = %47, %45, %30, %16
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_PASSCRED = global i32 0
define i32 @serv_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %6, i32* %7
%8 = add i64 %5, -4
%9 = icmp ult i64 %8, -5
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32, i32* @ENAMETOOLONG
store i32 %11, i32* @errno
br label %31
12: ; preds = %1
%13 = tail call i32 @strcpy(i32 undef, i8* %0)
%14 = load i32, i32* @AF_UNIX
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %31, label %18
18: ; preds = %12
%19 = trunc i64 %5 to i32
%20 = add nsw i32 %19, 4
%21 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%22 = call i64 @connect(i32 %16, %struct.sockaddr* %21, i32 %20)
%23 = icmp slt i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %18
store i32 1, i32* %3
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_PASSCRED
%27 = call i64 @setsockopt(i32 %16, i32 %25, i32 %26, i32* %3, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24, %18
%30 = call i32 @close(i32 %16)
br label %31
31: ; preds = %29, %12, %24, %10
%32 = phi i32 [ -1, %10 ], [ -1, %29 ], [ -1, %12 ], [ %16, %24 ]
ret i32 %32
}