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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ die(char *, void *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int opensocket(void) {
  int fd;
  struct sockaddr_un addr = {.sun_family = AF_UNIX, .sun_path = "@initctl"};
  if (addr.sun_path[0] == '@')
    addr.sun_path[0] = '\0';
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0)
    die("Can't create socket: ", ((void *)-1));
  if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)))
    die("Can't connect to "
        "@initctl"
        ": ",
        ((void *)-1));
  return fd;
}


x86 assembly (clang, -O0):
<opensocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x40,%eax
jne    1166 <opensocket+0x36>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    119f <opensocket+0x6f>
lea    0xe76(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
call   1030 <die@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1040 <connect@plt>
cmp    $0x0,%rax
je     11cd <opensocket+0x9d>
lea    0xe5e(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
call   1030 <die@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opensocket>:
push   %rbx
sub    $0x10,%rsp
lea    0xec4(%rip),%rax
mov    %rax,(%rsp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x8(%rsp)
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1176 <opensocket+0x46>
lea    0xe9f(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
call   1030 <die@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1040 <connect@plt>
test   %rax,%rax
je     119d <opensocket+0x6d>
lea    0xe8e(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
call   1030 <die@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"@initctl\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Can't create socket: \00"
@.str.2 = [28 x i8] c"Can't connect to @initctl: \00"
define i32 @opensocket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %3
%4 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
%5 = load i32, i32* @AF_UNIX
store i32 %5, i32* %4
%6 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%7 = load i8*, i8** %6
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 64
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 0, i8* %15
br label %16
16: ; preds = %12, %0
%17 = load i32, i32* @AF_UNIX
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %1
%20 = load i32, i32* %1
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
br label %24
24: ; preds = %22, %16
%25 = load i32, i32* %1
%26 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%27 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 16)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
br label %31
31: ; preds = %29, %24
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"@initctl\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Can't create socket: \00"
@.str.2 = [28 x i8] c"Can't connect to @initctl: \00"
define i32 @opensocket() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %3
%4 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
%5 = load i32, i32* @AF_UNIX
store i32 %5, i32* %4
%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 %9, label %11
9: ; preds = %0
%10 = tail call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
br label %11
11: ; preds = %9, %0
%12 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%13 = call i64 @connect(i32 %7, %struct.sockaddr* %12, i32 16)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
br label %17
17: ; preds = %15, %11
ret i32 %7
}