#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*/ LOG_INFO;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ getlogin();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/, ...);
int /*<<< orphan*/ ttyname(int);
void test_tty_exist() {
  int fdtty = open("/dev/tty", O_RDWR);
  if (fdtty == -1)
    syslog(LOG_INFO, "open default tty failed, errno = %d\n", errno);
  else
    syslog(LOG_INFO, "open default tty OK, tty: %s, login: %s\n",
           ttyname(fdtty), getlogin());
  close(fdtty);
}


x86 assembly (clang, -O0):
<test_tty_exist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    119f <test_tty_exist+0x4f>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1040 <syslog@plt>
jmp    11d3 <test_tty_exist+0x83>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <ttyname@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getlogin@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%edx
mov    %eax,%ecx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1040 <syslog@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_tty_exist>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
lea    0xe9c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e5e(%rip),%rax
mov    (%rax),%ebp
cmp    $0xffffffff,%ebx
je     11a1 <test_tty_exist+0x51>
mov    %ebx,%edi
call   1060 <ttyname@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1050 <getlogin@plt>
lea    0xe9d(%rip),%rsi
mov    %ebp,%edi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <syslog@plt>
jmp    11ba <test_tty_exist+0x6a>
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
lea    0xe58(%rip),%rsi
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <syslog@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <close@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@LOG_INFO = global i32 0
@.str.1 = [37 x i8] c"open default tty failed, errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"open default tty OK, tty: %s, login: %s\0A\00"
define void @test_tty_exist() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* @LOG_INFO
%8 = load i32, i32* @errno
%9 = call i32 (i32, i8*, i32, ...) @syslog(i32 %7, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %16
10: ; preds = %0
%11 = load i32, i32* @LOG_INFO
%12 = load i32, i32* %1
%13 = call i32 @ttyname(i32 %12)
%14 = call i32 (...) @getlogin()
%15 = call i32 (i32, i8*, i32, ...) @syslog(i32 %11, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %14)
br label %16
16: ; preds = %10, %6
%17 = load i32, i32* %1
%18 = call i32 @close(i32 %17)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@LOG_INFO = global i32 0
@.str.1 = [37 x i8] c"open default tty failed, errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"open default tty OK, tty: %s, login: %s\0A\00"
define void @test_tty_exist() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
%4 = load i32, i32* @LOG_INFO
br i1 %3, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %4, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %12
8: ; preds = %0
%9 = tail call i32 @ttyname(i32 %2)
%10 = tail call i32 (...) @getlogin()
%11 = tail call i32 (i32, i8*, i32, ...) @syslog(i32 %4, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 %10)
br label %12
12: ; preds = %8, %5
%13 = tail call i32 @close(i32 %2)
ret void
}