#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 */
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ TIOCGPTN;
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned int *);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
char *ptsname(int fd) {
  unsigned int pty_num;
  static char buff[64];
  if (ioctl(fd, TIOCGPTN, &pty_num) != 0)
    return NULL;
  snprintf(buff, sizeof(buff), "/dev/pts/%u", pty_num);
  return buff;
}


x86 assembly (clang, -O0):
<ptsname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2eb3(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1157 <ptsname+0x37>
movq   $0x0,-0x8(%rbp)
jmp    117d <ptsname+0x5d>
mov    -0x10(%rbp),%ecx
lea    0x2edf(%rip),%rdi
mov    $0x40,%esi
lea    0xe93(%rip),%rdx
call   1030 <snprintf@plt>
lea    0x2ec7(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptsname>:
push   %rbx
sub    $0x10,%rsp
mov    0x2ebc(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
je     1141 <ptsname+0x21>
xor    %ebx,%ebx
jmp    1160 <ptsname+0x40>
mov    0xc(%rsp),%ecx
lea    0x2ef4(%rip),%rbx
lea    0xead(%rip),%rdx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1030 <snprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@ptsname.buff = internal global [64 x i8] zeroinitializer
@TIOCGPTN = global i32 0
@.str = [12 x i8] c"/dev/pts/%u\00"
define i8* @ptsname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @TIOCGPTN
%7 = call i64 @ioctl(i32 %5, i32 %6, i32* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %13
10: ; preds = %1
%11 = load i32, i32* %4
%12 = call i32 @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @ptsname.buff, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %11)
store i8* getelementptr inbounds ([64 x i8], [64 x i8]* @ptsname.buff, i64 0, i64 0), i8** %2
br label %13
13: ; preds = %10, %9
%14 = load i8*, i8** %2
ret i8* %14
}

LLVM IR (-O2):
@ptsname.buff = internal global [64 x i8] zeroinitializer
@TIOCGPTN = global i32 0
@.str = [12 x i8] c"/dev/pts/%u\00"
define i8* @ptsname(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @TIOCGPTN
%5 = call i64 @ioctl(i32 %0, i32 %4, i32* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* %2
%9 = call i32 @snprintf(i8* getelementptr inbounds ([64 x i8], [64 x i8]* @ptsname.buff, i64 0, i64 0), i32 64, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %1, %7
%11 = phi i8* [ getelementptr inbounds ([64 x i8], [64 x i8]* @ptsname.buff, i64 0, i64 0), %7 ], [ null, %1 ]
ret i8* %11
}