#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 {
  int dummy;
};
typedef int /*<<< orphan*/ _PATH_DEV;

/* Variables and functions */
int EINVAL;
int ERANGE;
int /*<<< orphan*/ _PATH_TTY;
int /*<<< orphan*/ __isatty(int);
int /*<<< orphan*/ __set_errno(int);
scalar_t__ __stat(char *, struct stat *);
int __ttyname_r(int, char *, size_t);
int errno;
int strlen(int /*<<< orphan*/);
int __ptsname_r(int fd, char *buf, size_t buflen) {
  int save_errno = errno;
  int err;
  struct stat st;
  if (buf == NULL) {
    __set_errno(EINVAL);
    return EINVAL;
  }
  if (!__isatty(fd))
    return errno;
  if (buflen < strlen(_PATH_TTY) + 3) {
    __set_errno(ERANGE);
    return ERANGE;
  }
  err = __ttyname_r(fd, buf, buflen);
  if (err != 0) {
    __set_errno(err);
    return errno;
  }
  buf[sizeof(_PATH_DEV) - 1] = 't';
  if (__stat(buf, &st) < 0)
    return errno;
  __set_errno(save_errno);
  return 0;
}


x86 assembly (clang, -O0):
<__ptsname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1199 <__ptsname_r+0x49>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <__ptsname_r+0x12a>
mov    -0x8(%rbp),%edi
call   1060 <__isatty@plt>
cmp    $0x0,%eax
jne    11bb <__ptsname_r+0x6b>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <__ptsname_r+0x12a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
add    $0x3,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    1205 <__ptsname_r+0xb5>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <__ptsname_r+0x12a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <__ttyname_r@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     123b <__ptsname_r+0xeb>
mov    -0x20(%rbp),%edi
call   1030 <__set_errno@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <__ptsname_r+0x12a>
mov    -0x10(%rbp),%rax
movb   $0x74,0x3(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <__stat@plt>
cmp    $0x0,%rax
jge    126b <__ptsname_r+0x11b>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <__ptsname_r+0x12a>
mov    -0x1c(%rbp),%edi
call   1030 <__set_errno@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__ptsname_r>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     119e <__ptsname_r+0x4e>
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e68(%rip),%r12
mov    (%r12),%r14d
call   1060 <__isatty@plt>
test   %eax,%eax
je     11c8 <__ptsname_r+0x78>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
add    $0x3,%eax
cltq
cmp    %r15,%rax
jbe    11b0 <__ptsname_r+0x60>
mov    0x2e4c(%rip),%rbx
jmp    11a5 <__ptsname_r+0x55>
mov    0x2e2b(%rip),%rbx
mov    (%rbx),%edi
call   1030 <__set_errno@plt>
mov    (%rbx),%eax
jmp    11cc <__ptsname_r+0x7c>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <__ttyname_r@plt>
test   %eax,%eax
je     11d9 <__ptsname_r+0x89>
mov    %eax,%edi
call   1030 <__set_errno@plt>
mov    (%r12),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x74,0x3(%rbx)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1040 <__stat@plt>
test   %rax,%rax
js     11c8 <__ptsname_r+0x78>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
xor    %eax,%eax
jmp    11cc <__ptsname_r+0x7c>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@EINVAL = global i32 0
@_PATH_TTY = global i32 0
@ERANGE = global i32 0
define i32 @__ptsname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32, i32* @errno
store i32 %11, i32* %8
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @EINVAL
%16 = call i32 @__set_errno(i32 %15)
%17 = load i32, i32* @EINVAL
store i32 %17, i32* %4
br label %57
18: ; preds = %3
%19 = load i32, i32* %5
%20 = call i32 @__isatty(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = load i32, i32* @errno
store i32 %23, i32* %4
br label %57
24: ; preds = %18
%25 = load i64, i64* %7
%26 = load i32, i32* @_PATH_TTY
%27 = call i32 @strlen(i32 %26)
%28 = add nsw i32 %27, 3
%29 = sext i32 %28 to i64
%30 = icmp ult i64 %25, %29
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i32, i32* @ERANGE
%33 = call i32 @__set_errno(i32 %32)
%34 = load i32, i32* @ERANGE
store i32 %34, i32* %4
br label %57
35: ; preds = %24
%36 = load i32, i32* %5
%37 = load i8*, i8** %6
%38 = load i64, i64* %7
%39 = call i32 @__ttyname_r(i32 %36, i8* %37, i64 %38)
store i32 %39, i32* %9
%40 = load i32, i32* %9
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %35
%43 = load i32, i32* %9
%44 = call i32 @__set_errno(i32 %43)
%45 = load i32, i32* @errno
store i32 %45, i32* %4
br label %57
46: ; preds = %35
%47 = load i8*, i8** %6
%48 = getelementptr inbounds i8, i8* %47, i64 3
store i8 116, i8* %48
%49 = load i8*, i8** %6
%50 = call i64 @__stat(i8* %49, %struct.stat* %10)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = load i32, i32* @errno
store i32 %53, i32* %4
br label %57
54: ; preds = %46
%55 = load i32, i32* %8
%56 = call i32 @__set_errno(i32 %55)
store i32 0, i32* %4
br label %57
57: ; preds = %54, %52, %42, %31, %22, %14
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@EINVAL = global i32 0
@_PATH_TTY = global i32 0
@ERANGE = global i32 0
define i32 @__ptsname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.stat
%5 = load i32, i32* @errno
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i8* %1, null
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = load i32, i32* @EINVAL
%10 = tail call i32 @__set_errno(i32 %9)
%11 = load i32, i32* @EINVAL
br label %41
12: ; preds = %3
%13 = tail call i32 @__isatty(i32 %0)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i32, i32* @errno
br label %41
17: ; preds = %12
%18 = load i32, i32* @_PATH_TTY
%19 = tail call i32 @strlen(i32 %18)
%20 = add nsw i32 %19, 3
%21 = sext i32 %20 to i64
%22 = icmp ugt i64 %21, %2
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i32, i32* @ERANGE
%25 = tail call i32 @__set_errno(i32 %24)
%26 = load i32, i32* @ERANGE
br label %41
27: ; preds = %17
%28 = tail call i32 @__ttyname_r(i32 %0, i8* %1, i64 %2)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = tail call i32 @__set_errno(i32 %28)
%32 = load i32, i32* @errno
br label %41
33: ; preds = %27
%34 = getelementptr inbounds i8, i8* %1, i64 3
store i8 116, i8* %34
%35 = call i64 @__stat(i8* %1, %struct.stat* %4)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = load i32, i32* @errno
br label %41
39: ; preds = %33
%40 = call i32 @__set_errno(i32 %5)
br label %41
41: ; preds = %39, %37, %30, %23, %15, %8
%42 = phi i32 [ %11, %8 ], [ %26, %23 ], [ %32, %30 ], [ %38, %37 ], [ 0, %39 ], [ %16, %15 ]
ret i32 %42
}