#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_dev;
  scalar_t__ st_ino;
};
typedef size_t ssize_t;

/* Variables and functions */
int ENODEV;
int ERANGE;
int /*<<< orphan*/ __procfdname(char *, int);
int errno;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ isatty(int);
size_t readlink(char *, char *, size_t);
scalar_t__ stat(char *, struct stat *);
int ttyname_r(int fd, char *name, size_t size) {
  struct stat st1, st2;
  char procname[sizeof "/proc/self/fd/" + 3 * sizeof(int) + 2];
  ssize_t l;
  if (!isatty(fd))
    return errno;
  __procfdname(procname, fd);
  l = readlink(procname, name, size);
  if (l < 0)
    return errno;
  else if (l == size)
    return ERANGE;
  name[l] = 0;
  if (stat(name, &st1) || fstat(fd, &st2))
    return errno;
  if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino)
    return ENODEV;
  return 0;
}


x86 assembly (clang, -O0):
<ttyname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%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    1185 <ttyname_r+0x35>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <ttyname_r+0x114>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1050 <__procfdname@plt>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <readlink@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jae    11c2 <ttyname_r+0x72>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <ttyname_r+0x114>
mov    -0x68(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    11e1 <ttyname_r+0x91>
mov    0x2e11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <ttyname_r+0x114>
jmp    11e6 <ttyname_r+0x96>
mov    -0x10(%rbp),%rax
mov    -0x68(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    121f <ttyname_r+0xcf>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
je     1230 <ttyname_r+0xe0>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <ttyname_r+0x114>
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    124c <ttyname_r+0xfc>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     125d <ttyname_r+0x10d>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1264 <ttyname_r+0x114>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyname_r>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
call   1030 <isatty@plt>
test   %eax,%eax
je     11bc <ttyname_r+0x6c>
lea    0x20(%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    1196 <ttyname_r+0x46>
mov    0x2e54(%rip),%rax
jmp    11c3 <ttyname_r+0x73>
movb   $0x0,(%r14,%rax,1)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    11bc <ttyname_r+0x6c>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1070 <fstat@plt>
test   %rax,%rax
je     11d0 <ttyname_r+0x80>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    11e9 <ttyname_r+0x99>
mov    0x18(%rsp),%rcx
xor    %eax,%eax
cmp    0x8(%rsp),%rcx
je     11c5 <ttyname_r+0x75>
mov    0x2de0(%rip),%rax
jmp    11c3 <ttyname_r+0x73>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@errno = global i32 0
@ERANGE = global i32 0
@ENODEV = 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 %struct.stat
%9 = alloca %struct.stat
%10 = alloca [29 x i8]
%11 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i32, i32* %5
%13 = call i32 @isatty(i32 %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = load i32, i32* @errno
store i32 %16, i32* %4
br label %64
17: ; preds = %3
%18 = getelementptr inbounds [29 x i8], [29 x i8]* %10, i64 0, i64 0
%19 = load i32, i32* %5
%20 = call i32 @__procfdname(i8* %18, i32 %19)
%21 = getelementptr inbounds [29 x i8], [29 x i8]* %10, i64 0, i64 0
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = call i64 @readlink(i8* %21, i8* %22, i64 %23)
store i64 %24, i64* %11
%25 = load i64, i64* %11
%26 = icmp ult i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %17
%28 = load i32, i32* @errno
store i32 %28, i32* %4
br label %64
29: ; preds = %17
%30 = load i64, i64* %11
%31 = load i64, i64* %7
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i32, i32* @ERANGE
store i32 %34, i32* %4
br label %64
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = load i8*, i8** %6
%38 = load i64, i64* %11
%39 = getelementptr inbounds i8, i8* %37, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %6
%41 = call i64 @stat(i8* %40, %struct.stat* %8)
%42 = icmp ne i64 %41, 0
br i1 %42, label %47, label %43
43: ; preds = %36
%44 = load i32, i32* %5
%45 = call i64 @fstat(i32 %44, %struct.stat* %9)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %43, %36
%48 = load i32, i32* @errno
store i32 %48, i32* %4
br label %64
49: ; preds = %43
%50 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%51 = load i64, i64* %50
%52 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%53 = load i64, i64* %52
%54 = icmp ne i64 %51, %53
br i1 %54, label %61, label %55
55: ; preds = %49
%56 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%57 = load i64, i64* %56
%58 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%59 = load i64, i64* %58
%60 = icmp ne i64 %57, %59
br i1 %60, label %61, label %63
61: ; preds = %55, %49
%62 = load i32, i32* @ENODEV
store i32 %62, i32* %4
br label %64
63: ; preds = %55
store i32 0, i32* %4
br label %64
64: ; preds = %63, %61, %47, %33, %27, %15
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@errno = global i32 0
@ERANGE = global i32 0
@ENODEV = global i32 0
define i32 @ttyname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = alloca [29 x i8]
%7 = bitcast %struct.stat* %4 to i8*
%8 = bitcast %struct.stat* %5 to i8*
%9 = getelementptr inbounds [29 x i8], [29 x i8]* %6, i64 0, i64 0
%10 = tail call i32 @isatty(i32 %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %3
%13 = load i32, i32* @errno
br label %43
14: ; preds = %3
%15 = call i32 @__procfdname(i8* %9, i32 %0)
%16 = call i64 @readlink(i8* %9, i8* %1, i64 %2)
%17 = icmp eq i64 %16, %2
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i32, i32* @ERANGE
br label %43
20: ; preds = %14
%21 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %21
%22 = call i64 @stat(i8* %1, %struct.stat* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i64 @fstat(i32 %0, %struct.stat* %5)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24, %20
%28 = load i32, i32* @errno
br label %43
29: ; preds = %24
%30 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%31 = load i64, i64* %30
%32 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%33 = load i64, i64* %32
%34 = icmp eq i64 %31, %33
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%37 = load i64, i64* %36
%38 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%39 = load i64, i64* %38
%40 = icmp eq i64 %37, %39
br i1 %40, label %43, label %41
41: ; preds = %35, %29
%42 = load i32, i32* @ENODEV
br label %43
43: ; preds = %35, %41, %27, %18, %12
%44 = phi i32 [ %19, %18 ], [ %28, %27 ], [ %42, %41 ], [ %13, %12 ], [ 0, %35 ]
ret i32 %44
}