#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 /*<<< orphan*/ TIOCGPTN;
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
size_t snprintf(char *, size_t, char *, int);
int __ptsname_r(int fd, char *buf, size_t len) {
  int pty;
  if (!buf)
    len = 0;
  return -(ioctl(fd, TIOCGPTN, &pty) < 0 ||
           snprintf(buf, len, "/dev/pts/%d", pty) >= len);
}


x86 assembly (clang, -O0):
<__ptsname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1146 <__ptsname_r+0x26>
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x1d(%rbp)
jl     118e <__ptsname_r+0x6e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
lea    0xe81(%rip),%rdx
call   1030 <snprintf@plt>
cmp    -0x18(%rbp),%rax
setae  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
and    $0x1,%al
movzbl %al,%ecx
xor    %eax,%eax
sub    %ecx,%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
test   %rsi,%rsi
cmove  %rsi,%r14
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     116c <__ptsname_r+0x4c>
mov    0x4(%rsp),%ecx
lea    0xeac(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r14,%rcx
adc    $0xffffffff,%eax
jmp    1171 <__ptsname_r+0x51>
mov    $0xffffffff,%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"
define i32 @__ptsname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %3
store i64 0, i64* %6
br label %11
11: ; preds = %10, %3
%12 = load i32, i32* %4
%13 = load i32, i32* @TIOCGPTN
%14 = call i64 @ioctl(i32 %12, i32 %13, i32* %7)
%15 = icmp slt i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %11
%17 = load i8*, i8** %5
%18 = load i64, i64* %6
%19 = load i32, i32* %7
%20 = call i64 @snprintf(i8* %17, i64 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i64, i64* %6
%22 = icmp uge i64 %20, %21
br label %23
23: ; preds = %16, %11
%24 = phi i1 [ true, %11 ], [ %22, %16 ]
%25 = zext i1 %24 to i32
%26 = sub nsw i32 0, %25
ret i32 %26
}

LLVM IR (-O2):
@TIOCGPTN = global i32 0
@.str = [12 x i8] c"/dev/pts/%d\00"
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 i64 @ioctl(i32 %0, i32 %8, i32* %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %16, label %11
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 uge i64 %13, %7
%15 = sext i1 %14 to i32
br label %16
16: ; preds = %11, %3
%17 = phi i32 [ -1, %3 ], [ %15, %11 ]
ret i32 %17
}