#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 */
struct lt_config_app {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int getdtablesize();
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int tty_restore(struct lt_config_app *cfg) {
  int i, num_files = getdtablesize();
  for (i = 0; i < num_files; i++) {
    if (fcntl(i, F_GETFD, NULL) != -1)
      close(i);
  }
  open("/dev/tty", O_RDWR);
  dup(0);
  dup(0);
  return 0;
}


x86 assembly (clang, -O0):
<tty_restore>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <getdtablesize@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11b2 <tty_restore+0x62>
mov    -0xc(%rbp),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     119f <tty_restore+0x4f>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    11a4 <tty_restore+0x54>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116d <tty_restore+0x1d>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1060 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_restore>:
push   %rbp
push   %r14
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <getdtablesize@plt>
test   %eax,%eax
jle    1191 <tty_restore+0x41>
mov    %eax,%ebp
mov    0x2e6e(%rip),%r14
jmp    1177 <tty_restore+0x27>
nopl   0x0(%rax)
add    $0x1,%ebx
cmp    %ebx,%ebp
je     1191 <tty_restore+0x41>
mov    (%r14),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1170 <tty_restore+0x20>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1170 <tty_restore+0x20>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1060 <open@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %edi,%edi
call   1040 <dup@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.lt_config_app = type { i32 }
@F_GETFD = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
define i32 @tty_restore(%struct.lt_config_app* %0) {
%2 = alloca %struct.lt_config_app*
%3 = alloca i32
%4 = alloca i32
store %struct.lt_config_app* %0, %struct.lt_config_app** %2
%5 = call i32 (...) @getdtablesize()
store i32 %5, i32* %4
store i32 0, i32* %3
br label %6
6: ; preds = %19, %1
%7 = load i32, i32* %3
%8 = load i32, i32* %4
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %22
10: ; preds = %6
%11 = load i32, i32* %3
%12 = load i32, i32* @F_GETFD
%13 = call i32 @fcntl(i32 %11, i32 %12, i32* null)
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* %3
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %15, %10
br label %19
19: ; preds = %18
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
br label %6
22: ; preds = %6
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = call i32 @dup(i32 0)
%26 = call i32 @dup(i32 0)
ret i32 0
}

LLVM IR (-O2):
%struct.lt_config_app = type { i32 }
@F_GETFD = global i32 0
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
define i32 @tty_restore(%struct.lt_config_app* nocapture readnone %0) {
%2 = tail call i32 (...) @getdtablesize()
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %14
4: ; preds = %1, %11
%5 = phi i32 [ %12, %11 ], [ 0, %1 ]
%6 = load i32, i32* @F_GETFD
%7 = tail call i32 @fcntl(i32 %5, i32 %6, i32* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @close(i32 %5)
br label %11
11: ; preds = %4, %9
%12 = add nuw nsw i32 %5, 1
%13 = icmp eq i32 %12, %2
br i1 %13, label %14, label %4
14: ; preds = %11, %1
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = tail call i32 @dup(i32 0)
%18 = tail call i32 @dup(i32 0)
ret i32 0
}