#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ err(scalar_t__, char *, char *);
scalar_t__ errno;
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int open_initctl(char *init_fifo) {
  struct stat st, st2;
  if (stat(init_fifo, &st2) < 0 && errno == ENOENT)
    mkfifo(init_fifo, 0600);
  int pipe_fd = open(init_fifo, O_RDWR);
  if (pipe_fd < 0)
    err(errno, "error opening %s", init_fifo);
  fstat(pipe_fd, &st);
  if (!S_ISFIFO(st.st_mode)) {
    errno = EINVAL;
    err(errno, "%s is not a fifo", init_fifo);
  }
  return pipe_fd;
}


x86 assembly (clang, -O0):
<open_initctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    11ab <open_initctl+0x4b>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
jne    11ab <open_initctl+0x4b>
mov    -0x8(%rbp),%rdi
mov    $0x180,%esi
call   1040 <mkfifo@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11e4 <open_initctl+0x84>
mov    0x2e07(%rip),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1060 <err@plt>
mov    -0x1c(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x10(%rbp),%edi
call   1030 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    122f <open_initctl+0xcf>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rcx
mov    0x2dc6(%rip),%rax
mov    %rcx,(%rax)
mov    0x2dbc(%rip),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rdx
lea    0xde7(%rip),%rsi
call   1060 <err@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_initctl>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jns    119b <open_initctl+0x3b>
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    119b <open_initctl+0x3b>
mov    %rbx,%rdi
mov    $0x180,%esi
call   1040 <mkfifo@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11cb <open_initctl+0x6b>
mov    0x2e1f(%rip),%rax
mov    (%rax),%rdi
lea    0xe3d(%rip),%rsi
mov    %rbx,%rdx
call   1060 <err@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
mov    0x8(%rsp),%edi
call   1030 <S_ISFIFO@plt>
test   %eax,%eax
jne    1207 <open_initctl+0xa7>
mov    0x2de5(%rip),%rax
mov    (%rax),%rdi
mov    0x2de3(%rip),%rax
mov    %rdi,(%rax)
lea    0xe12(%rip),%rsi
mov    %rbx,%rdx
call   1060 <err@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@O_RDWR = global i32 0
@.str = [17 x i8] c"error opening %s\00"
@EINVAL = global i64 0
@.str.1 = [17 x i8] c"%s is not a fifo\00"
define i32 @open_initctl(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @stat(i8* %6, %struct.stat* %4)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i8*, i8** %2
%15 = call i32 @mkfifo(i8* %14, i32 384)
br label %16
16: ; preds = %13, %9, %1
%17 = load i8*, i8** %2
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i8*, i8** %2
%25 = call i32 @err(i64 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %22, %16
%27 = load i32, i32* %5
%28 = call i32 @fstat(i32 %27, %struct.stat* %3)
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @S_ISFIFO(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %26
%34 = load i64, i64* @EINVAL
store i64 %34, i64* @errno
%35 = load i64, i64* @errno
%36 = load i8*, i8** %2
%37 = call i32 @err(i64 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %33, %26
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@O_RDWR = global i32 0
@.str = [17 x i8] c"error opening %s\00"
@EINVAL = global i64 0
@.str.1 = [17 x i8] c"%s is not a fifo\00"
define i32 @open_initctl(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %2 to i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i64 @stat(i8* %0, %struct.stat* %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @mkfifo(i8* %0, i32 384)
br label %14
14: ; preds = %12, %8, %1
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %0, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = call i32 @err(i64 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %18, %14
%22 = call i32 @fstat(i32 %16, %struct.stat* %2)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = call i32 @S_ISFIFO(i32 %24)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i64, i64* @EINVAL
store i64 %28, i64* @errno
%29 = call i32 @err(i64 %28, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %27, %21
ret i32 %16
}