#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ unlink(char *);
int ipc_init(int as_master) {
  int sock;
  int flags;
  struct sockaddr_un so;
  sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    return -1;
  }
  memset(&so, 0, sizeof(struct sockaddr_un));
  so.sun_family = AF_UNIX;
  strncpy(so.sun_path, "/run/ldm.socket", sizeof(so.sun_path));
  if (as_master) {
    unlink("/run/ldm.socket");
    if (bind(sock, (struct sockaddr *)&so, sizeof(struct sockaddr_un)) < 0) {
      perror("bind");
      return -1;
    }
    flags = fcntl(sock, F_GETFL, 0);
    if (flags < 0) {
      perror("fcntl");
      return -1;
    }
    if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
      perror("fcntl");
      return -1;
    }
  } else {
    if (connect(sock, (struct sockaddr *)&so, sizeof(struct sockaddr_un)) < 0) {
      perror("connect");
      return -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<ipc_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c9 <ipc_init+0x49>
lea    0xe51(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fb <ipc_init+0x17b>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    0xe11(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
cmpl   $0x0,-0x8(%rbp)
je     12bd <ipc_init+0x13d>
lea    0xdf6(%rip),%rdi
call   1030 <unlink@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    1242 <ipc_init+0xc2>
lea    0xddf(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fb <ipc_init+0x17b>
mov    -0xc(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    127a <ipc_init+0xfa>
lea    0xdac(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fb <ipc_init+0x17b>
mov    -0xc(%rbp),%edi
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    0x2d58(%rip),%rax
or     (%rax),%edx
call   1050 <fcntl@plt>
cmp    $0x0,%eax
jge    12b8 <ipc_init+0x138>
lea    0xd6e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fb <ipc_init+0x17b>
jmp    12f5 <ipc_init+0x175>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    12f0 <ipc_init+0x170>
lea    0xd3c(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fb <ipc_init+0x17b>
jmp    12f5 <ipc_init+0x175>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_init>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e4f(%rip),%r14
mov    (%r14),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     125e <ipc_init+0xde>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe2f(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncpy@plt>
test   %ebp,%ebp
je     123f <ipc_init+0xbf>
lea    0xe1a(%rip),%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1267 <ipc_init+0xe7>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
js     1236 <ipc_init+0xb6>
mov    0x2db2(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dc1(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <fcntl@plt>
test   %eax,%eax
jns    1278 <ipc_init+0xf8>
lea    0xdd8(%rip),%rdi
jmp    126e <ipc_init+0xee>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1090 <connect@plt>
test   %rax,%rax
jns    1278 <ipc_init+0xf8>
lea    0xdbf(%rip),%rdi
jmp    126e <ipc_init+0xee>
lea    0xda4(%rip),%rdi
jmp    126e <ipc_init+0xee>
lea    0xda2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [16 x i8] c"/run/ldm.socket\00"
@.str.2 = [5 x i8] c"bind\00"
@F_GETFL = global i32 0
@.str.3 = [6 x i8] c"fcntl\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [8 x i8] c"connect\00"
define i32 @ipc_init(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
store i32 %0, i32* %3
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strncpy(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 4)
%21 = load i32, i32* %3
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %50
23: ; preds = %14
%24 = call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%27 = call i64 @bind(i32 %25, %struct.sockaddr* %26, i32 8)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
31: ; preds = %23
%32 = load i32, i32* %4
%33 = load i32, i32* @F_GETFL
%34 = call i32 @fcntl(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
39: ; preds = %31
%40 = load i32, i32* %4
%41 = load i32, i32* @F_SETFL
%42 = load i32, i32* %5
%43 = load i32, i32* @O_NONBLOCK
%44 = or i32 %42, %43
%45 = call i32 @fcntl(i32 %40, i32 %41, i32 %44)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
49: ; preds = %39
br label %58
50: ; preds = %14
%51 = load i32, i32* %4
%52 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%53 = call i64 @connect(i32 %51, %struct.sockaddr* %52, i32 8)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %60
57: ; preds = %50
br label %58
58: ; preds = %57, %49
%59 = load i32, i32* %4
store i32 %59, i32* %2
br label %60
60: ; preds = %58, %55, %47, %37, %29, %12
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [16 x i8] c"/run/ldm.socket\00"
@.str.2 = [5 x i8] c"bind\00"
@F_GETFL = global i32 0
@.str.3 = [6 x i8] c"fcntl\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.4 = [8 x i8] c"connect\00"
define i32 @ipc_init(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %45
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @strncpy(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 4)
%17 = icmp eq i32 %0, 0
br i1 %17, label %39, label %18
18: ; preds = %10
%19 = call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%20 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%21 = call i64 @bind(i32 %6, %struct.sockaddr* %20, i32 8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %45
25: ; preds = %18
%26 = load i32, i32* @F_GETFL
%27 = call i32 @fcntl(i32 %6, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %45
31: ; preds = %25
%32 = load i32, i32* @F_SETFL
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %33, %27
%35 = call i32 @fcntl(i32 %6, i32 %32, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %31
%38 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %45
39: ; preds = %10
%40 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%41 = call i64 @connect(i32 %6, %struct.sockaddr* %40, i32 8)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %31, %39, %43, %37, %29, %23, %8
%46 = phi i32 [ -1, %8 ], [ -1, %23 ], [ -1, %29 ], [ -1, %37 ], [ -1, %43 ], [ %6, %39 ], [ %6, %31 ]
ret i32 %46
}