#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ LOG_CRIT;
int SUN_LEN(struct sockaddr_un *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ elog(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                        char const *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ strncpy(char *, char const *, int);
int connect_sock(int sk, const char *uri) {
  int namelen, ret;
  union {
    struct sockaddr sa;
    struct sockaddr_un un;
  } name = {};
  name.un.sun_family = AF_UNIX;
  strncpy(name.un.sun_path, uri, sizeof(name.un.sun_path));
  namelen = SUN_LEN(&name.un);
  if (name.un.sun_path[0] == '@')
    name.un.sun_path[0] = 0;
  ret = connect(sk, &name.sa, namelen);
  if (ret < 0)
    elog(LOG_CRIT, errno, "connect %s", uri);
  return ret;
}


x86 assembly (clang, -O0):
<connect_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xeaa(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xea7(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
lea    -0x28(%rbp),%rdi
call   1060 <SUN_LEN@plt>
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x40,%eax
jne    11a6 <connect_sock+0x66>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11e4 <connect_sock+0xa4>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
lea    0xe31(%rip),%rdx
call   1040 <elog@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_sock>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
xor    %edi,%edi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rdi
call   1060 <SUN_LEN@plt>
mov    (%rsp),%rcx
cmpb   $0x40,(%rcx)
jne    117f <connect_sock+0x3f>
movb   $0x0,(%rcx)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1050 <connect@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11b2 <connect_sock+0x72>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdx
mov    %rbx,%rcx
call   1040 <elog@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%union.anon = type { %struct.sockaddr_un }
%struct.sockaddr_un = type { i8*, i32 }
@__const.connect_sock.name = { %struct.sockaddr, [12 x i8] } { %struct.sockaddr zeroinitializer, [12 x i8] undef }
@AF_UNIX = global i32 0
@LOG_CRIT = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"connect %s\00"
define i32 @connect_sock(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %union.anon
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = bitcast %union.anon* %7 to i8*
%9 = load i32, i32* @AF_UNIX
%10 = bitcast %union.anon* %7 to %struct.sockaddr_un*
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %10, i32 0, i32 1
store i32 %9, i32* %11
%12 = bitcast %union.anon* %7 to %struct.sockaddr_un*
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%14 = load i8*, i8** %13
%15 = load i8*, i8** %4
%16 = call i32 @strncpy(i8* %14, i8* %15, i32 8)
%17 = bitcast %union.anon* %7 to %struct.sockaddr_un*
%18 = call i32 @SUN_LEN(%struct.sockaddr_un* %17)
store i32 %18, i32* %5
%19 = bitcast %union.anon* %7 to %struct.sockaddr_un*
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %19, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 64
br i1 %25, label %26, label %31
26: ; preds = %2
%27 = bitcast %union.anon* %7 to %struct.sockaddr_un*
%28 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 0, i8* %30
br label %31
31: ; preds = %26, %2
%32 = load i32, i32* %3
%33 = bitcast %union.anon* %7 to %struct.sockaddr*
%34 = load i32, i32* %5
%35 = call i32 @connect(i32 %32, %struct.sockaddr* %33, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %31
%39 = load i32, i32* @LOG_CRIT
%40 = load i32, i32* @errno
%41 = load i8*, i8** %4
%42 = call i32 @elog(i32 %39, i32 %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %38, %31
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
%union.anon = type { %struct.sockaddr_un }
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@LOG_CRIT = global i32 0
@errno = global i32 0
@.str = [11 x i8] c"connect %s\00"
define i32 @connect_sock(i32 %0, i8* %1) {
%3 = alloca %union.anon
%4 = bitcast %union.anon* %3 to i8*
%5 = load i32, i32* @AF_UNIX
%6 = getelementptr inbounds %union.anon, %union.anon* %3, i64 0, i32 0
%7 = getelementptr inbounds %union.anon, %union.anon* %3, i64 0, i32 0, i32 1
store i32 %5, i32* %7
%8 = getelementptr inbounds %union.anon, %union.anon* %3, i64 0, i32 0, i32 0
%9 = tail call i32 @strncpy(i8* null, i8* %1, i32 8)
%10 = call i32 @SUN_LEN(%struct.sockaddr_un* %6)
%11 = load i8*, i8** %8
%12 = load i8, i8* %11
%13 = icmp eq i8 %12, 64
br i1 %13, label %14, label %15
14: ; preds = %2
store i8 0, i8* %11
br label %15
15: ; preds = %14, %2
%16 = bitcast %union.anon* %3 to %struct.sockaddr*
%17 = call i32 @connect(i32 %0, %struct.sockaddr* %16, i32 %10)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i32, i32* @LOG_CRIT
%21 = load i32, i32* @errno
%22 = call i32 @elog(i32 %20, i32 %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %23
23: ; preds = %19, %15
ret i32 %17
}