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

/* Variables and functions */
void *AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int nscd_open_socket(void) {
  struct sockaddr_un addr;
  int sock;
  sock = socket(PF_UNIX, SOCK_STREAM, 0);
  if (sock < 0)
    return -1;
  addr.sun_family = AF_UNIX;
  assert(sizeof(addr.sun_path) >= sizeof("/var/run/nscd/socket"));
  strcpy(addr.sun_path, "/var/run/nscd/socket");
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    addr.sun_family = AF_UNIX;
    assert(sizeof(addr.sun_path) >= sizeof("/var/run/.nscd_socket"));
    strcpy(addr.sun_path, "/var/run/.nscd_socket");
    if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
      close(sock);
      return -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<nscd_open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118a <nscd_open_socket+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <nscd_open_socket+0xd7>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
xor    %edi,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%edi
lea    0xe57(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    1221 <nscd_open_socket+0xd1>
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
xor    %edi,%edi
call   1050 <assert@plt>
mov    -0x18(%rbp),%edi
lea    0xe2d(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    121c <nscd_open_socket+0xcc>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1227 <nscd_open_socket+0xd7>
jmp    1221 <nscd_open_socket+0xd1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nscd_open_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     11e6 <nscd_open_socket+0x96>
mov    %eax,%ebp
mov    0x2e5d(%rip),%r14
mov    (%r14),%rax
mov    %rax,0x8(%rsp)
xor    %edi,%edi
call   1050 <assert@plt>
lea    0xe67(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11e4 <nscd_open_socket+0x94>
mov    (%r14),%rax
mov    %rax,0x8(%rsp)
xor    %edi,%edi
call   1050 <assert@plt>
mov    (%rsp),%edi
lea    0xe4a(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1060 <connect@plt>
test   %rax,%rax
js     11f1 <nscd_open_socket+0xa1>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11e6 <nscd_open_socket+0x96>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i8* null
@.str = [21 x i8] c"/var/run/nscd/socket\00"
@.str.1 = [22 x i8] c"/var/run/.nscd_socket\00"
define i32 @nscd_open_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = load i32, i32* @PF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = call i32 @socket(i32 %4, i32 %5, i32 0)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %38
10: ; preds = %0
%11 = load i8*, i8** @AF_UNIX
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i8* %11, i8** %12
%13 = call i32 @assert(i32 0)
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strcpy(i32 %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %3
%18 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%19 = call i64 @connect(i32 %17, %struct.sockaddr* %18, i32 16)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %36
21: ; preds = %10
%22 = load i8*, i8** @AF_UNIX
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i8* %22, i8** %23
%24 = call i32 @assert(i32 0)
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @strcpy(i32 %26, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %3
%29 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 16)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %21
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %1
br label %38
35: ; preds = %21
br label %36
36: ; preds = %35, %10
%37 = load i32, i32* %3
store i32 %37, i32* %1
br label %38
38: ; preds = %36, %32, %9
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i8* }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i8* null
@.str = [21 x i8] c"/var/run/nscd/socket\00"
@.str.1 = [22 x i8] c"/var/run/.nscd_socket\00"
define i32 @nscd_open_socket() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = load i32, i32* @PF_UNIX
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %25, label %7
7: ; preds = %0
%8 = load i8*, i8** @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i8* %8, i8** %9
%10 = tail call i32 @assert(i32 0)
%11 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%12 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%13 = call i64 @connect(i32 %5, %struct.sockaddr* %12, i32 16)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %7
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%17 = load i8*, i8** @AF_UNIX
store i8* %17, i8** %9
%18 = call i32 @assert(i32 0)
%19 = load i32, i32* %16
%20 = call i32 @strcpy(i32 %19, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%21 = call i64 @connect(i32 %5, %struct.sockaddr* %12, i32 16)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = call i32 @close(i32 %5)
br label %25
25: ; preds = %7, %15, %0, %23
%26 = phi i32 [ -1, %23 ], [ -1, %0 ], [ %5, %15 ], [ %5, %7 ]
ret i32 %26
}