#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 _tty_error(char *, int);
int isatty(int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
char *ttyname(int /*<<< orphan*/);
int _tty(void) {
  char *tty;
  if (isatty(0) == 1) {
    if ((tty = ttyname(0)) == NULL)
      return _tty_error("ttyname", 2);
    puts(tty);
    return 0;
  }
  puts("not a tty");
  return 1;
}


x86 assembly (clang, -O0):
<_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
call   1040 <isatty@plt>
cmp    $0x1,%eax
jne    119b <_tty+0x5b>
xor    %edi,%edi
call   1050 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    1186 <_tty+0x46>
lea    0xe8c(%rip),%rdi
mov    $0x2,%esi
call   1060 <_tty_error@plt>
mov    %eax,-0x4(%rbp)
jmp    11ae <_tty+0x6e>
mov    -0x10(%rbp),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ae <_tty+0x6e>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tty>:
push   %rbx
xor    %edi,%edi
call   1040 <isatty@plt>
cmp    $0x1,%eax
jne    1165 <_tty+0x25>
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <ttyname@plt>
test   %rax,%rax
je     117a <_tty+0x3a>
mov    %rax,%rdi
call   1030 <puts@plt>
jmp    1176 <_tty+0x36>
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe7f(%rip),%rdi
mov    $0x2,%esi
pop    %rbx
jmp    1060 <_tty_error@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"ttyname\00"
@.str.1 = [10 x i8] c"not a tty\00"
define i32 @_tty() {
%1 = alloca i32
%2 = alloca i8*
%3 = call i32 @isatty(i32 0)
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %13
5: ; preds = %0
%6 = call i8* @ttyname(i32 0)
store i8* %6, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = call i32 @_tty_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 2)
store i32 %9, i32* %1
br label %15
10: ; preds = %5
%11 = load i8*, i8** %2
%12 = call i32 @puts(i8* %11)
store i32 0, i32* %1
br label %15
13: ; preds = %0
%14 = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %15
15: ; preds = %13, %10, %8
%16 = load i32, i32* %1
ret i32 %16
}

LLVM IR (-O2):
@.str = [8 x i8] c"ttyname\00"
@.str.1 = [10 x i8] c"not a tty\00"
define i32 @_tty() {
%1 = tail call i32 @isatty(i32 0)
%2 = icmp eq i32 %1, 1
br i1 %2, label %3, label %10
3: ; preds = %0
%4 = tail call i8* @ttyname(i32 0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @_tty_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 2)
br label %12
8: ; preds = %3
%9 = tail call i32 @puts(i8* %4)
br label %12
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %8, %6
%13 = phi i32 [ %7, %6 ], [ 0, %8 ], [ 1, %10 ]
ret i32 %13
}