#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 O_NOCTTY;
int O_RDWR;
int close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int grantpt(int);
int open(char const *, int);
char *ptsname(int);
int /*<<< orphan*/ stderr;
char *strerror(int);
char *ttyname(int);
int unlockpt(int);
int reopen(int fd) {
  const char *name = ttyname(fd);
  if (close(fd) == -1) {
    fprintf(stderr,
            "close(pty)"
            ": %s (%i)\n",
            strerror(errno), errno);
    return -errno;
  };
  fd = open(name, O_RDWR | O_NOCTTY);
  if (fd == -1) {
    fprintf(stderr,
            "open"
            ": %s (%i)\n",
            strerror(errno), errno);
    return -errno;
  };
  if (grantpt(fd) == -1) {
    fprintf(stderr,
            "grantpt"
            ": %s (%i)\n",
            strerror(errno), errno);
    return -errno;
  };
  if (unlockpt(fd) == -1) {
    fprintf(stderr,
            "unlockpt"
            ": %s (%i)\n",
            strerror(errno), errno);
    return -errno;
  };
  fprintf(stderr, "%s\n", ptsname(fd));
  return fd;
}


x86 assembly (clang, -O0):
<reopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <ttyname@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    11f2 <reopen+0x72>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2de0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1348 <reopen+0x1c8>
mov    -0x10(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1264 <reopen+0xe4>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
mov    0x2d87(%rip),%rax
mov    (%rax),%ecx
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d6e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1348 <reopen+0x1c8>
mov    -0x8(%rbp),%edi
call   1090 <grantpt@plt>
cmp    $0xffffffff,%eax
jne    12bf <reopen+0x13f>
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
mov    0x2d2c(%rip),%rax
mov    (%rax),%ecx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2d13(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1348 <reopen+0x1c8>
mov    -0x8(%rbp),%edi
call   1050 <unlockpt@plt>
cmp    $0xffffffff,%eax
jne    131a <reopen+0x19a>
mov    0x2cf9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
mov    0x2cd1(%rip),%rax
mov    (%rax),%ecx
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    0x2cb8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1348 <reopen+0x1c8>
mov    0x2caf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <ptsname@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd0e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reopen>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
call   1070 <ttyname@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1203 <reopen+0x83>
mov    0x2e47(%rip),%rax
mov    0x2e30(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1227 <reopen+0xa7>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <grantpt@plt>
cmp    $0xffffffff,%eax
je     124b <reopen+0xcb>
mov    %ebx,%edi
call   1050 <unlockpt@plt>
mov    0x2df7(%rip),%rcx
mov    (%rcx),%ebp
cmp    $0xffffffff,%eax
je     126f <reopen+0xef>
mov    %ebx,%edi
call   1030 <ptsname@plt>
lea    0xe57(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1299 <reopen+0x119>
mov    0x2dc6(%rip),%rax
mov    (%rax),%ebp
mov    0x2db5(%rip),%r14
mov    (%r14),%edi
call   10a0 <strerror@plt>
mov    (%r14),%ecx
lea    0xddb(%rip),%rsi
jmp    1288 <reopen+0x108>
mov    0x2da2(%rip),%rax
mov    (%rax),%ebp
mov    0x2d91(%rip),%r14
mov    (%r14),%edi
call   10a0 <strerror@plt>
mov    (%r14),%ecx
lea    0xdcc(%rip),%rsi
jmp    1288 <reopen+0x108>
mov    0x2d7e(%rip),%rax
mov    (%rax),%ebp
mov    0x2d6d(%rip),%r14
mov    (%r14),%edi
call   10a0 <strerror@plt>
mov    (%r14),%ecx
lea    0xdb7(%rip),%rsi
jmp    1288 <reopen+0x108>
mov    0x2d52(%rip),%r14
mov    (%r14),%edi
call   10a0 <strerror@plt>
mov    (%r14),%ecx
lea    0xdae(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
sub    (%r14),%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"close(pty): %s (%i)\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [15 x i8] c"open: %s (%i)\0A\00"
@.str.2 = [18 x i8] c"grantpt: %s (%i)\0A\00"
@.str.3 = [19 x i8] c"unlockpt: %s (%i)\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @reopen(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i8* @ttyname(i32 %5)
store i8* %6, i8** %4
%7 = load i32, i32* %3
%8 = call i32 @close(i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %18
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = load i32, i32* @errno
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14)
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %2
br label %64
18: ; preds = %1
%19 = load i8*, i8** %4
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_NOCTTY
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %19, i32 %22)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %34
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = load i32, i32* @errno
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30)
%32 = load i32, i32* @errno
%33 = sub nsw i32 0, %32
store i32 %33, i32* %2
br label %64
34: ; preds = %18
%35 = load i32, i32* %3
%36 = call i32 @grantpt(i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %46
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = load i32, i32* @errno
%43 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %41, i32 %42)
%44 = load i32, i32* @errno
%45 = sub nsw i32 0, %44
store i32 %45, i32* %2
br label %64
46: ; preds = %34
%47 = load i32, i32* %3
%48 = call i32 @unlockpt(i32 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %58
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = load i32, i32* @errno
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %53, i32 %54)
%56 = load i32, i32* @errno
%57 = sub nsw i32 0, %56
store i32 %57, i32* %2
br label %64
58: ; preds = %46
%59 = load i32, i32* @stderr
%60 = load i32, i32* %3
%61 = call i8* @ptsname(i32 %60)
%62 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = load i32, i32* %3
store i32 %63, i32* %2
br label %64
64: ; preds = %58, %50, %38, %26, %10
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"close(pty): %s (%i)\0A\00"
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [15 x i8] c"open: %s (%i)\0A\00"
@.str.2 = [18 x i8] c"grantpt: %s (%i)\0A\00"
@.str.3 = [19 x i8] c"unlockpt: %s (%i)\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @reopen(i32 %0) {
%2 = tail call i8* @ttyname(i32 %0)
%3 = tail call i32 @close(i32 %0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %13
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = load i32, i32* @errno
%10 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %52
13: ; preds = %1
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_NOCTTY
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* %2, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %27
19: ; preds = %13
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = load i32, i32* @errno
%24 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %23)
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
br label %52
27: ; preds = %13
%28 = tail call i32 @grantpt(i32 %17)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %38
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = tail call i8* @strerror(i32 %32)
%34 = load i32, i32* @errno
%35 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %33, i32 %34)
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
br label %52
38: ; preds = %27
%39 = tail call i32 @unlockpt(i32 %17)
%40 = icmp eq i32 %39, -1
%41 = load i32, i32* @stderr
br i1 %40, label %42, label %49
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = load i32, i32* @errno
%46 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %44, i32 %45)
%47 = load i32, i32* @errno
%48 = sub nsw i32 0, %47
br label %52
49: ; preds = %38
%50 = tail call i8* @ptsname(i32 %17)
%51 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %42, %30, %19, %5
%53 = phi i32 [ %12, %5 ], [ %26, %19 ], [ %37, %30 ], [ %48, %42 ], [ %17, %49 ]
ret i32 %53
}