#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 */
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ mkfifo(char *, int);
int open(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ unlink(char *);
int reopen_fifo(int fd) {
  if (fd >= 0)
    close(fd);
  struct stat st;
  if (stat("/dev/initctl", &st) < 0 || !S_ISFIFO(st.st_mode)) {
    unlink("/dev/initctl");
    mkfifo("/dev/initctl", 0660);
  }
  return open("/dev/initctl", O_RDONLY | O_CLOEXEC);
}


x86 assembly (clang, -O0):
<reopen_fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     117d <reopen_fifo+0x1d>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
lea    0xe7c(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jl     11a8 <reopen_fifo+0x48>
mov    -0x8(%rbp),%edi
call   1040 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    11c5 <reopen_fifo+0x65>
lea    0xe51(%rip),%rdi
call   1030 <unlink@plt>
lea    0xe45(%rip),%rdi
mov    $0x1b0,%esi
call   1060 <mkfifo@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
or     (%rax),%esi
lea    0xe22(%rip),%rdi
call   1080 <open@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reopen_fifo>:
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
js     116e <reopen_fifo+0xe>
call   1050 <close@plt>
lea    0xe8b(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     1191 <reopen_fifo+0x31>
mov    0x8(%rsp),%edi
call   1040 <S_ISFIFO@plt>
test   %eax,%eax
jne    11ad <reopen_fifo+0x4d>
lea    0xe68(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    $0x1b0,%esi
call   1060 <mkfifo@plt>
mov    0x2e34(%rip),%rax
mov    0x2e15(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1080 <open@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"/dev/initctl\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i32 @reopen_fifo(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.stat
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp sge i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* %2
%8 = call i32 @close(i32 %7)
br label %9
9: ; preds = %6, %1
%10 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%11 = icmp slt i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @S_ISFIFO(i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %12, %9
%18 = call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 432)
br label %20
20: ; preds = %17, %12
%21 = load i32, i32* @O_RDONLY
%22 = load i32, i32* @O_CLOEXEC
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %23)
ret i32 %24
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"/dev/initctl\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i32 @reopen_fifo(i32 %0) {
%2 = alloca %struct.stat
%3 = icmp sgt i32 %0, -1
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @close(i32 %0)
br label %6
6: ; preds = %4, %1
%7 = bitcast %struct.stat* %2 to i8*
%8 = call i64 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%9 = icmp slt i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %6
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @S_ISFIFO(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10, %6
%16 = call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @mkfifo(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 432)
br label %18
18: ; preds = %15, %10
%19 = load i32, i32* @O_RDONLY
%20 = load i32, i32* @O_CLOEXEC
%21 = or i32 %20, %19
%22 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %21)
ret i32 %22
}