#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 */
scalar_t__ EAGAIN;
scalar_t__ ENOENT;
scalar_t__ errno;
int open(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int posix_openpt(int oflag) {
  int fdm;
  char *ptr1, *ptr2;
  char ptm_name[16];
  strcpy(ptm_name, "/dev/ptyXY");
  for (ptr1 = "pqrstuvwxyzPQRST"; *ptr1 != 0; ptr1++) {
    ptm_name[8] = *ptr1;
    for (ptr2 = "0123456789abcdef"; *ptr2 != 0; ptr2++) {
      ptm_name[9] = *ptr2;
      if ((fdm = open(ptm_name, oflag)) < 0) {
        if (errno == ENOENT)
          return (-1);
        else
          continue;
      }
      return (fdm);
    }
  }
  errno = EAGAIN;
  return (-1);
}


x86 assembly (clang, -O0):
<posix_openpt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xeca(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xec9(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f8 <posix_openpt+0xd8>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x28(%rbp)
lea    0xeb6(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e2 <posix_openpt+0xc2>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x27(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c6 <posix_openpt+0xa6>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <posix_openpt+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <posix_openpt+0xf3>
jmp    11d1 <posix_openpt+0xb1>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <posix_openpt+0xf3>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    116a <posix_openpt+0x4a>
jmp    11e7 <posix_openpt+0xc7>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1146 <posix_openpt+0x26>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rcx
mov    0x2dd7(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<posix_openpt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0xec9(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
lea    0xec0(%rip),%rdx
mov    $0x70,%cl
mov    0x2e8c(%rip),%r15
mov    0x2e75(%rip),%r13
lea    0xeba(%rip),%rbp
mov    %rdx,0x8(%rsp)
mov    %cl,0x18(%rsp)
mov    $0x30,%cl
xor    %r12d,%r12d
mov    %cl,0x19(%rsp)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <open@plt>
test   %eax,%eax
jns    11c5 <posix_openpt+0xa5>
mov    (%r15),%rcx
mov    $0xffffffff,%eax
cmp    0x0(%r13),%rcx
je     11c5 <posix_openpt+0xa5>
movzbl 0x1(%r12,%rbp,1),%ecx
add    $0x1,%r12
cmp    $0x10,%r12
jne    1170 <posix_openpt+0x50>
mov    0x8(%rsp),%rdx
mov    0x1(%rdx),%cl
add    $0x1,%rdx
lea    0xe68(%rip),%rsi
cmp    %rsi,%rdx
jne    1162 <posix_openpt+0x42>
mov    0x2e09(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%r15)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/ptyXY\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzPQRST\00"
@.str.2 = [17 x i8] c"0123456789abcdef\00"
@errno = global i64 0
@ENOENT = global i64 0
@EAGAIN = global i64 0
define i32 @posix_openpt(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [16 x i8]
store i32 %0, i32* %3
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %5
br label %10
10: ; preds = %44, %1
%11 = load i8*, i8** %5
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %47
15: ; preds = %10
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 8
store i8 %17, i8* %18
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %6
br label %19
19: ; preds = %40, %15
%20 = load i8*, i8** %6
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %43
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 9
store i8 %26, i8* %27
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%29 = load i32, i32* %3
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %4
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %24
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 -1, i32* %2
br label %49
37: ; preds = %32
br label %40
38: ; preds = %24
%39 = load i32, i32* %4
store i32 %39, i32* %2
br label %49
40: ; preds = %37
%41 = load i8*, i8** %6
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %6
br label %19
43: ; preds = %19
br label %44
44: ; preds = %43
%45 = load i8*, i8** %5
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %5
br label %10
47: ; preds = %10
%48 = load i64, i64* @EAGAIN
store i64 %48, i64* @errno
store i32 -1, i32* %2
br label %49
49: ; preds = %47, %38, %36
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ptyXY\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzPQRST\00"
@.str.2 = [17 x i8] c"0123456789abcdef\00"
@errno = global i64 0
@ENOENT = global i64 0
@EAGAIN = global i64 0
define i32 @posix_openpt(i32 %0) {
%2 = alloca [16 x i8]
%3 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%4 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 8
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 9
br label %7
7: ; preds = %1, %23
%8 = phi i8 [ 112, %1 ], [ %25, %23 ]
%9 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %1 ], [ %24, %23 ]
store i8 %8, i8* %5
br label %13
10: ; preds = %18
%11 = load i8, i8* %22
%12 = icmp eq i8* %22, getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 16)
br i1 %12, label %23, label %13
13: ; preds = %7, %10
%14 = phi i8 [ 48, %7 ], [ %11, %10 ]
%15 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %7 ], [ %22, %10 ]
store i8 %14, i8* %6
%16 = call i32 @open(i8* %3, i32 %0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOENT
%21 = icmp eq i64 %19, %20
%22 = getelementptr inbounds i8, i8* %15, i64 1
br i1 %21, label %29, label %10
23: ; preds = %10
%24 = getelementptr inbounds i8, i8* %9, i64 1
%25 = load i8, i8* %24
%26 = icmp eq i8* %24, getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 16)
br i1 %26, label %27, label %7
27: ; preds = %23
%28 = load i64, i64* @EAGAIN
store i64 %28, i64* @errno
br label %29
29: ; preds = %13, %18, %27
%30 = phi i32 [ -1, %27 ], [ %16, %13 ], [ -1, %18 ]
ret i32 %30
}