#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
void ensure_console(void) {
  for (unsigned int i = 0; i < 1000; ++i) {
    int fd = open("/dev/console", O_RDWR);
    if (fd < 0) {
      usleep(50000);
      continue;
    }
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
    close(fd);
    if (write(1, "\0\0\0\0\n", 5) == 5)
      return;
  }
  panic("Unable to open console device");
}


x86 assembly (clang, -O0):
<ensure_console>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0x3e8,-0x4(%rbp)
jae    1210 <ensure_console+0xb0>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ad <ensure_console+0x4d>
mov    $0xc350,%edi
call   1080 <usleep@plt>
jmp    1202 <ensure_console+0xa2>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%edi
lea    0xe46(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x5,%eax
jne    11fd <ensure_console+0x9d>
jmp    121c <ensure_console+0xbc>
jmp    1202 <ensure_console+0xa2>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    116f <ensure_console+0xf>
lea    0xdf6(%rip),%rdi
call   1050 <panic@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensure_console>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x3e8,%ebx
mov    0x2e74(%rip),%r12
lea    0xe85(%rip),%r15
lea    0xea9(%rip),%r14
jmp    119f <ensure_console+0x3f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0xc350,%edi
call   1080 <usleep@plt>
add    $0xffffffff,%ebx
je     11f9 <ensure_console+0x99>
mov    (%r12),%esi
mov    %r15,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1190 <ensure_console+0x30>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x5,%eax
jne    119a <ensure_console+0x3a>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0d(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <panic@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"\00\00\00\00\0A\00"
@.str.2 = [30 x i8] c"Unable to open console device\00"
define void @ensure_console() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
br label %3
3: ; preds = %26, %0
%4 = load i32, i32* %1
%5 = icmp ult i32 %4, 1000
br i1 %5, label %6, label %29
6: ; preds = %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i32 @usleep(i32 50000)
br label %26
13: ; preds = %6
%14 = load i32, i32* %2
%15 = call i32 @dup2(i32 %14, i32 0)
%16 = load i32, i32* %2
%17 = call i32 @dup2(i32 %16, i32 1)
%18 = load i32, i32* %2
%19 = call i32 @dup2(i32 %18, i32 2)
%20 = load i32, i32* %2
%21 = call i32 @close(i32 %20)
%22 = call i32 @write(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%23 = icmp eq i32 %22, 5
br i1 %23, label %24, label %25
24: ; preds = %13
br label %31
25: ; preds = %13
br label %26
26: ; preds = %25, %11
%27 = load i32, i32* %1
%28 = add i32 %27, 1
store i32 %28, i32* %1
br label %3
29: ; preds = %3
%30 = call i32 @panic(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"\00\00\00\00\0A\00"
@.str.2 = [30 x i8] c"Unable to open console device\00"
define void @ensure_console() {
br label %1
1: ; preds = %0, %15
%2 = phi i32 [ 0, %0 ], [ %16, %15 ]
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @usleep(i32 50000)
br label %15
8: ; preds = %1
%9 = tail call i32 @dup2(i32 %4, i32 0)
%10 = tail call i32 @dup2(i32 %4, i32 1)
%11 = tail call i32 @dup2(i32 %4, i32 2)
%12 = tail call i32 @close(i32 %4)
%13 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%14 = icmp eq i32 %13, 5
br i1 %14, label %20, label %15
15: ; preds = %6, %8
%16 = add nuw nsw i32 %2, 1
%17 = icmp eq i32 %16, 1000
br i1 %17, label %18, label %1
18: ; preds = %15
%19 = tail call i32 @panic(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %8, %18
ret void
}