#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 *);
scalar_t__ __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;
  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;
  }
  if (__ttyname_r(fd, buf, buflen) != 0)
    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    126f <__ptsname_r+0x11f>
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    126f <__ptsname_r+0x11f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%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    126f <__ptsname_r+0x11f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <__ttyname_r@plt>
cmp    $0x0,%rax
je     1230 <__ptsname_r+0xe0>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <__ptsname_r+0x11f>
mov    -0x10(%rbp),%rax
movb   $0x74,0x3(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <__stat@plt>
cmp    $0x0,%rax
jge    1260 <__ptsname_r+0x110>
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <__ptsname_r+0x11f>
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     11c2 <__ptsname_r+0x72>
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    11c6 <__ptsname_r+0x76>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <__ttyname_r@plt>
test   %rax,%rax
je     11d3 <__ptsname_r+0x83>
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     11c2 <__ptsname_r+0x72>
mov    %r14d,%edi
call   1030 <__set_errno@plt>
xor    %eax,%eax
jmp    11c6 <__ptsname_r+0x76>

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

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 %40
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 %40
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 %40
27: ; preds = %17
%28 = tail call i64 @__ttyname_r(i32 %0, i8* %1, i64 %2)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = load i32, i32* @errno
br label %40
32: ; preds = %27
%33 = getelementptr inbounds i8, i8* %1, i64 3
store i8 116, i8* %33
%34 = call i64 @__stat(i8* %1, %struct.stat* %4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = load i32, i32* @errno
br label %40
38: ; preds = %32
%39 = call i32 @__set_errno(i32 %5)
br label %40
40: ; preds = %38, %36, %30, %23, %15, %8
%41 = phi i32 [ %11, %8 ], [ %26, %23 ], [ %31, %30 ], [ %37, %36 ], [ 0, %38 ], [ %16, %15 ]
ret i32 %41
}