#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ mkfifo(char *, int);
int open(char *, int);
int /*<<< orphan*/ stderr;
FILE *open_stream(char *device, int open_flags, char *typo) {
  FILE *ret;
  int file_desc = open(device, open_flags);
  if (file_desc < 0) {
    if (mkfifo(device, S_IRUSR | S_IWUSR)) {
      fprintf(stderr, "can't create stream %s\n", device);
      return NULL;
    } else {
      file_desc = open(device, open_flags);
    }
  }
  ret = fdopen(file_desc, typo);
  if (!ret) {
    fprintf(stderr, "can't access stream or file %s\n", device);
    return NULL;
  }
  return ret;
}


x86 assembly (clang, -O0):
<open_stream>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11cb <open_stream+0x8b>
mov    -0x10(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
call   1040 <mkfifo@plt>
cmp    $0x0,%rax
je     11b7 <open_stream+0x77>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5b(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1214 <open_stream+0xd4>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
jmp    11cb <open_stream+0x8b>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    120c <open_stream+0xcc>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1e(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1214 <open_stream+0xd4>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_stream>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
call   1060 <open@plt>
test   %eax,%eax
js     116d <open_stream+0x2d>
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <fdopen@plt>
test   %rax,%rax
jne    11a6 <open_stream+0x66>
lea    0xead(%rip),%rsi
jmp    1193 <open_stream+0x53>
mov    0x2e5c(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <mkfifo@plt>
test   %rax,%rax
je     11ab <open_stream+0x6b>
lea    0xe6d(%rip),%rsi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <open@plt>
jmp    1155 <open_stream+0x15>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"can't create stream %s\0A\00"
@.str.1 = [32 x i8] c"can't access stream or file %s\0A\00"
define i32* @open_stream(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %31
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i64 @mkfifo(i8* %16, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %5
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %24)
store i32* null, i32** %4
br label %43
26: ; preds = %15
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %9
br label %30
30: ; preds = %26
br label %31
31: ; preds = %30, %3
%32 = load i32, i32* %9
%33 = load i8*, i8** %7
%34 = call i32* @fdopen(i32 %32, i8* %33)
store i32* %34, i32** %8
%35 = load i32*, i32** %8
%36 = icmp ne i32* %35, null
br i1 %36, label %41, label %37
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %5
%40 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %39)
store i32* null, i32** %4
br label %43
41: ; preds = %31
%42 = load i32*, i32** %8
store i32* %42, i32** %4
br label %43
43: ; preds = %41, %37, %22
%44 = load i32*, i32** %4
ret i32* %44
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"can't create stream %s\0A\00"
@.str.1 = [32 x i8] c"can't access stream or file %s\0A\00"
define noalias i32* @open_stream(i8* %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32 @open(i8* %0, i32 %1)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = load i32, i32* @S_IRUSR
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %8, %7
%10 = tail call i64 @mkfifo(i8* %0, i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = tail call i32 @open(i8* %0, i32 %1)
br label %14
14: ; preds = %12, %3
%15 = phi i32 [ %13, %12 ], [ %4, %3 ]
%16 = tail call i32* @fdopen(i32 %15, i8* %2)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %14, %6
%19 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %14 ]
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* %19, i8* %0)
br label %22
22: ; preds = %18, %14
%23 = phi i32* [ %16, %14 ], [ null, %18 ]
ret i32* %23
}