#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 ERANGE;
int /*<<< orphan*/ TIOCGPTN;
int __syscall(int, int, int /*<<< orphan*/, int *);
size_t snprintf(char *, size_t, char *, int);
int __ptsname_r(int fd, char *buf, size_t len) {
  int pty, err;
  if (!buf)
    len = 0;
  if ((err = __syscall(54, fd, TIOCGPTN, &pty)))
    return -err;
  if (snprintf(buf, len, "/dev/pts/%d", pty) >= len)
    return ERANGE;
  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    1146 <__ptsname_r+0x26>
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%esi
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
mov    $0x36,%edi
lea    -0x1c(%rbp),%rcx
call   1040 <__syscall@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     1179 <__ptsname_r+0x59>
xor    %eax,%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <__ptsname_r+0x92>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
lea    0xe75(%rip),%rdx
call   1030 <snprintf@plt>
cmp    -0x18(%rbp),%rax
jb     11ab <__ptsname_r+0x8b>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b2 <__ptsname_r+0x92>
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
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%esi
test   %rbx,%rbx
cmove  %rbx,%r14
mov    0x2eae(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x36,%edi
call   1040 <__syscall@plt>
test   %eax,%eax
je     1153 <__ptsname_r+0x33>
neg    %eax
jmp    117c <__ptsname_r+0x5c>
mov    0x4(%rsp),%ecx
lea    0xea2(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r14,%rcx
jb     117c <__ptsname_r+0x5c>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@TIOCGPTN = global i32 0
@.str = [12 x i8] c"/dev/pts/%d\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 i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i64 0, i64* %7
br label %13
13: ; preds = %12, %3
%14 = load i32, i32* %5
%15 = load i32, i32* @TIOCGPTN
%16 = call i32 @__syscall(i32 54, i32 %14, i32 %15, i32* %8)
store i32 %16, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %9
%20 = sub nsw i32 0, %19
store i32 %20, i32* %4
br label %31
21: ; preds = %13
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = load i32, i32* %8
%25 = call i64 @snprintf(i8* %22, i64 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = load i64, i64* %7
%27 = icmp uge i64 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i32, i32* @ERANGE
store i32 %29, i32* %4
br label %31
30: ; preds = %21
store i32 0, i32* %4
br label %31
31: ; preds = %30, %28, %18
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@TIOCGPTN = global i32 0
@.str = [12 x i8] c"/dev/pts/%d\00"
@ERANGE = global i32 0
define i32 @__ptsname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i8* %1, null
%7 = select i1 %6, i64 0, i64 %2
%8 = load i32, i32* @TIOCGPTN
%9 = call i32 @__syscall(i32 54, i32 %0, i32 %8, i32* %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = sub nsw i32 0, %9
br label %19
13: ; preds = %3
%14 = load i32, i32* %4
%15 = call i64 @snprintf(i8* %1, i64 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp ult i64 %15, %7
%17 = load i32, i32* @ERANGE
%18 = select i1 %16, i32 0, i32 %17
br label %19
19: ; preds = %13, %11
%20 = phi i32 [ %12, %11 ], [ %18, %13 ]
ret i32 %20
}