#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;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned int *);
int snprintf(char *, size_t, char *, unsigned int);
int /*<<< orphan*/ stderr;
int my_ptsname_r(int ptm, char *pts_name, size_t size) {
  unsigned int pts_no;
  int end;
  if (ioctl(ptm, TIOCGPTN, &pts_no)) {
    fprintf(stderr, "Error %d: ioctl() failure\n", errno);
    return (-1);
  }
  end = snprintf(pts_name, size, "/dev/pts/%u", pts_no);
  pts_name[end] = '\0';
  return (0);
}


x86 assembly (clang, -O0):
<my_ptsname_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     118c <my_ptsname_r+0x5c>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
lea    0xe85(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <my_ptsname_r+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%ecx
lea    0xe7d(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_ptsname_r>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
je     1177 <my_ptsname_r+0x47>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
lea    0xe95(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1195 <my_ptsname_r+0x65>
mov    0x4(%rsp),%ecx
lea    0xe99(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <snprintf@plt>
cltq
movb   $0x0,(%rbx,%rax,1)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@TIOCGPTN = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Error %d: ioctl() failure\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"/dev/pts/%u\00"
define i32 @my_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 i32, i32* %5
%11 = load i32, i32* @TIOCGPTN
%12 = call i64 @ioctl(i32 %10, i32 %11, i32* %8)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 -1, i32* %4
br label %27
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i64, i64* %7
%21 = load i32, i32* %8
%22 = call i32 @snprintf(i8* %19, i64 %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 %22, i32* %9
%23 = load i8*, i8** %6
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
store i32 0, i32* %4
br label %27
27: ; preds = %18, %14
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@TIOCGPTN = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Error %d: ioctl() failure\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"/dev/pts/%u\00"
define i32 @my_ptsname_r(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* @TIOCGPTN
%7 = call i64 @ioctl(i32 %0, i32 %6, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %11)
br label %18
13: ; preds = %3
%14 = load i32, i32* %4
%15 = call i32 @snprintf(i8* %1, i64 %2, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %13, %9
%19 = phi i32 [ -1, %9 ], [ 0, %13 ]
ret i32 %19
}