#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 errno;
int /*<<< orphan*/ isatty(int);
size_t readlink(char *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
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;
  snprintf(procname, sizeof procname, "/proc/self/fd/%d", fd);
  l = readlink(procname, name, size);
  if (l < 0)
    return errno;
  else if (l == size)
    return ERANGE;
  else
    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),%ecx
mov    $0x1d,%esi
lea    0xe88(%rip),%rdx
call   1050 <snprintf@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    11ae <ttyname_r+0x7e>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <ttyname_r+0xa4>
mov    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11cd <ttyname_r+0x9d>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11d4 <ttyname_r+0xa4>
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     1185 <ttyname_r+0x55>
lea    0xeae(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x1d,%esi
mov    %ebp,%ecx
call   1050 <snprintf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rbx,%rcx
jne    118e <ttyname_r+0x5e>
mov    0x2e5d(%rip),%rax
jmp    118c <ttyname_r+0x5c>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOTTY = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@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 %34
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 @snprintf(i8* %16, i32 29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), 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 %34
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 %34
33: ; preds = %27
store i32 0, i32* %4
br label %34
34: ; preds = %33, %31, %25, %13
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@ENOTTY = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@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 %16
10: ; preds = %3
%11 = call i32 @snprintf(i8* %5, i32 29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = call i64 @readlink(i8* %5, i8* %1, i64 %2)
%13 = icmp eq i64 %12, %2
%14 = load i32, i32* @ERANGE
%15 = select i1 %13, i32 %14, i32 0
br label %16
16: ; preds = %10, %8
%17 = phi i32 [ %9, %8 ], [ %15, %10 ]
ret i32 %17
}