#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*/ O_RDWR;
int R_OK;
int W_OK;
scalar_t__ access(char *, int);
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ qe_pstrcpy(char *, int, char *);
int strlen(char *);
int get_pty(char *tty_str, int size) {
  int fd;
  char ptydev[] = "/dev/pty??";
  char ttydev[] = "/dev/tty??";
  int len = strlen(ttydev);
  const char *c1, *c2;
  for (c1 = "pqrstuvwxyzabcde"; *c1; c1++) {
    ptydev[len - 2] = ttydev[len - 2] = *c1;
    for (c2 = "0123456789abcdef"; *c2; c2++) {
      ptydev[len - 1] = ttydev[len - 1] = *c2;
      if ((fd = open(ptydev, O_RDWR)) >= 0) {
        if (access(ttydev, R_OK | W_OK) == 0) {
          qe_pstrcpy(tty_str, size, ttydev);
          return fd;
        }
        close(fd);
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe97(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xe8f(%rip),%al
mov    %al,-0x19(%rbp)
mov    0xe86(%rip),%rax
mov    %rax,-0x2e(%rbp)
mov    0xe83(%rip),%ax
mov    %ax,-0x26(%rbp)
mov    0xe7b(%rip),%al
mov    %al,-0x24(%rbp)
lea    -0x2e(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x34(%rbp)
lea    0xe66(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmpb   $0x0,(%rax)
je     12a7 <get_pty+0x157>
mov    -0x40(%rbp),%rax
mov    (%rax),%cl
mov    -0x34(%rbp),%eax
sub    $0x2,%eax
cltq
mov    %cl,-0x2e(%rbp,%rax,1)
mov    -0x34(%rbp),%eax
sub    $0x2,%eax
cltq
mov    %cl,-0x23(%rbp,%rax,1)
lea    0xe41(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmpb   $0x0,(%rax)
je     1291 <get_pty+0x141>
mov    -0x48(%rbp),%rax
mov    (%rax),%cl
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %cl,-0x2e(%rbp,%rax,1)
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
cltq
mov    %cl,-0x23(%rbp,%rax,1)
lea    -0x23(%rbp),%rdi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     127b <get_pty+0x12b>
lea    -0x2e(%rbp),%rdi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
call   1070 <access@plt>
cmp    $0x0,%rax
jne    1273 <get_pty+0x123>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x2e(%rbp),%rdx
call   1050 <qe_pstrcpy@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ae <get_pty+0x15e>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    1280 <get_pty+0x130>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    11ea <get_pty+0x9a>
jmp    1296 <get_pty+0x146>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    11b4 <get_pty+0x64>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,0x14(%rsp)
mov    %rdi,0x28(%rsp)
movabs $0x7974702f7665642f,%rax
70 74 79
mov    %rax,0x18(%rsp)
movl   $0x3f3f79,0x1f(%rsp)
movabs $0x7974742f7665642f,%rax
74 74 79
mov    %rax,0x8(%rsp)
movl   $0x3f3f79,0xf(%rsp)
lea    0x8(%rsp),%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
lea    0xe6d(%rip),%rcx
mov    $0x70,%al
mov    0x2e36(%rip),%r15
lea    0x18(%rsp),%r13
lea    0xe69(%rip),%r14
jmp    11dc <get_pty+0x8c>
mov    0x30(%rsp),%rcx
mov    0x1(%rcx),%al
add    $0x1,%rcx
lea    0xe53(%rip),%rdx
cmp    %rdx,%rcx
je     1265 <get_pty+0x115>
mov    %rcx,0x30(%rsp)
mov    %al,0x6(%rsp,%rbx,1)
mov    %al,0x16(%rsp,%rbx,1)
mov    $0x30,%al
xor    %r12d,%r12d
jmp    1207 <get_pty+0xb7>
mov    %ebp,%edi
call   1040 <close@plt>
movzbl 0x1(%r12,%r14,1),%eax
add    $0x1,%r12
cmp    $0x10,%r12
je     11c0 <get_pty+0x70>
mov    %al,0x7(%rsp,%rbx,1)
mov    %al,0x17(%rsp,%rbx,1)
mov    (%r15),%esi
mov    %r13,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f7 <get_pty+0xa7>
mov    %eax,%ebp
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
lea    0x8(%rsp),%rdi
call   1070 <access@plt>
test   %rax,%rax
jne    11f0 <get_pty+0xa0>
lea    0x8(%rsp),%rdx
mov    0x28(%rsp),%rdi
mov    0x14(%rsp),%esi
call   1050 <qe_pstrcpy@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
jmp    1254 <get_pty+0x104>

LLVM IR (-O0):
@__const.get_pty.ptydev = [11 x i8] c"/dev/pty??\00"
@__const.get_pty.ttydev = [11 x i8] c"/dev/tty??\00"
@.str = [17 x i8] c"pqrstuvwxyzabcde\00"
@.str.1 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
@R_OK = global i32 0
@W_OK = global i32 0
define i32 @get_pty(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [11 x i8]
%8 = alloca [11 x i8]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%12 = bitcast [11 x i8]* %7 to i8*
%13 = bitcast [11 x i8]* %8 to i8*
%14 = getelementptr inbounds [11 x i8], [11 x i8]* %8, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %9
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %10
br label %16
16: ; preds = %71, %2
%17 = load i8*, i8** %10
%18 = load i8, i8* %17
%19 = icmp ne i8 %18, 0
br i1 %19, label %20, label %74
20: ; preds = %16
%21 = load i8*, i8** %10
%22 = load i8, i8* %21
%23 = load i32, i32* %9
%24 = sub nsw i32 %23, 2
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [11 x i8], [11 x i8]* %8, i64 0, i64 %25
store i8 %22, i8* %26
%27 = load i32, i32* %9
%28 = sub nsw i32 %27, 2
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 %29
store i8 %22, i8* %30
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %11
br label %31
31: ; preds = %67, %20
%32 = load i8*, i8** %11
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %35, label %70
35: ; preds = %31
%36 = load i8*, i8** %11
%37 = load i8, i8* %36
%38 = load i32, i32* %9
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [11 x i8], [11 x i8]* %8, i64 0, i64 %40
store i8 %37, i8* %41
%42 = load i32, i32* %9
%43 = sub nsw i32 %42, 1
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 %44
store i8 %37, i8* %45
%46 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 0
%47 = load i32, i32* @O_RDWR
%48 = call i32 @open(i8* %46, i32 %47)
store i32 %48, i32* %6
%49 = icmp sge i32 %48, 0
br i1 %49, label %50, label %66
50: ; preds = %35
%51 = getelementptr inbounds [11 x i8], [11 x i8]* %8, i64 0, i64 0
%52 = load i32, i32* @R_OK
%53 = load i32, i32* @W_OK
%54 = or i32 %52, %53
%55 = call i64 @access(i8* %51, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %50
%58 = load i8*, i8** %4
%59 = load i32, i32* %5
%60 = getelementptr inbounds [11 x i8], [11 x i8]* %8, i64 0, i64 0
%61 = call i32 @qe_pstrcpy(i8* %58, i32 %59, i8* %60)
%62 = load i32, i32* %6
store i32 %62, i32* %3
br label %75
63: ; preds = %50
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %63, %35
br label %67
67: ; preds = %66
%68 = load i8*, i8** %11
%69 = getelementptr inbounds i8, i8* %68, i32 1
store i8* %69, i8** %11
br label %31
70: ; preds = %31
br label %71
71: ; preds = %70
%72 = load i8*, i8** %10
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %10
br label %16
74: ; preds = %16
store i32 -1, i32* %3
br label %75
75: ; preds = %74, %57
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@__const.get_pty.ptydev = [11 x i8] c"/dev/pty??\00"
@__const.get_pty.ttydev = [11 x i8] c"/dev/tty??\00"
@.str = [17 x i8] c"pqrstuvwxyzabcde\00"
@.str.1 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
@R_OK = global i32 0
@W_OK = global i32 0
define i32 @get_pty(i8* %0, i32 %1) {
%3 = alloca [11 x i8]
%4 = alloca [11 x i8]
%5 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%7 = call i32 @strlen(i8* %6)
%8 = add nsw i32 %7, -2
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 %9
%11 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 %9
%12 = add nsw i32 %7, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 %13
%15 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 %13
br label %16
16: ; preds = %2, %39
%17 = phi i8 [ 112, %2 ], [ %41, %39 ]
%18 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), %2 ], [ %40, %39 ]
store i8 %17, i8* %10
store i8 %17, i8* %11
br label %19
19: ; preds = %16, %35
%20 = phi i8 [ 48, %16 ], [ %37, %35 ]
%21 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %16 ], [ %36, %35 ]
store i8 %20, i8* %14
store i8 %20, i8* %15
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* %5, i32 %22)
%24 = icmp sgt i32 %23, -1
br i1 %24, label %25, label %35
25: ; preds = %19
%26 = load i32, i32* @R_OK
%27 = load i32, i32* @W_OK
%28 = or i32 %27, %26
%29 = call i64 @access(i8* %6, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @qe_pstrcpy(i8* %0, i32 %1, i8* %6)
br label %43
33: ; preds = %25
%34 = call i32 @close(i32 %23)
br label %35
35: ; preds = %19, %33
%36 = getelementptr inbounds i8, i8* %21, i64 1
%37 = load i8, i8* %36
%38 = icmp eq i8* %36, getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 16)
br i1 %38, label %39, label %19
39: ; preds = %35
%40 = getelementptr inbounds i8, i8* %18, i64 1
%41 = load i8, i8* %40
%42 = icmp eq i8* %40, getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 16)
br i1 %42, label %43, label %16
43: ; preds = %39, %31
%44 = phi i32 [ %23, %31 ], [ -1, %39 ]
ret i32 %44
}