#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 */

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


x86 assembly (clang, -O0):
<ptsname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1161 <ptsname_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    11f0 <ptsname_r+0xd0>
mov    -0x8(%rbp),%edi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11a3 <ptsname_r+0x83>
mov    0x2e59(%rip),%rax
mov    (%rax),%ecx
mov    0x2e38(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <ptsname_r+0xd0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
lea    0xe4b(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jl     11e9 <ptsname_r+0xc9>
mov    0x2e0b(%rip),%rax
mov    (%rax),%ecx
mov    0x2df2(%rip),%rax
mov    %ecx,(%rax)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <ptsname_r+0xd0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptsname_r>:
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1150 <ptsname_r+0x30>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2eb2(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
je     1159 <ptsname_r+0x39>
mov    0x2e92(%rip),%rax
jmp    117f <ptsname_r+0x5f>
mov    0x2e69(%rip),%rax
jmp    117f <ptsname_r+0x5f>
mov    0x4(%rsp),%ecx
lea    0xe9c(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %r14d,%ecx
jl     118a <ptsname_r+0x6a>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    0x2e40(%rip),%rcx
mov    %eax,(%rcx)
add    $0x8,%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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
%13 = load i32, i32* @errno
store i32 %13, i32* %4
br label %34
14: ; preds = %3
%15 = load i32, i32* %5
%16 = load i32, i32* @TIOCGPTN
%17 = call i64 @ioctl(i32 %15, i32 %16, i32* %8)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @ENOTTY
store i32 %20, i32* @errno
%21 = load i32, i32* @errno
store i32 %21, i32* %4
br label %34
22: ; preds = %14
%23 = load i8*, i8** %6
%24 = load i64, i64* %7
%25 = load i32, i32* %8
%26 = call i32 @snprintf(i8* %23, i64 %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i64, i64* %7
%28 = trunc i64 %27 to i32
%29 = icmp sge i32 %26, %28
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = load i32, i32* @ERANGE
store i32 %31, i32* @errno
%32 = load i32, i32* @errno
store i32 %32, i32* %4
br label %34
33: ; preds = %22
store i32 0, i32* %4
br label %34
34: ; preds = %33, %30, %19, %11
%35 = load i32, i32* %4
ret i32 %35
}

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 = 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 %23
8: ; preds = %3
%9 = bitcast i32* %4 to i8*
%10 = load i32, i32* @TIOCGPTN
%11 = call i64 @ioctl(i32 %0, i32 %10, i32* %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* %4
%15 = call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = trunc i64 %2 to i32
%17 = icmp slt i32 %15, %16
br i1 %17, label %21, label %18
18: ; preds = %13, %8
%19 = phi i32* [ @ENOTTY, %8 ], [ @ERANGE, %13 ]
%20 = load i32, i32* %19
store i32 %20, i32* @errno
br label %21
21: ; preds = %18, %13
%22 = phi i32 [ 0, %13 ], [ %20, %18 ]
br label %23
23: ; preds = %21, %6
%24 = phi i32 [ %7, %6 ], [ %22, %21 ]
ret i32 %24
}