#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int fstat(int, struct stat *);
int /*<<< orphan*/ isatty(int);
char *ttynameCheckDir(struct stat *, char *);
char *ttyname(int fd) {
  char *d;
  struct stat fdStat;
  if (!isatty(fd))
    return NULL;
  if (fstat(fd, &fdStat) == -1)
    return NULL;
  if (!S_ISCHR(fdStat.st_mode))
    return NULL;
  d = ttynameCheckDir(&fdStat, "/dev/pts");
  return (d != NULL) ? d : ttynameCheckDir(&fdStat, "/dev");
}


x86 assembly (clang, -O0):
<ttyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1169 <ttyname+0x29>
movq   $0x0,-0x8(%rbp)
jmp    11f1 <ttyname+0xb1>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    118b <ttyname+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    11f1 <ttyname+0xb1>
mov    -0x20(%rbp),%edi
call   1040 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    11a9 <ttyname+0x69>
movq   $0x0,-0x8(%rbp)
jmp    11f1 <ttyname+0xb1>
lea    -0x20(%rbp),%rdi
lea    0xe4c(%rip),%rsi
call   1050 <ttynameCheckDir@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d5 <ttyname+0x95>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11e9 <ttyname+0xa9>
lea    -0x20(%rbp),%rdi
lea    0xe29(%rip),%rsi
call   1050 <ttynameCheckDir@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyname>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
call   1030 <isatty@plt>
test   %eax,%eax
je     1197 <ttyname+0x57>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
je     1197 <ttyname+0x57>
mov    0x8(%rsp),%edi
call   1040 <S_ISCHR@plt>
test   %eax,%eax
je     1197 <ttyname+0x57>
lea    0xe8b(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1050 <ttynameCheckDir@plt>
test   %rax,%rax
jne    1199 <ttyname+0x59>
lea    0xe7e(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1050 <ttynameCheckDir@plt>
jmp    1199 <ttyname+0x59>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"/dev/pts\00"
@.str.1 = [5 x i8] c"/dev\00"
define i8* @ttyname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @isatty(i32 %6)
%8 = icmp ne i32 %7, 0
br i1 %8, label %10, label %9
9: ; preds = %1
store i8* null, i8** %2
br label %31
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i32 @fstat(i32 %11, %struct.stat* %5)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %10
store i8* null, i8** %2
br label %31
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @S_ISCHR(i32 %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %15
store i8* null, i8** %2
br label %31
21: ; preds = %15
%22 = call i8* @ttynameCheckDir(%struct.stat* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %22, i8** %4
%23 = load i8*, i8** %4
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i8*, i8** %4
br label %29
27: ; preds = %21
%28 = call i8* @ttynameCheckDir(%struct.stat* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
%30 = phi i8* [ %26, %25 ], [ %28, %27 ]
store i8* %30, i8** %2
br label %31
31: ; preds = %29, %20, %14, %9
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"/dev/pts\00"
@.str.1 = [5 x i8] c"/dev\00"
define i8* @ttyname(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @isatty(i32 %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %19, label %6
6: ; preds = %1
%7 = call i32 @fstat(i32 %0, %struct.stat* %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %19, label %9
9: ; preds = %6
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @S_ISCHR(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = call i8* @ttynameCheckDir(%struct.stat* %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i8* @ttynameCheckDir(%struct.stat* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %14, %9, %6, %1
%20 = phi i8* [ null, %1 ], [ null, %6 ], [ null, %9 ], [ %18, %17 ], [ %15, %14 ]
ret i8* %20
}