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

/* Variables and functions */
int EINVAL;
int ERANGE;
int errno;
int /*<<< orphan*/ isatty(int);
int readlink(char *, char *, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int bb_ttyname_r(int fd, char *buf, size_t len) {
  if (buf == NULL) {
    errno = EINVAL;
    return errno;
  }
  if (!isatty(fd)) {
    return errno;
  }
  char path[64];
  snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
  ssize_t count = readlink(path, buf, len);
  if (count == -1) {
    return errno;
  }
  if ((size_t)(count) == len) {
    errno = ERANGE;
    return errno;
  }
  buf[count] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<bb_ttyname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1171 <bb_ttyname_r+0x41>
mov    0x2e7b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7a(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <bb_ttyname_r+0xee>
mov    -0x8(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1193 <bb_ttyname_r+0x63>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <bb_ttyname_r+0xee>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x40,%esi
lea    0xe5a(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <readlink@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    11da <bb_ttyname_r+0xaa>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <bb_ttyname_r+0xee>
movslq -0x64(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    120b <bb_ttyname_r+0xdb>
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    0x2de0(%rip),%rax
mov    %ecx,(%rax)
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    121e <bb_ttyname_r+0xee>
mov    -0x10(%rbp),%rax
movslq -0x64(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bb_ttyname_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
test   %rsi,%rsi
je     118c <bb_ttyname_r+0x5c>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
call   1030 <isatty@plt>
test   %eax,%eax
je     11a0 <bb_ttyname_r+0x70>
lea    0xea9(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x40,%esi
mov    %ebp,%ecx
call   1050 <snprintf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     11a0 <bb_ttyname_r+0x70>
cltq
cmp    %r14,%rax
jne    11ab <bb_ttyname_r+0x7b>
mov    0x2e5e(%rip),%rax
jmp    1193 <bb_ttyname_r+0x63>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    0x2e3c(%rip),%rcx
mov    %eax,(%rcx)
jmp    11b1 <bb_ttyname_r+0x81>
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
jmp    11b1 <bb_ttyname_r+0x81>
movb   $0x0,(%rbx,%rax,1)
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@ERANGE = global i32 0
define i32 @bb_ttyname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [64 x i8]
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @EINVAL
store i32 %13, i32* @errno
%14 = load i32, i32* @errno
store i32 %14, i32* %4
br label %46
15: ; preds = %3
%16 = load i32, i32* %5
%17 = call i32 @isatty(i32 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = load i32, i32* @errno
store i32 %20, i32* %4
br label %46
21: ; preds = %15
%22 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%23 = load i32, i32* %5
%24 = call i32 @snprintf(i8* %22, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %8, i64 0, i64 0
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = call i32 @readlink(i8* %25, i8* %26, i64 %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = load i32, i32* @errno
store i32 %32, i32* %4
br label %46
33: ; preds = %21
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = load i64, i64* %7
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* @ERANGE
store i32 %39, i32* @errno
%40 = load i32, i32* @errno
store i32 %40, i32* %4
br label %46
41: ; preds = %33
%42 = load i8*, i8** %6
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
store i32 0, i32* %4
br label %46
46: ; preds = %41, %38, %31, %19, %12
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@ERANGE = global i32 0
define i32 @bb_ttyname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca [64 x i8]
%5 = icmp eq i8* %1, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %29
8: ; preds = %3
%9 = tail call i32 @isatty(i32 %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = load i32, i32* @errno
br label %29
13: ; preds = %8
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%15 = call i32 @snprintf(i8* %14, i32 64, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%16 = call i32 @readlink(i8* %14, i8* %1, i64 %2)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32, i32* @errno
br label %27
20: ; preds = %13
%21 = sext i32 %16 to i64
%22 = icmp eq i64 %21, %2
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = load i32, i32* @ERANGE
store i32 %24, i32* @errno
br label %27
25: ; preds = %20
%26 = getelementptr inbounds i8, i8* %1, i64 %21
store i8 0, i8* %26
br label %27
27: ; preds = %25, %23, %18
%28 = phi i32 [ %19, %18 ], [ %24, %23 ], [ 0, %25 ]
br label %29
29: ; preds = %27, %11, %6
%30 = phi i32 [ %7, %6 ], [ %28, %27 ], [ %12, %11 ]
ret i32 %30
}