#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*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int tty_open(char *path) {
  int res;
  int o_fd = open(path, O_RDWR);
  if (o_fd == -1)
    return 0;
  close(0);
  close(1);
  close(2);
  res = dup2(o_fd, 0);
  if (res == -1) {
    close(o_fd);
    return 0;
  }
  res = dup2(o_fd, 1);
  if (res == -1) {
    close(o_fd);
    return 0;
  }
  res = dup2(o_fd, 2);
  if (res == -1) {
    close(o_fd);
    return 0;
  }
  close(o_fd);
  ioctl(0, TIOCSCTTY, 1);
  return 1;
}


x86 assembly (clang, -O0):
<tty_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1177 <tty_open+0x37>
movl   $0x0,-0x4(%rbp)
jmp    123d <tty_open+0xfd>
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bd <tty_open+0x7d>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    123d <tty_open+0xfd>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11eb <tty_open+0xab>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    123d <tty_open+0xfd>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1219 <tty_open+0xd9>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    123d <tty_open+0xfd>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <ioctl@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_open>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11de <tty_open+0x9e>
mov    %eax,%ebx
xor    %ebp,%ebp
xor    %edi,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     11ce <tty_open+0x8e>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     11d7 <tty_open+0x97>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <dup2@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%ebp
je     11de <tty_open+0x9e>
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%ebp
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <ioctl@plt>
jmp    11e0 <tty_open+0xa0>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11e0 <tty_open+0xa0>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@TIOCSCTTY = global i32 0
define i32 @tty_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 0, i32* %2
br label %44
12: ; preds = %1
%13 = call i32 @close(i32 0)
%14 = call i32 @close(i32 1)
%15 = call i32 @close(i32 2)
%16 = load i32, i32* %5
%17 = call i32 @dup2(i32 %16, i32 0)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = load i32, i32* %5
%22 = call i32 @close(i32 %21)
store i32 0, i32* %2
br label %44
23: ; preds = %12
%24 = load i32, i32* %5
%25 = call i32 @dup2(i32 %24, i32 1)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
store i32 0, i32* %2
br label %44
31: ; preds = %23
%32 = load i32, i32* %5
%33 = call i32 @dup2(i32 %32, i32 2)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* %5
%38 = call i32 @close(i32 %37)
store i32 0, i32* %2
br label %44
39: ; preds = %31
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @TIOCSCTTY
%43 = call i32 @ioctl(i32 0, i32 %42, i32 1)
store i32 1, i32* %2
br label %44
44: ; preds = %39, %36, %28, %20, %11
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@TIOCSCTTY = global i32 0
define i32 @tty_open(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %25, label %5
5: ; preds = %1
%6 = tail call i32 @close(i32 0)
%7 = tail call i32 @close(i32 1)
%8 = tail call i32 @close(i32 2)
%9 = tail call i32 @dup2(i32 %3, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %5
%12 = tail call i32 @close(i32 %3)
br label %25
13: ; preds = %5
%14 = tail call i32 @dup2(i32 %3, i32 1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @close(i32 %3)
br label %25
18: ; preds = %13
%19 = tail call i32 @dup2(i32 %3, i32 2)
%20 = icmp eq i32 %19, -1
%21 = tail call i32 @close(i32 %3)
br i1 %20, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* @TIOCSCTTY
%24 = tail call i32 @ioctl(i32 0, i32 %23, i32 1)
br label %25
25: ; preds = %18, %1, %22, %16, %11
%26 = phi i32 [ 0, %11 ], [ 0, %16 ], [ 1, %22 ], [ 0, %1 ], [ 0, %18 ]
ret i32 %26
}