#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     1177 <__ptsname_r+0x57>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <__ptsname_r+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
lea    0xe77(%rip),%rdx
call   1030 <snprintf@plt>
cmp    -0x18(%rbp),%rax
jb     11a9 <__ptsname_r+0x89>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <__ptsname_r+0x90>
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
jne    1178 <__ptsname_r+0x58>
mov    0x4(%rsp),%ecx
lea    0xea6(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r14,%rcx
jb     1178 <__ptsname_r+0x58>
mov    0x2e6a(%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 %20
18: ; preds = %13
%19 = load i32, i32* %9
store i32 %19, i32* %4
br label %30
20: ; preds = %13
%21 = load i8*, i8** %6
%22 = load i64, i64* %7
%23 = load i32, i32* %8
%24 = call i64 @snprintf(i8* %21, i64 %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i64, i64* %7
%26 = icmp uge i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = load i32, i32* @ERANGE
store i32 %28, i32* %4
br label %30
29: ; preds = %20
store i32 0, i32* %4
br label %30
30: ; preds = %29, %27, %18
%31 = load i32, i32* %4
ret i32 %31
}

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 %11, label %17
11: ; preds = %3
%12 = load i32, i32* %4
%13 = call i64 @snprintf(i8* %1, i64 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp ult i64 %13, %7
%15 = load i32, i32* @ERANGE
%16 = select i1 %14, i32 0, i32 %15
br label %17
17: ; preds = %11, %3
%18 = phi i32 [ %9, %3 ], [ %16, %11 ]
ret i32 %18
}