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


x86 assembly (clang, -O0):
<ptym_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec9(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xec8(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     120f <ptym_open+0xef>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x8(%rax)
lea    0xeb1(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11f9 <ptym_open+0xd9>
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x9(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d5 <ptym_open+0xb5>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
jne    11d0 <ptym_open+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <ptym_open+0xf6>
jmp    11e8 <ptym_open+0xc8>
mov    -0x10(%rbp),%rax
movb   $0x74,0x5(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <ptym_open+0xf6>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116f <ptym_open+0x4f>
jmp    11fe <ptym_open+0xde>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1147 <ptym_open+0x27>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptym_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xecb(%rip),%rsi
call   1030 <strcpy@plt>
lea    0xeca(%rip),%rdx
mov    $0x70,%cl
mov    0x2e9e(%rip),%r15
mov    0x2e87(%rip),%r12
mov    0x2e70(%rip),%r13
lea    0xebd(%rip),%rbp
mov    %rdx,(%rsp)
mov    %cl,0x8(%rbx)
mov    $0x30,%cl
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    %cl,0x9(%rbx)
mov    (%r15),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
test   %eax,%eax
jns    11ba <ptym_open+0x9a>
mov    (%r12),%rcx
mov    $0xffffffff,%eax
cmp    0x0(%r13),%rcx
je     11be <ptym_open+0x9e>
movzbl 0x1(%r14,%rbp,1),%ecx
add    $0x1,%r14
cmp    $0x10,%r14
jne    1170 <ptym_open+0x50>
mov    (%rsp),%rdx
mov    0x1(%rdx),%cl
add    $0x1,%rdx
lea    0xe68(%rip),%rsi
cmp    %rsi,%rdx
jne    115f <ptym_open+0x3f>
jmp    11be <ptym_open+0x9e>
movb   $0x74,0x5(%rbx)
add    $0x8,%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"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @ptym_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32 @strcpy(i8* %7, 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 %9
9: ; preds = %47, %1
%10 = load i8*, i8** %5
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %50
14: ; preds = %9
%15 = load i8*, i8** %5
%16 = load i8, i8* %15
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i64 8
store i8 %16, 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 = %43, %14
%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 %46
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 9
store i8 %26, i8* %28
%29 = load i8*, i8** %3
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %4
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %24
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 -1, i32* %2
br label %51
38: ; preds = %33
br label %43
39: ; preds = %24
%40 = load i8*, i8** %3
%41 = getelementptr inbounds i8, i8* %40, i64 5
store i8 116, i8* %41
%42 = load i32, i32* %4
store i32 %42, i32* %2
br label %51
43: ; preds = %38
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %6
br label %19
46: ; preds = %19
br label %47
47: ; preds = %46
%48 = load i8*, i8** %5
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %5
br label %9
50: ; preds = %9
store i32 -1, i32* %2
br label %51
51: ; preds = %50, %39, %37
%52 = load i32, i32* %2
ret i32 %52
}

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"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @ptym_open(i8* %0) {
%2 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%3 = getelementptr inbounds i8, i8* %0, i64 8
%4 = getelementptr inbounds i8, i8* %0, i64 9
br label %5
5: ; preds = %1, %24
%6 = phi i8 [ 112, %1 ], [ %26, %24 ]
%7 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %1 ], [ %25, %24 ]
store i8 %6, i8* %3
br label %11
8: ; preds = %17
%9 = load i8, i8* %21
%10 = icmp eq i8* %21, getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 16)
br i1 %10, label %24, label %11
11: ; preds = %5, %8
%12 = phi i8 [ 48, %5 ], [ %9, %8 ]
%13 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), %5 ], [ %21, %8 ]
store i8 %12, i8* %4
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* %0, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
%21 = getelementptr inbounds i8, i8* %13, i64 1
br i1 %20, label %28, label %8
22: ; preds = %11
%23 = getelementptr inbounds i8, i8* %0, i64 5
store i8 116, i8* %23
br label %28
24: ; preds = %8
%25 = getelementptr inbounds i8, i8* %7, i64 1
%26 = load i8, i8* %25
%27 = icmp eq i8* %25, getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 16)
br i1 %27, label %28, label %5
28: ; preds = %24, %17, %22
%29 = phi i32 [ %15, %22 ], [ -1, %17 ], [ -1, %24 ]
ret i32 %29
}