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

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int connect_ipc(char *socket_path) {
  int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
  if (sockfd == -1)
    err(EXIT_FAILURE, "Could not create socket");
  struct sockaddr_un addr;
  memset(&addr, 0, sizeof(struct sockaddr_un));
  addr.sun_family = AF_LOCAL;
  strcpy(addr.sun_path, socket_path);
  if (connect(sockfd, (const struct sockaddr *)&addr,
              sizeof(struct sockaddr_un)) < 0)
    err(EXIT_FAILURE, "Could not connect to i3");
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_ipc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1197 <connect_ipc+0x47>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1050 <err@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    11ef <connect_ipc+0x9f>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1050 <err@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_ipc>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e86(%rip),%r12
mov    (%r12),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1192 <connect_ipc+0x42>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
call   1050 <err@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    11e0 <connect_ipc+0x90>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1050 <err@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@.str.1 = [24 x i8] c"Could not connect to i3\00"
define i32 @connect_ipc(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
store i8* %0, i8** %2
%5 = load i32, i32* @AF_LOCAL
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @err(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %10, %1
%14 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%15 = load i32, i32* @AF_LOCAL
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %2
%20 = call i32 @strcpy(i32 %18, i8* %19)
%21 = load i32, i32* %3
%22 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%23 = call i64 @connect(i32 %21, %struct.sockaddr* %22, i32 8)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @err(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %25, %13
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@EXIT_FAILURE = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@.str.1 = [24 x i8] c"Could not connect to i3\00"
define i32 @connect_ipc(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = load i32, i32* @AF_LOCAL
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @err(i32 %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %7, %1
%11 = bitcast %struct.sockaddr_un* %2 to i8*
%12 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%13 = load i32, i32* @AF_LOCAL
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* %0)
%18 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%19 = call i64 @connect(i32 %5, %struct.sockaddr* %18, i32 8)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %10
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @err(i32 %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %10
ret i32 %5
}