#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__ ENXIO;
int O_NONBLOCK;
int O_WRONLY;
scalar_t__ errno;
int /*<<< orphan*/ ltr_int_log_message(char *);
int /*<<< orphan*/ ltr_int_make_fifo(char const *);
int /*<<< orphan*/ ltr_int_my_perror(char *);
int open(char const *, int);
int /*<<< orphan*/ sleep(int);
int ltr_int_open_fifo_for_writing(const char *name, bool waitForOpen) {
  if (!ltr_int_make_fifo(name)) {
    ltr_int_log_message("Failed to create fifo for writing!\n");
    return -1;
  }
  int fifo = -1;
  int timeout = 3;
  while (timeout > 0) {
    --timeout;
    if ((fifo = open(name, O_WRONLY | O_NONBLOCK)) < 0) {
      ltr_int_my_perror("open_fifo_for_writing");
      if (errno != ENXIO) {
        ltr_int_my_perror("open@open_fifo_for_writing");
        return -1;
      }
      fifo = -1;
      if (!waitForOpen) {
        return -1;
      }
      sleep(1);
    } else {
      break;
    }
  }
  return fifo;
}


x86 assembly (clang, -O0):
<ltr_int_open_fifo_for_writing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ltr_int_make_fifo@plt>
cmp    $0x0,%eax
jne    1189 <ltr_int_open_fifo_for_writing+0x39>
lea    0xe88(%rip),%rdi
call   1030 <ltr_int_log_message@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <ltr_int_open_fifo_for_writing+0xfb>
movl   $0xffffffff,-0x18(%rbp)
movl   $0x3,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1245 <ltr_int_open_fifo_for_writing+0xf5>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    0x2e2a(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    123b <ltr_int_open_fifo_for_writing+0xeb>
lea    0xe51(%rip),%rdi
call   1050 <ltr_int_my_perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%rax
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
je     120f <ltr_int_open_fifo_for_writing+0xbf>
lea    0xe26(%rip),%rdi
call   1050 <ltr_int_my_perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <ltr_int_open_fifo_for_writing+0xfb>
movl   $0xffffffff,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    122c <ltr_int_open_fifo_for_writing+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    124b <ltr_int_open_fifo_for_writing+0xfb>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1240 <ltr_int_open_fifo_for_writing+0xf0>
jmp    1245 <ltr_int_open_fifo_for_writing+0xf5>
jmp    1197 <ltr_int_open_fifo_for_writing+0x47>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ltr_int_open_fifo_for_writing>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <ltr_int_make_fifo@plt>
test   %eax,%eax
je     1236 <ltr_int_open_fifo_for_writing+0xe6>
mov    0x2e4c(%rip),%r14
mov    0x2e6d(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    1255 <ltr_int_open_fifo_for_writing+0x105>
lea    0xe91(%rip),%rdi
call   1050 <ltr_int_my_perror@plt>
mov    0x2e34(%rip),%r12
mov    (%r12),%rax
mov    0x2e21(%rip),%r13
cmp    0x0(%r13),%rax
jne    1244 <ltr_int_open_fifo_for_writing+0xf4>
mov    $0xffffffff,%eax
test   %ebp,%ebp
je     1255 <ltr_int_open_fifo_for_writing+0x105>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    1255 <ltr_int_open_fifo_for_writing+0x105>
lea    0xe40(%rip),%rdi
call   1050 <ltr_int_my_perror@plt>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1244 <ltr_int_open_fifo_for_writing+0xf4>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
jns    1255 <ltr_int_open_fifo_for_writing+0x105>
lea    0xe0e(%rip),%rdi
call   1050 <ltr_int_my_perror@plt>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    1244 <ltr_int_open_fifo_for_writing+0xf4>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1250 <ltr_int_open_fifo_for_writing+0x100>
lea    0xdc3(%rip),%rdi
call   1030 <ltr_int_log_message@plt>
jmp    1250 <ltr_int_open_fifo_for_writing+0x100>
lea    0xdd9(%rip),%rdi
call   1050 <ltr_int_my_perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Failed to create fifo for writing!\0A\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"open_fifo_for_writing\00"
@errno = global i64 0
@ENXIO = global i64 0
@.str.2 = [27 x i8] c"open@open_fifo_for_writing\00"
define i32 @ltr_int_open_fifo_for_writing(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call i32 @ltr_int_make_fifo(i8* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 @ltr_int_log_message(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
13: ; preds = %2
store i32 -1, i32* %6
store i32 3, i32* %7
br label %14
14: ; preds = %40, %13
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %41
17: ; preds = %14
%18 = load i32, i32* %7
%19 = add nsw i32 %18, -1
store i32 %19, i32* %7
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %39
26: ; preds = %17
%27 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENXIO
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
33: ; preds = %26
store i32 -1, i32* %6
%34 = load i32, i32* %5
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %33
store i32 -1, i32* %3
br label %43
37: ; preds = %33
%38 = call i32 @sleep(i32 1)
br label %40
39: ; preds = %17
br label %41
40: ; preds = %37
br label %14
41: ; preds = %39, %14
%42 = load i32, i32* %6
store i32 %42, i32* %3
br label %43
43: ; preds = %41, %36, %31, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [36 x i8] c"Failed to create fifo for writing!\0A\00"
@O_WRONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [22 x i8] c"open_fifo_for_writing\00"
@errno = global i64 0
@ENXIO = global i64 0
@.str.2 = [27 x i8] c"open@open_fifo_for_writing\00"
define i32 @ltr_int_open_fifo_for_writing(i8* %0, i32 %1) {
%3 = tail call i32 @ltr_int_make_fifo(i8* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %12, label %5
5: ; preds = %2
%6 = icmp eq i32 %1, 0
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %14, label %48
12: ; preds = %2
%13 = tail call i32 @ltr_int_log_message(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
br label %48
14: ; preds = %5
%15 = tail call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENXIO
%18 = icmp eq i64 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %41, %29, %14
%20 = tail call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %48
21: ; preds = %14
br i1 %6, label %48, label %22
22: ; preds = %21
%23 = tail call i32 @sleep(i32 1)
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* %0, i32 %26)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %22
%30 = tail call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENXIO
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %19
34: ; preds = %29
%35 = tail call i32 @sleep(i32 1)
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %37, %36
%39 = tail call i32 @open(i8* %0, i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %34
%42 = tail call i32 @ltr_int_my_perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%43 = load i64, i64* @errno
%44 = load i64, i64* @ENXIO
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %19
46: ; preds = %41
%47 = tail call i32 @sleep(i32 1)
br label %48
48: ; preds = %21, %5, %22, %34, %46, %19, %12
%49 = phi i32 [ -1, %12 ], [ -1, %19 ], [ -1, %21 ], [ %10, %5 ], [ %27, %22 ], [ %39, %34 ], [ -1, %46 ]
ret i32 %49
}