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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_SEQPACKET;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ipc_connect(const char *socket_path) {
  int err;
  int sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  if (sock < 0)
    return -1;
  struct sockaddr_un addr = {
      .sun_family = AF_UNIX,
  };
  snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
  err = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
  if (err != 0) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<ipc_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    117e <ipc_connect+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <ipc_connect+0xa1>
movl   $0x0,-0x20(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe5c(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1050 <connect@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11db <ipc_connect+0x9b>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <ipc_connect+0xa1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_connect>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e95(%rip),%r15
mov    (%r15),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11ad <ipc_connect+0x6d>
mov    %eax,%ebx
movl   $0x0,0x8(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
lea    0xe7e(%rip),%rdx
xor    %edi,%edi
mov    $0x4,%esi
mov    %r14,%rcx
call   1030 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1050 <connect@plt>
test   %eax,%eax
je     11b2 <ipc_connect+0x72>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @ipc_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_SEQPACKET
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %31
13: ; preds = %1
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
%16 = load i32, i32* @AF_UNIX
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = call i32 @snprintf(i32 %18, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* %5
%22 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%23 = call i32 @connect(i32 %21, %struct.sockaddr* %22, i32 8)
store i32 %23, i32* %4
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %13
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
store i32 -1, i32* %2
br label %31
29: ; preds = %13
%30 = load i32, i32* %5
store i32 %30, i32* %2
br label %31
31: ; preds = %29, %26, %12
%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
@SOCK_SEQPACKET = global i32 0
@.str = [3 x i8] c"%s\00"
define i32 @ipc_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_SEQPACKET
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %1
%8 = bitcast %struct.sockaddr_un* %2 to i8*
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
%11 = load i32, i32* @AF_UNIX
store i32 %11, i32* %10
%12 = tail call i32 @snprintf(i32 0, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%14 = call i32 @connect(i32 %5, %struct.sockaddr* %13, i32 8)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %7
%17 = call i32 @close(i32 %5)
br label %18
18: ; preds = %7, %16
%19 = phi i32 [ -1, %16 ], [ %5, %7 ]
br label %20
20: ; preds = %1, %18
%21 = phi i32 [ %19, %18 ], [ -1, %1 ]
ret i32 %21
}