#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;
      fdm = open(pts_name, O_RDWR);
      if (fdm < 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     1210 <ptym_open+0xf0>
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     11fa <ptym_open+0xda>
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)
cmpl   $0x0,-0x14(%rbp)
jge    11d6 <ptym_open+0xb6>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    11d1 <ptym_open+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <ptym_open+0xf7>
jmp    11e9 <ptym_open+0xc9>
mov    -0x10(%rbp),%rax
movb   $0x74,0x5(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <ptym_open+0xf7>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    116f <ptym_open+0x4f>
jmp    11ff <ptym_open+0xdf>
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 = %48, %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 %51
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 = %44, %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 %47
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 = load i32, i32* %4
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %24
%35 = load i64, i64* @errno
%36 = load i64, i64* @ENOENT
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 -1, i32* %2
br label %52
39: ; preds = %34
br label %44
40: ; preds = %24
%41 = load i8*, i8** %3
%42 = getelementptr inbounds i8, i8* %41, i64 5
store i8 116, i8* %42
%43 = load i32, i32* %4
store i32 %43, i32* %2
br label %52
44: ; preds = %39
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %6
br label %19
47: ; preds = %19
br label %48
48: ; preds = %47
%49 = load i8*, i8** %5
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %5
br label %9
51: ; preds = %9
store i32 -1, i32* %2
br label %52
52: ; preds = %51, %40, %38
%53 = load i32, i32* %2
ret i32 %53
}

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
}