#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 /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENOTTY;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ TIOCGPTN;
int /*<<< orphan*/ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned int *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int snprintf(char *, int, char *, unsigned int);
int ptsname_r(int fd, char *buf, size_t buflen) {
  unsigned int pty_num;
  char buff[64];
  int len;
  if (buf == NULL) {
    errno = EINVAL;
    return -1;
  }
  if (ioctl(fd, TIOCGPTN, &pty_num) != 0) {
    errno = ENOTTY;
    return -1;
  }
  len = snprintf(buff, sizeof(buff), "/dev/pts/%u", pty_num);
  if (len + 1 > (int)buflen) {
    errno = ERANGE;
    return -1;
  }
  memcpy(buf, buff, len + 1);
  return 0;
}


x86 assembly (clang, -O0):
<ptsname_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    116c <ptsname_r+0x3c>
mov    0x2e6b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <ptsname_r+0xde>
mov    -0x8(%rbp),%edi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11a9 <ptsname_r+0x79>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <ptsname_r+0xde>
lea    -0x60(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x40,%esi
lea    0xe44(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jle    11f4 <ptsname_r+0xc4>
mov    0x2dfb(%rip),%rax
mov    (%rax),%ecx
mov    0x2de2(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    120e <ptsname_r+0xde>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptsname_r>:
push   %r14
push   %rbx
sub    $0x58,%rsp
test   %rsi,%rsi
je     1163 <ptsname_r+0x33>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2e9f(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
je     116c <ptsname_r+0x3c>
mov    0x2e7f(%rip),%rax
jmp    11a8 <ptsname_r+0x78>
mov    0x2e56(%rip),%rax
jmp    11a8 <ptsname_r+0x78>
mov    0xc(%rsp),%ecx
lea    0xe89(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
cmp    %r14d,%eax
jge    11a1 <ptsname_r+0x71>
add    $0x1,%eax
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    11b8 <ptsname_r+0x88>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    0x2e17(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@TIOCGPTN = global i32 0
@ENOTTY = global i32 0
@.str = [12 x i8] c"/dev/pts/%u\00"
@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 [64 x i8]
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
store i32 -1, i32* %4
br label %39
15: ; preds = %3
%16 = load i32, i32* %5
%17 = load i32, i32* @TIOCGPTN
%18 = call i64 @ioctl(i32 %16, i32 %17, i32* %8)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = load i32, i32* @ENOTTY
store i32 %21, i32* @errno
store i32 -1, i32* %4
br label %39
22: ; preds = %15
%23 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%24 = load i32, i32* %8
%25 = call i32 @snprintf(i8* %23, i32 64, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = add nsw i32 %26, 1
%28 = load i64, i64* %7
%29 = trunc i64 %28 to i32
%30 = icmp sgt i32 %27, %29
br i1 %30, label %31, label %33
31: ; preds = %22
%32 = load i32, i32* @ERANGE
store i32 %32, i32* @errno
store i32 -1, i32* %4
br label %39
33: ; preds = %22
%34 = load i8*, i8** %6
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
%38 = call i32 @memcpy(i8* %34, i8* %35, i32 %37)
store i32 0, i32* %4
br label %39
39: ; preds = %33, %31, %20, %13
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@TIOCGPTN = global i32 0
@ENOTTY = global i32 0
@.str = [12 x i8] c"/dev/pts/%u\00"
@ERANGE = global i32 0
define i32 @ptsname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = bitcast i32* %4 to i8*
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = icmp eq i8* %1, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
br label %27
11: ; preds = %3
%12 = load i32, i32* @TIOCGPTN
%13 = call i64 @ioctl(i32 %0, i32 %12, i32* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = load i32, i32* @ENOTTY
store i32 %16, i32* @errno
br label %27
17: ; preds = %11
%18 = load i32, i32* %4
%19 = call i32 @snprintf(i8* %7, i32 64, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = trunc i64 %2 to i32
%21 = icmp slt i32 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = load i32, i32* @ERANGE
store i32 %23, i32* @errno
br label %27
24: ; preds = %17
%25 = add nsw i32 %19, 1
%26 = call i32 @memcpy(i8* %1, i8* %7, i32 %25)
br label %27
27: ; preds = %24, %22, %15, %9
%28 = phi i32 [ -1, %9 ], [ -1, %15 ], [ -1, %22 ], [ 0, %24 ]
ret i32 %28
}