#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*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
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 const *);
int nw_connect_unix(const char *sckf) {
  struct sockaddr_un serv_addr;
  serv_addr.sun_family = AF_UNIX;
  strcpy(serv_addr.sun_path, sckf);
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    return -1;
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    close(sockfd);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<nw_connect_unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1196 <nw_connect_unix+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <nw_connect_unix+0x8b>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
cmp    $0x0,%rax
jge    11c5 <nw_connect_unix+0x85>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <nw_connect_unix+0x8b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nw_connect_unix>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e96(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rdi,%rsi
call   1030 <strcpy@plt>
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     119b <nw_connect_unix+0x5b>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
test   %rax,%rax
js     1194 <nw_connect_unix+0x54>
mov    %ebp,%ebx
jmp    119b <nw_connect_unix+0x5b>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%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
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @nw_connect_unix(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @AF_UNIX
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i8*, i8** %3
%11 = call i32 @strcpy(i32 %9, i8* %10)
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 -1, i32* %2
br label %28
18: ; preds = %1
%19 = load i32, i32* %5
%20 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%21 = call i64 @connect(i32 %19, %struct.sockaddr* %20, i32 8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
br label %28
26: ; preds = %18
%27 = load i32, i32* %5
store i32 %27, i32* %2
br label %28
28: ; preds = %26, %23, %17
%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
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @nw_connect_unix(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %4, i32* %5
%6 = tail call i32 @strcpy(i32 undef, i8* %0)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %1
%12 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%13 = call i64 @connect(i32 %9, %struct.sockaddr* %12, i32 8)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @close(i32 %9)
br label %17
17: ; preds = %11, %1, %15
%18 = phi i32 [ -1, %15 ], [ -1, %1 ], [ %9, %11 ]
ret i32 %18
}