#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 size_t ssize_t;

/* Variables and functions */
int ENOTTY;
int ERANGE;
int /*<<< orphan*/ __procfdname(char *, int);
int errno;
int /*<<< orphan*/ isatty(int);
size_t readlink(char *, char *, size_t);
int ttyname_r(int fd, char *name, size_t size) {
  char procname[sizeof "/proc/self/fd/" + 3 * sizeof(int) + 2];
  ssize_t l;
  if (!isatty(fd))
    return ENOTTY;
  __procfdname(procname, fd);
  l = readlink(procname, name, size);
  if (l < 0)
    return errno;
  else if (l == size)
    return ERANGE;
  else {
    name[l] = 0;
    return 0;
  }
}


x86 assembly (clang, -O0):
<ttyname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1165 <ttyname_r+0x35>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <ttyname_r+0xa4>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1050 <__procfdname@plt>
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <readlink@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jae    11a2 <ttyname_r+0x72>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <ttyname_r+0xa4>
mov    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11c1 <ttyname_r+0x91>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <ttyname_r+0xa4>
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyname_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
call   1030 <isatty@plt>
test   %eax,%eax
je     1176 <ttyname_r+0x46>
mov    %rsp,%r15
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <__procfdname@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
cmp    %rbx,%rax
jne    1181 <ttyname_r+0x51>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
jmp    1188 <ttyname_r+0x58>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
jmp    1188 <ttyname_r+0x58>
movb   $0x0,(%r14,%rax,1)
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOTTY = global i32 0
@errno = global i32 0
@ERANGE = global i32 0
define i32 @ttyname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [29 x i8]
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i32, i32* %5
%11 = call i32 @isatty(i32 %10)
%12 = icmp ne i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %3
%14 = load i32, i32* @ENOTTY
store i32 %14, i32* %4
br label %37
15: ; preds = %3
%16 = getelementptr inbounds [29 x i8], [29 x i8]* %8, i64 0, i64 0
%17 = load i32, i32* %5
%18 = call i32 @__procfdname(i8* %16, i32 %17)
%19 = getelementptr inbounds [29 x i8], [29 x i8]* %8, i64 0, i64 0
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = call i64 @readlink(i8* %19, i8* %20, i64 %21)
store i64 %22, i64* %9
%23 = load i64, i64* %9
%24 = icmp ult i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = load i32, i32* @errno
store i32 %26, i32* %4
br label %37
27: ; preds = %15
%28 = load i64, i64* %9
%29 = load i64, i64* %7
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i32, i32* @ERANGE
store i32 %32, i32* %4
br label %37
33: ; preds = %27
%34 = load i8*, i8** %6
%35 = load i64, i64* %9
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 0, i8* %36
store i32 0, i32* %4
br label %37
37: ; preds = %33, %31, %25, %13
%38 = load i32, i32* %4
ret i32 %38
}

LLVM IR (-O2):
@ENOTTY = global i32 0
@errno = global i32 0
@ERANGE = global i32 0
define i32 @ttyname_r(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca [29 x i8]
%5 = getelementptr inbounds [29 x i8], [29 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @isatty(i32 %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = load i32, i32* @ENOTTY
br label %18
10: ; preds = %3
%11 = call i32 @__procfdname(i8* %5, i32 %0)
%12 = call i64 @readlink(i8* %5, i8* %1, i64 %2)
%13 = icmp eq i64 %12, %2
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i32, i32* @ERANGE
br label %18
16: ; preds = %10
%17 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 0, i8* %17
br label %18
18: ; preds = %16, %14, %8
%19 = phi i32 [ %15, %14 ], [ 0, %16 ], [ %9, %8 ]
ret i32 %19
}