#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 */

/* Variables and functions */
scalar_t__ EEXIST;
int O_NDELAY;
int O_RDONLY;
scalar_t__ errno;
int mkfifo(char const *, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int open_pipe(const char *pipe_name) {
  int fd, rv;
  rv = mkfifo(pipe_name, 0666);
  if ((rv == -1) && (errno != EEXIST)) {
    perror("Error creating the named pipe");
    return rv;
  }
  if ((fd = open("/tmp/challenge_fifo", O_RDONLY | O_NDELAY)) < 0) {
    perror("Error opening pipe for reading");
    return fd;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   1030 <mkfifo@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1188 <open_pipe+0x58>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
je     1188 <open_pipe+0x58>
lea    0xe88(%rip),%rdi
call   1050 <perror@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <open_pipe+0x9f>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e30(%rip),%rax
or     (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11c9 <open_pipe+0x99>
lea    0xe79(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <open_pipe+0x9f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_pipe>:
push   %rbx
mov    $0x1b6,%esi
call   1030 <mkfifo@plt>
cmp    $0xffffffff,%eax
jne    1156 <open_pipe+0x26>
mov    0x2e89(%rip),%rax
mov    (%rax),%rax
mov    0x2e87(%rip),%rcx
cmp    (%rcx),%rax
jne    1187 <open_pipe+0x57>
mov    0x2e8b(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeaf(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     117e <open_pipe+0x4e>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xead(%rip),%rdi
jmp    1193 <open_pipe+0x63>
mov    $0xffffffff,%ebx
lea    0xe6d(%rip),%rdi
call   1050 <perror@plt>
jmp    117a <open_pipe+0x4a>

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
@.str = [30 x i8] c"Error creating the named pipe\00"
@.str.1 = [20 x i8] c"/tmp/challenge_fifo\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [31 x i8] c"Error opening pipe for reading\00"
define i32 @open_pipe(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @mkfifo(i8* %6, i32 438)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @EEXIST
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %5
store i32 %16, i32* %2
br label %28
17: ; preds = %10, %1
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_NDELAY
%20 = or i32 %18, %19
%21 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 %21, i32* %4
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %28
26: ; preds = %17
%27 = load i32, i32* %4
store i32 %27, i32* %2
br label %28
28: ; preds = %26, %23, %14
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
@.str = [30 x i8] c"Error creating the named pipe\00"
@.str.1 = [20 x i8] c"/tmp/challenge_fifo\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [31 x i8] c"Error opening pipe for reading\00"
define i32 @open_pipe(i8* %0) {
%2 = tail call i32 @mkfifo(i8* %0, i32 438)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i64, i64* @errno
%6 = load i64, i64* @EEXIST
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %14
8: ; preds = %4, %1
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_NDELAY
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %8, %4
%15 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), %8 ]
%16 = phi i32 [ -1, %4 ], [ %12, %8 ]
%17 = tail call i32 @perror(i8* %15)
br label %18
18: ; preds = %14, %8
%19 = phi i32 [ %12, %8 ], [ %16, %14 ]
ret i32 %19
}