#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 /*<<< orphan*/ __ltostr(char *, int, int, int, int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ strcpy(char *, char *);
char *ptsname(int fd) {
  static char buffer[9 + 4];
  int pty;
  strcpy(buffer, "/dev/pts/");
  if ((ioctl(fd, TIOCGPTN, &pty)) == -1)
    return 0;
  __ltostr(buffer + 9, 4, pty, 10, 0);
  return buffer;
}


x86 assembly (clang, -O0):
<ptsname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
lea    0x2efa(%rip),%rdi
lea    0xeb7(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1179 <ptsname+0x49>
movq   $0x0,-0x8(%rbp)
jmp    11a4 <ptsname+0x74>
mov    -0x10(%rbp),%edx
lea    0x2eb9(%rip),%rdi
add    $0x9,%rdi
mov    $0x4,%esi
mov    $0xa,%ecx
xor    %r8d,%r8d
call   1050 <__ltostr@plt>
lea    0x2e9c(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptsname>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0x2f00(%rip),%rbx
lea    0xebd(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1184 <ptsname+0x54>
mov    0x4(%rsp),%edx
lea    0x2ed5(%rip),%rdi
mov    $0x4,%esi
mov    $0xa,%ecx
xor    %r8d,%r8d
call   1050 <__ltostr@plt>
jmp    1186 <ptsname+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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