#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 */
struct stat64 {
  int dummy;
};

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ ENOMEM;
int ENOTTY;
int __ptsname_internal(int, char *, size_t, struct stat64 *);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
scalar_t__ memchr(char *, char, size_t);
scalar_t__ realloc(char *, size_t);
int pts_name(int fd, char **pts, size_t buf_len, struct stat64 *stp) {
  int rv;
  char *buf = *pts;
  for (;;) {
    char *new_buf;
    if (buf_len) {
      rv = __ptsname_internal(fd, buf, buf_len, stp);
      if (rv != 0) {
        if (rv == ENOTTY)
          rv = EINVAL;
        errno = rv;
        break;
      }
      if (memchr(buf, '\0', buf_len))
        break;
      buf_len += buf_len;
    } else
      buf_len = 128;
    if (buf != *pts)
      new_buf = (char *)realloc(buf, buf_len);
    else
      new_buf = (char *)malloc(buf_len);
    if (!new_buf) {
      rv = -1;
      __set_errno(ENOMEM);
      break;
    }
    buf = new_buf;
  }
  if (rv == 0)
    *pts = buf;
  else if (buf != *pts)
    free(buf);
  return rv;
}


x86 assembly (clang, -O0):
<pts_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     120c <pts_name+0xac>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1050 <__ptsname_internal@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11dd <pts_name+0x7d>
mov    -0x24(%rbp),%eax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%eax
jne    11cc <pts_name+0x6c>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    0x2dfa(%rip),%rax
mov    %ecx,(%rax)
jmp    127a <pts_name+0x11a>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1060 <memchr@plt>
cmp    $0x0,%rax
je     11fb <pts_name+0x9b>
jmp    127a <pts_name+0x11a>
mov    -0x18(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1214 <pts_name+0xb4>
movq   $0x80,-0x18(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
je     123b <pts_name+0xdb>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x38(%rbp)
jmp    1248 <pts_name+0xe8>
mov    -0x18(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    126d <pts_name+0x10d>
movl   $0xffffffff,-0x24(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    127a <pts_name+0x11a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1182 <pts_name+0x22>
cmpl   $0x0,-0x24(%rbp)
jne    1294 <pts_name+0x134>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    12b3 <pts_name+0x153>
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
je     12ae <pts_name+0x14e>
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
jmp    12b3 <pts_name+0x153>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pts_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r13d
mov    (%rsi),%r12
test   %rbx,%rbx
je     11d0 <pts_name+0x70>
nop
mov    %r13d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1050 <__ptsname_internal@plt>
test   %eax,%eax
jne    1206 <pts_name+0xa6>
mov    %r12,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1060 <memchr@plt>
test   %rax,%rax
jne    1238 <pts_name+0xd8>
add    %rbx,%rbx
cmp    (%r14),%r12
je     11da <pts_name+0x7a>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
test   %rax,%rax
jne    11e7 <pts_name+0x87>
jmp    11f1 <pts_name+0x91>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x80,%ebx
cmp    (%r14),%r12
jne    11b3 <pts_name+0x53>
mov    %rbx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11f1 <pts_name+0x91>
mov    %rax,%r12
test   %rbx,%rbx
jne    1180 <pts_name+0x20>
jmp    11d0 <pts_name+0x70>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%ebp
jmp    1229 <pts_name+0xc9>
mov    %eax,%ebp
mov    0x2dd9(%rip),%rax
cmp    (%rax),%ebp
jne    121c <pts_name+0xbc>
mov    0x2dae(%rip),%rax
mov    (%rax),%ebp
mov    0x2dad(%rip),%rax
mov    %ebp,(%rax)
test   %ebp,%ebp
je     1238 <pts_name+0xd8>
cmp    (%r14),%r12
je     123d <pts_name+0xdd>
mov    %r12,%rdi
call   1040 <free@plt>
jmp    123d <pts_name+0xdd>
mov    %r12,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat64 = type { i32 }
@ENOTTY = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @pts_name(i32 %0, i8** %1, i64 %2, %struct.stat64* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64
%8 = alloca %struct.stat64*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i64 %2, i64* %7
store %struct.stat64* %3, %struct.stat64** %8
%12 = load i8**, i8*** %6
%13 = load i8*, i8** %12
store i8* %13, i8** %10
br label %14
14: ; preds = %64, %4
%15 = load i64, i64* %7
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %43
17: ; preds = %14
%18 = load i32, i32* %5
%19 = load i8*, i8** %10
%20 = load i64, i64* %7
%21 = load %struct.stat64*, %struct.stat64** %8
%22 = call i32 @__ptsname_internal(i32 %18, i8* %19, i64 %20, %struct.stat64* %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %17
%26 = load i32, i32* %9
%27 = load i32, i32* @ENOTTY
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = load i32, i32* @EINVAL
store i32 %30, i32* %9
br label %31
31: ; preds = %29, %25
%32 = load i32, i32* %9
store i32 %32, i32* @errno
br label %66
33: ; preds = %17
%34 = load i8*, i8** %10
%35 = load i64, i64* %7
%36 = call i64 @memchr(i8* %34, i8 signext 0, i64 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %66
39: ; preds = %33
%40 = load i64, i64* %7
%41 = load i64, i64* %7
%42 = add i64 %41, %40
store i64 %42, i64* %7
br label %44
43: ; preds = %14
store i64 128, i64* %7
br label %44
44: ; preds = %43, %39
%45 = load i8*, i8** %10
%46 = load i8**, i8*** %6
%47 = load i8*, i8** %46
%48 = icmp ne i8* %45, %47
br i1 %48, label %49, label %54
49: ; preds = %44
%50 = load i8*, i8** %10
%51 = load i64, i64* %7
%52 = call i64 @realloc(i8* %50, i64 %51)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %11
br label %58
54: ; preds = %44
%55 = load i64, i64* %7
%56 = call i64 @malloc(i64 %55)
%57 = inttoptr i64 %56 to i8*
store i8* %57, i8** %11
br label %58
58: ; preds = %54, %49
%59 = load i8*, i8** %11
%60 = icmp ne i8* %59, null
br i1 %60, label %64, label %61
61: ; preds = %58
store i32 -1, i32* %9
%62 = load i32, i32* @ENOMEM
%63 = call i32 @__set_errno(i32 %62)
br label %66
64: ; preds = %58
%65 = load i8*, i8** %11
store i8* %65, i8** %10
br label %14
66: ; preds = %61, %38, %31
%67 = load i32, i32* %9
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load i8*, i8** %10
%71 = load i8**, i8*** %6
store i8* %70, i8** %71
br label %81
72: ; preds = %66
%73 = load i8*, i8** %10
%74 = load i8**, i8*** %6
%75 = load i8*, i8** %74
%76 = icmp ne i8* %73, %75
br i1 %76, label %77, label %80
77: ; preds = %72
%78 = load i8*, i8** %10
%79 = call i32 @free(i8* %78)
br label %80
80: ; preds = %77, %72
br label %81
81: ; preds = %80, %69
%82 = load i32, i32* %9
ret i32 %82
}

LLVM IR (-O2):
%struct.stat64 = type { i32 }
@ENOTTY = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @pts_name(i32 %0, i8** nocapture %1, i64 %2, %struct.stat64* %3) {
%5 = load i8*, i8** %1
br label %6
6: ; preds = %32, %4
%7 = phi i64 [ %2, %4 ], [ %19, %32 ]
%8 = phi i8* [ %5, %4 ], [ %33, %32 ]
%9 = icmp eq i64 %7, 0
br i1 %9, label %18, label %10
10: ; preds = %6
%11 = tail call i32 @__ptsname_internal(i32 %0, i8* %8, i64 %7, %struct.stat64* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %34
13: ; preds = %10
%14 = tail call i64 @memchr(i8* %8, i8 signext 0, i64 %7)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %40
16: ; preds = %13
%17 = shl i64 %7, 1
br label %18
18: ; preds = %6, %16
%19 = phi i64 [ %17, %16 ], [ 128, %6 ]
%20 = load i8*, i8** %1
%21 = icmp eq i8* %8, %20
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i64 @realloc(i8* %8, i64 %19)
br label %26
24: ; preds = %18
%25 = tail call i64 @malloc(i64 %19)
br label %26
26: ; preds = %24, %22
%27 = phi i64 [ %23, %22 ], [ %25, %24 ]
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @ENOMEM
%31 = tail call i32 @__set_errno(i32 %30)
br label %41
32: ; preds = %26
%33 = inttoptr i64 %27 to i8*
br label %6
34: ; preds = %10
%35 = load i32, i32* @ENOTTY
%36 = icmp eq i32 %11, %35
%37 = load i32, i32* @EINVAL
%38 = select i1 %36, i32 %37, i32 %11
store i32 %38, i32* @errno
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %13, %34
store i8* %8, i8** %1
br label %47
41: ; preds = %29, %34
%42 = phi i32 [ -1, %29 ], [ %38, %34 ]
%43 = load i8*, i8** %1
%44 = icmp eq i8* %8, %43
br i1 %44, label %47, label %45
45: ; preds = %41
%46 = tail call i32 @free(i8* %8)
br label %47
47: ; preds = %41, %45, %40
%48 = phi i32 [ %42, %41 ], [ %42, %45 ], [ 0, %40 ]
ret i32 %48
}