#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_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct sockaddr_un *malloc(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int ipc_connect(char *name) {
  int s, r;
  struct sockaddr_un *socknaam;
  socknaam = malloc(sizeof(*socknaam));
  if (!socknaam)
    return -2;
  s = socket(PF_UNIX, SOCK_STREAM, 0);
  if (s < 0) {
    return s;
  }
  socknaam->sun_family = AF_UNIX;
  strncpy(socknaam->sun_path, name, 108);
  r = connect(s, (struct sockaddr *)socknaam, sizeof(*socknaam));
  if (r) {
    close(s);
    return -2;
  }
  return s;
}


x86 assembly (clang, -O0):
<ipc_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x8,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1181 <ipc_connect+0x31>
movl   $0xfffffffe,-0x4(%rbp)
jmp    120e <ipc_connect+0xbe>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11b2 <ipc_connect+0x62>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <ipc_connect+0xbe>
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    $0x6c,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1208 <ipc_connect+0xb8>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    120e <ipc_connect+0xbe>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x8,%edi
call   1050 <malloc@plt>
mov    $0xfffffffe,%r15d
test   %rax,%rax
je     11cb <ipc_connect+0x7b>
mov    %rax,%rbx
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11c8 <ipc_connect+0x78>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    (%rbx),%edi
mov    %r14,%rsi
mov    $0x6c,%edx
call   1030 <strncpy@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
test   %eax,%eax
je     11c8 <ipc_connect+0x78>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11cb <ipc_connect+0x7b>
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
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 = call %struct.sockaddr_un* @malloc(i32 8)
store %struct.sockaddr_un* %7, %struct.sockaddr_un** %6
%8 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%9 = icmp ne %struct.sockaddr_un* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 -2, i32* %2
br label %39
11: ; preds = %1
%12 = load i32, i32* @PF_UNIX
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = load i32, i32* %4
store i32 %18, i32* %2
br label %39
19: ; preds = %11
%20 = load i32, i32* @AF_UNIX
%21 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %21, i32 0, i32 1
store i32 %20, i32* %22
%23 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i8*, i8** %3
%27 = call i32 @strncpy(i32 %25, i8* %26, i32 108)
%28 = load i32, i32* %4
%29 = load %struct.sockaddr_un*, %struct.sockaddr_un** %6
%30 = bitcast %struct.sockaddr_un* %29 to %struct.sockaddr*
%31 = call i32 @connect(i32 %28, %struct.sockaddr* %30, i32 8)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %19
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
store i32 -2, i32* %2
br label %39
37: ; preds = %19
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %39
39: ; preds = %37, %34, %17, %10
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
define i32 @ipc_connect(i8* %0) {
%2 = tail call %struct.sockaddr_un* @malloc(i32 8)
%3 = icmp eq %struct.sockaddr_un* %2, null
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %4
%10 = load i32, i32* @AF_UNIX
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i32 @strncpy(i32 %13, i8* %0, i32 108)
%15 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%16 = tail call i32 @connect(i32 %7, %struct.sockaddr* %15, i32 8)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %9
%19 = tail call i32 @close(i32 %7)
br label %20
20: ; preds = %9, %4, %1, %18
%21 = phi i32 [ -2, %18 ], [ -2, %1 ], [ %7, %4 ], [ %7, %9 ]
ret i32 %21
}