#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 */
typedef int /*<<< orphan*/ obuf;

/* Variables and functions */
int __ltostr(char *, int, unsigned long, int, int /*<<< orphan*/);
int /*<<< orphan*/ isatty(int);
int readlink(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
char *ttyname(int fd) {
  char ibuf[20];
  static char obuf[20];
  int len;
  if (!isatty(fd))
    return 0;
  strcpy(ibuf, "/proc/self/fd/");
  ibuf[__ltostr(ibuf + 14, 6, (unsigned long)fd, 10, 0) + 14] = 0;
  if ((len = readlink(ibuf, obuf, sizeof(obuf) - 1)) < 0)
    return 0;
  obuf[len] = 0;
  return obuf;
}


x86 assembly (clang, -O0):
<ttyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <isatty@plt>
cmp    $0x0,%eax
jne    1169 <ttyname+0x29>
movq   $0x0,-0x8(%rbp)
jmp    11e9 <ttyname+0xa9>
lea    -0x20(%rbp),%rdi
lea    0xe8c(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x20(%rbp),%rdi
add    $0xe,%rdi
movslq -0xc(%rbp),%rdx
mov    $0x6,%esi
mov    $0xa,%ecx
xor    %r8d,%r8d
call   1060 <__ltostr@plt>
add    $0xe,%eax
cltq
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rdi
lea    0x2ea4(%rip),%rsi
mov    $0x13,%edx
call   1050 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11cf <ttyname+0x8f>
movq   $0x0,-0x8(%rbp)
jmp    11e9 <ttyname+0xa9>
movslq -0x24(%rbp),%rcx
lea    0x2e76(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e6b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyname>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
call   1040 <isatty@plt>
test   %eax,%eax
je     11a9 <ttyname+0x69>
lea    0xea7(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0xe(%rsp),%rdi
movslq %ebx,%rdx
mov    $0x6,%esi
mov    $0xa,%ecx
xor    %r8d,%r8d
call   1060 <__ltostr@plt>
cltq
movb   $0x0,0xe(%rax,%r14,1)
lea    0x2ec3(%rip),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x13,%edx
call   1050 <readlink@plt>
test   %eax,%eax
js     11a9 <ttyname+0x69>
mov    %eax,%eax
movb   $0x0,(%rax,%rbx,1)
jmp    11ab <ttyname+0x6b>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@ttyname.obuf = internal global [20 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/self/fd/\00"
define i8* @ttyname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca i32
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 %29
10: ; preds = %1
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%12 = call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds i8, i8* %13, i64 14
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = call i32 @__ltostr(i8* %14, i32 6, i64 %16, i32 10, i32 0)
%18 = add nsw i32 %17, 14
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %19
store i8 0, i8* %20
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%22 = call i32 @readlink(i8* %21, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @ttyname.obuf, i64 0, i64 0), i32 19)
store i32 %22, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %10
store i8* null, i8** %2
br label %29
25: ; preds = %10
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [20 x i8], [20 x i8]* @ttyname.obuf, i64 0, i64 %27
store i8 0, i8* %28
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @ttyname.obuf, i64 0, i64 0), i8** %2
br label %29
29: ; preds = %25, %24, %9
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
@ttyname.obuf = internal global [20 x i8] zeroinitializer
@.str = [15 x i8] c"/proc/self/fd/\00"
define i8* @ttyname(i32 %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%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 @strcpy(i8* %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 14
%9 = sext i32 %0 to i64
%10 = call i32 @__ltostr(i8* %8, i32 6, i64 %9, i32 10, i32 0)
%11 = add nsw i32 %10, 14
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @readlink(i8* %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @ttyname.obuf, i64 0, i64 0), i32 19)
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %6
%17 = zext i32 %14 to i64
%18 = getelementptr inbounds [20 x i8], [20 x i8]* @ttyname.obuf, i64 0, i64 %17
store i8 0, i8* %18
br label %19
19: ; preds = %6, %1, %16
%20 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @ttyname.obuf, i64 0, i64 0), %16 ], [ null, %1 ], [ null, %6 ]
ret i8* %20
}