#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ LOG_ERR;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
int open(char const *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
void opentty(const char *tty) {
  int i, fd, flags;
  fd = open(tty, O_RDWR | O_NONBLOCK);
  if (fd == -1) {
    syslog(LOG_ERR, "FATAL: can't reopen tty: %s", strerror(errno));
    sleep(1);
    exit(1);
  }
  flags = fcntl(fd, F_GETFL);
  flags &= ~O_NONBLOCK;
  fcntl(fd, F_SETFL, flags);
  for (i = 0; i < fd; i++)
    close(i);
  for (i = 0; i < 3; i++)
    if (fd != i)
      dup2(fd, i);
  if (fd >= 3)
    close(fd);
}


x86 assembly (clang, -O0):
<opentty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f3 <opentty+0x73>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe26(%rip),%rsi
call   1060 <syslog@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2dab(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    125a <opentty+0xda>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1238 <opentty+0xb8>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    1295 <opentty+0x115>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1282 <opentty+0x102>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <dup2@plt>
jmp    1287 <opentty+0x107>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1261 <opentty+0xe1>
cmpl   $0x3,-0x10(%rbp)
jl     12a7 <opentty+0x127>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opentty>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e5d(%rip),%rax
mov    0x2e4e(%rip),%r14
mov    (%r14),%esi
or     (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     124c <opentty+0xcc>
mov    %eax,%ebx
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    (%r14),%edx
not    %edx
and    %eax,%edx
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %ebx,%ebx
jle    11ee <opentty+0x6e>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    11e0 <opentty+0x60>
mov    %ebx,%edi
test   %ebx,%ebx
je     122d <opentty+0xad>
xor    %esi,%esi
call   1040 <dup2@plt>
cmp    $0x1,%ebx
je     1237 <opentty+0xb7>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
cmp    $0x2,%ebx
je     1247 <opentty+0xc7>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
cmp    $0x3,%ebx
jl     1247 <opentty+0xc7>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <dup2@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d5d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd96(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1060 <syslog@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@LOG_ERR = global i32 0
@.str = [28 x i8] c"FATAL: can't reopen tty: %s\00"
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
define void @opentty(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %7, %8
%10 = call i32 @open(i8* %6, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = load i32, i32* @LOG_ERR
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @syslog(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @sleep(i32 1)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = load i32, i32* %4
%22 = load i32, i32* @F_GETFL
%23 = call i32 (i32, i32, ...) @fcntl(i32 %21, i32 %22)
store i32 %23, i32* %5
%24 = load i32, i32* @O_NONBLOCK
%25 = xor i32 %24, -1
%26 = load i32, i32* %5
%27 = and i32 %26, %25
store i32 %27, i32* %5
%28 = load i32, i32* %4
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* %5
%31 = call i32 (i32, i32, ...) @fcntl(i32 %28, i32 %29, i32 %30)
store i32 0, i32* %3
br label %32
32: ; preds = %39, %20
%33 = load i32, i32* %3
%34 = load i32, i32* %4
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36
%40 = load i32, i32* %3
%41 = add nsw i32 %40, 1
store i32 %41, i32* %3
br label %32
42: ; preds = %32
store i32 0, i32* %3
br label %43
43: ; preds = %55, %42
%44 = load i32, i32* %3
%45 = icmp slt i32 %44, 3
br i1 %45, label %46, label %58
46: ; preds = %43
%47 = load i32, i32* %4
%48 = load i32, i32* %3
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i32, i32* %4
%52 = load i32, i32* %3
%53 = call i32 @dup2(i32 %51, i32 %52)
br label %54
54: ; preds = %50, %46
br label %55
55: ; preds = %54
%56 = load i32, i32* %3
%57 = add nsw i32 %56, 1
store i32 %57, i32* %3
br label %43
58: ; preds = %43
%59 = load i32, i32* %4
%60 = icmp sge i32 %59, 3
br i1 %60, label %61, label %64
61: ; preds = %58
%62 = load i32, i32* %4
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %58
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@LOG_ERR = global i32 0
@.str = [28 x i8] c"FATAL: can't reopen tty: %s\00"
@errno = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
define void @opentty(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NONBLOCK
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %14
7: ; preds = %1
%8 = load i32, i32* @LOG_ERR
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @syslog(i32 %8, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = tail call i32 @sleep(i32 1)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32, i32* @F_GETFL
%16 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %15)
%17 = load i32, i32* @O_NONBLOCK
%18 = xor i32 %17, -1
%19 = and i32 %16, %18
%20 = load i32, i32* @F_SETFL
%21 = tail call i32 (i32, i32, ...) @fcntl(i32 %5, i32 %20, i32 %19)
%22 = icmp sgt i32 %5, 0
br i1 %22, label %27, label %23
23: ; preds = %27, %14
%24 = icmp eq i32 %5, 0
br i1 %24, label %25, label %32
25: ; preds = %23
%26 = tail call i32 @dup2(i32 %5, i32 1)
br label %38
27: ; preds = %14, %27
%28 = phi i32 [ %30, %27 ], [ 0, %14 ]
%29 = tail call i32 @close(i32 %28)
%30 = add nuw nsw i32 %28, 1
%31 = icmp eq i32 %30, %5
br i1 %31, label %23, label %27
32: ; preds = %23
%33 = tail call i32 @dup2(i32 %5, i32 0)
%34 = icmp eq i32 %5, 1
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = tail call i32 @dup2(i32 %5, i32 1)
%37 = icmp eq i32 %5, 2
br i1 %37, label %45, label %40
38: ; preds = %25, %32
%39 = tail call i32 @dup2(i32 %5, i32 2)
br label %45
40: ; preds = %35
%41 = tail call i32 @dup2(i32 %5, i32 2)
%42 = icmp sgt i32 %5, 2
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = tail call i32 @close(i32 %5)
br label %45
45: ; preds = %35, %38, %43, %40
ret void
}