#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 {
  scalar_t__ st_ino;
  scalar_t__ st_dev;
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  scalar_t__ d_ino;
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ isatty(int);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *ttyname(int fd) {
  struct stat fdstat, devstat;
  DIR *dp;
  struct dirent *dirp;
  static char pathname[256 + 1];
  char *rval;
  if (isatty(fd) == 0)
    return (NULL);
  if (fstat(fd, &fdstat) < 0)
    return (NULL);
  if (S_ISCHR(fdstat.st_mode) == 0)
    return (NULL);
  strcpy(pathname, "/dev/pts/");
  if ((dp = opendir("/dev/pts/")) == NULL)
    return (NULL);
  rval = NULL;
  while ((dirp = readdir(dp)) != NULL) {
    if (dirp->d_ino != fdstat.st_ino)
      continue;
    strncpy(pathname + sizeof("/dev/pts/") - 1, dirp->d_name,
            256 - sizeof("/dev/pts/") - 1);
    if (stat(pathname, &devstat) < 0)
      continue;
    if (devstat.st_ino == fdstat.st_ino && devstat.st_dev == fdstat.st_dev) {
      rval = pathname;
      break;
    }
  }
  closedir(dp);
  return (rval);
}


x86 assembly (clang, -O0):
<ttyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <isatty@plt>
cmp    $0x0,%rax
jne    11ba <ttyname+0x2a>
movq   $0x0,-0x8(%rbp)
jmp    12e8 <ttyname+0x158>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
jge    11dd <ttyname+0x4d>
movq   $0x0,-0x8(%rbp)
jmp    12e8 <ttyname+0x158>
mov    -0x18(%rbp),%edi
call   10a0 <S_ISCHR@plt>
cmp    $0x0,%rax
jne    11fc <ttyname+0x6c>
movq   $0x0,-0x8(%rbp)
jmp    12e8 <ttyname+0x158>
lea    0x2e7d(%rip),%rdi
lea    0xdf6(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xdea(%rip),%rdi
call   1060 <opendir@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    1236 <ttyname+0xa6>
movq   $0x0,-0x8(%rbp)
jmp    12e8 <ttyname+0x158>
movq   $0x0,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x50(%rbp)
cmp    $0x0,%rax
je     12d7 <ttyname+0x147>
mov    -0x50(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
je     126b <ttyname+0xdb>
jmp    123e <ttyname+0xae>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%esi
lea    0x2e07(%rip),%rdi
add    $0x9,%rdi
mov    $0xf5,%edx
call   1030 <strncpy@plt>
lea    0x2df2(%rip),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    12a6 <ttyname+0x116>
jmp    123e <ttyname+0xae>
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    12d2 <ttyname+0x142>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    12d2 <ttyname+0x142>
lea    0x2db7(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    12d7 <ttyname+0x147>
jmp    123e <ttyname+0xae>
mov    -0x48(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebx
call   1050 <isatty@plt>
test   %rax,%rax
je     1272 <ttyname+0xe2>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10b0 <fstat@plt>
test   %rax,%rax
js     1272 <ttyname+0xe2>
mov    0x18(%rsp),%edi
call   10a0 <S_ISCHR@plt>
test   %rax,%rax
je     1272 <ttyname+0xe2>
lea    0x2ea4(%rip),%r14
lea    0xe1d(%rip),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <opendir@plt>
test   %rax,%rax
je     1272 <ttyname+0xe2>
mov    %rax,%r13
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1287 <ttyname+0xf7>
mov    0x8(%rsp),%rbx
mov    0x10(%rsp),%rbp
lea    0x2e6d(%rip),%r15
lea    0x20(%rsp),%r12
jmp    123d <ttyname+0xad>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r13,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1287 <ttyname+0xf7>
cmp    %rbx,(%rax)
jne    1230 <ttyname+0xa0>
mov    0x8(%rax),%esi
mov    %r15,%rdi
mov    $0xf5,%edx
call   1030 <strncpy@plt>
mov    %r14,%rdi
mov    %r12,%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     1230 <ttyname+0xa0>
cmp    %rbx,0x20(%rsp)
jne    1230 <ttyname+0xa0>
cmp    %rbp,0x28(%rsp)
jne    1230 <ttyname+0xa0>
jmp    128a <ttyname+0xfa>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
mov    %r13,%rdi
call   1070 <closedir@plt>
jmp    1275 <ttyname+0xe5>

LLVM IR (-O0):
%struct.stat = type { i64, i64, i32 }
%struct.dirent = type { i64, i32 }
@ttyname.pathname = internal global [257 x i8] zeroinitializer
@.str = [10 x i8] c"/dev/pts/\00"
define i8* @ttyname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i8*
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = call i64 @isatty(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %68
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i64 @fstat(i32 %14, %struct.stat* %4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %13
store i8* null, i8** %2
br label %68
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 2
%20 = load i32, i32* %19
%21 = call i64 @S_ISCHR(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i8* null, i8** %2
br label %68
24: ; preds = %18
%25 = call i32 @strcpy(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%26 = call i32* @opendir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32* %26, i32** %6
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %24
store i8* null, i8** %2
br label %68
29: ; preds = %24
store i8* null, i8** %8
br label %30
30: ; preds = %63, %49, %41, %29
%31 = load i32*, i32** %6
%32 = call %struct.dirent* @readdir(i32* %31)
store %struct.dirent* %32, %struct.dirent** %7
%33 = icmp ne %struct.dirent* %32, null
br i1 %33, label %34, label %64
34: ; preds = %30
%35 = load %struct.dirent*, %struct.dirent** %7
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%39 = load i64, i64* %38
%40 = icmp ne i64 %37, %39
br i1 %40, label %41, label %42
41: ; preds = %34
br label %30
42: ; preds = %34
%43 = load %struct.dirent*, %struct.dirent** %7
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = call i32 @strncpy(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 9), i32 %45, i32 245)
%47 = call i64 @stat(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 0), %struct.stat* %5)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %42
br label %30
50: ; preds = %42
%51 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%52 = load i64, i64* %51
%53 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%54 = load i64, i64* %53
%55 = icmp eq i64 %52, %54
br i1 %55, label %56, label %63
56: ; preds = %50
%57 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 1
%58 = load i64, i64* %57
%59 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%60 = load i64, i64* %59
%61 = icmp eq i64 %58, %60
br i1 %61, label %62, label %63
62: ; preds = %56
store i8* getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 0), i8** %8
br label %64
63: ; preds = %56, %50
br label %30
64: ; preds = %62, %30
%65 = load i32*, i32** %6
%66 = call i32 @closedir(i32* %65)
%67 = load i8*, i8** %8
store i8* %67, i8** %2
br label %68
68: ; preds = %64, %28, %23, %17, %12
%69 = load i8*, i8** %2
ret i8* %69
}

LLVM IR (-O2):
%struct.stat = type { i64, i64, i32 }
%struct.dirent = type { i64, i32 }
@ttyname.pathname = internal global [257 x i8] zeroinitializer
@.str = [10 x i8] c"/dev/pts/\00"
define i8* @ttyname(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %2 to i8*
%5 = bitcast %struct.stat* %3 to i8*
%6 = tail call i64 @isatty(i32 %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %53, label %8
8: ; preds = %1
%9 = call i64 @fstat(i32 %0, %struct.stat* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %53, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%13 = load i32, i32* %12
%14 = tail call i64 @S_ISCHR(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %53, label %16
16: ; preds = %11
%17 = tail call i32 @strcpy(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32* @opendir(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %53, label %20
20: ; preds = %16
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%22 = call %struct.dirent* @readdir(i32* %18)
%23 = icmp eq %struct.dirent* %22, null
br i1 %23, label %50, label %24
24: ; preds = %20
%25 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%26 = load i64, i64* %25
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%29 = load i64, i64* %28
br label %30
30: ; preds = %24, %47
%31 = phi %struct.dirent* [ %22, %24 ], [ %48, %47 ]
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 0
%33 = load i64, i64* %32
%34 = icmp eq i64 %33, %26
br i1 %34, label %35, label %47
35: ; preds = %30
%36 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 1
%37 = load i32, i32* %36
%38 = call i32 @strncpy(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 9), i32 %37, i32 245)
%39 = call i64 @stat(i8* getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 0), %struct.stat* %3)
%40 = icmp slt i64 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %35
%42 = load i64, i64* %27
%43 = icmp eq i64 %42, %26
%44 = load i64, i64* %21
%45 = icmp eq i64 %44, %29
%46 = select i1 %43, i1 %45, i1 false
br i1 %46, label %50, label %47
47: ; preds = %41, %30, %35
%48 = call %struct.dirent* @readdir(i32* %18)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %30
50: ; preds = %47, %41, %20
%51 = phi i8* [ null, %20 ], [ getelementptr inbounds ([257 x i8], [257 x i8]* @ttyname.pathname, i64 0, i64 0), %41 ], [ null, %47 ]
%52 = call i32 @closedir(i32* %18)
br label %53
53: ; preds = %16, %11, %8, %1, %50
%54 = phi i8* [ %51, %50 ], [ null, %1 ], [ null, %8 ], [ null, %11 ], [ null, %16 ]
ret i8* %54
}