#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 */
int EIO;
int ENOTTY;
int /*<<< orphan*/ close_nointr_nofail(int);
int errno;
int isatty(int);
int open(char const *, int);
int /*<<< orphan*/ usleep(int);
int open_terminal(const char *name, int mode) {
  int fd, r;
  unsigned c = 0;
  for (;;) {
    if ((fd = open(name, mode)) >= 0)
      break;
    if (errno != EIO)
      return -errno;
    if (c >= 20)
      return -errno;
    usleep(50 * 1000ULL);
    c++;
  }
  if (fd < 0)
    return -errno;
  if ((r = isatty(fd)) < 0) {
    close_nointr_nofail(fd);
    return -errno;
  }
  if (!r) {
    close_nointr_nofail(fd);
    return -ENOTTY;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_terminal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     1173 <open_terminal+0x33>
jmp    11d3 <open_terminal+0x93>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%eax
je     119e <open_terminal+0x5e>
xor    %eax,%eax
mov    0x2e3c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <open_terminal+0x10a>
cmpl   $0x14,-0x20(%rbp)
jb     11bb <open_terminal+0x7b>
xor    %eax,%eax
mov    0x2e1f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <open_terminal+0x10a>
mov    $0xc350,%edi
call   1060 <usleep@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1156 <open_terminal+0x16>
cmpl   $0x0,-0x18(%rbp)
jge    11f0 <open_terminal+0xb0>
xor    %eax,%eax
mov    0x2dea(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <open_terminal+0x10a>
mov    -0x18(%rbp),%edi
call   1030 <isatty@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    121f <open_terminal+0xdf>
mov    -0x18(%rbp),%edi
call   1040 <close_nointr_nofail@plt>
xor    %eax,%eax
mov    0x2dbb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <open_terminal+0x10a>
cmpl   $0x0,-0x1c(%rbp)
jne    1244 <open_terminal+0x104>
mov    -0x18(%rbp),%edi
call   1040 <close_nointr_nofail@plt>
xor    %eax,%eax
mov    0x2dae(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <open_terminal+0x10a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_terminal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1050 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1179 <open_terminal+0x39>
mov    %ebx,%edi
call   1030 <isatty@plt>
test   %eax,%eax
js     11bc <open_terminal+0x7c>
jne    11d2 <open_terminal+0x92>
mov    %ebx,%edi
call   1040 <close_nointr_nofail@plt>
mov    0x2e71(%rip),%rax
jmp    11ca <open_terminal+0x8a>
mov    $0x15,%ebp
mov    0x2e4b(%rip),%r12
mov    0x2e4c(%rip),%r13
nopl   0x0(%rax)
mov    (%r12),%ebx
cmp    0x0(%r13),%ebx
jne    11d0 <open_terminal+0x90>
add    $0xffffffff,%ebp
je     11d0 <open_terminal+0x90>
mov    $0xc350,%edi
call   1060 <usleep@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1050 <open@plt>
test   %eax,%eax
js     1190 <open_terminal+0x50>
mov    %eax,%ebx
jmp    115c <open_terminal+0x1c>
mov    %ebx,%edi
call   1040 <close_nointr_nofail@plt>
mov    0x2e06(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11d2 <open_terminal+0x92>
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@EIO = global i32 0
@ENOTTY = global i32 0
define i32 @open_terminal(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %8
br label %9
9: ; preds = %28, %2
%10 = load i8*, i8** %4
%11 = load i32, i32* %5
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
br label %32
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = load i32, i32* @EIO
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %57
22: ; preds = %15
%23 = load i32, i32* %8
%24 = icmp uge i32 %23, 20
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* %3
br label %57
28: ; preds = %22
%29 = call i32 @usleep(i32 50000)
%30 = load i32, i32* %8
%31 = add i32 %30, 1
store i32 %31, i32* %8
br label %9
32: ; preds = %14
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %3
br label %57
38: ; preds = %32
%39 = load i32, i32* %6
%40 = call i32 @isatty(i32 %39)
store i32 %40, i32* %7
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* %6
%44 = call i32 @close_nointr_nofail(i32 %43)
%45 = load i32, i32* @errno
%46 = sub nsw i32 0, %45
store i32 %46, i32* %3
br label %57
47: ; preds = %38
%48 = load i32, i32* %7
%49 = icmp ne i32 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %47
%51 = load i32, i32* %6
%52 = call i32 @close_nointr_nofail(i32 %51)
%53 = load i32, i32* @ENOTTY
%54 = sub nsw i32 0, %53
store i32 %54, i32* %3
br label %57
55: ; preds = %47
%56 = load i32, i32* %6
store i32 %56, i32* %3
br label %57
57: ; preds = %55, %50, %42, %35, %25, %19
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@errno = global i32 0
@EIO = global i32 0
@ENOTTY = global i32 0
define i32 @open_terminal(i8* nocapture %0, i32 %1) {
%3 = tail call i32 @open(i8* %0, i32 %1)
%4 = icmp sgt i32 %3, -1
br i1 %4, label %21, label %5
5: ; preds = %2, %16
%6 = phi i32 [ %18, %16 ], [ 0, %2 ]
%7 = load i32, i32* @errno
%8 = load i32, i32* @EIO
%9 = icmp eq i32 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = sub nsw i32 0, %7
br label %35
12: ; preds = %5
%13 = icmp eq i32 %6, 20
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = sub nsw i32 0, %7
br label %35
16: ; preds = %12
%17 = tail call i32 @usleep(i32 50000)
%18 = add nuw nsw i32 %6, 1
%19 = tail call i32 @open(i8* %0, i32 %1)
%20 = icmp sgt i32 %19, -1
br i1 %20, label %21, label %5
21: ; preds = %16, %2
%22 = phi i32 [ %3, %2 ], [ %19, %16 ]
%23 = tail call i32 @isatty(i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = tail call i32 @close_nointr_nofail(i32 %22)
%27 = load i32, i32* @errno
%28 = sub nsw i32 0, %27
br label %35
29: ; preds = %21
%30 = icmp eq i32 %23, 0
br i1 %30, label %31, label %35
31: ; preds = %29
%32 = tail call i32 @close_nointr_nofail(i32 %22)
%33 = load i32, i32* @ENOTTY
%34 = sub nsw i32 0, %33
br label %35
35: ; preds = %29, %31, %25, %14, %10
%36 = phi i32 [ %28, %25 ], [ %34, %31 ], [ %11, %10 ], [ %15, %14 ], [ %22, %29 ]
ret i32 %36
}