#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 pty_open(char *pty_name) {
  char *ptr1, *ptr2;
  int fd;
  strcpy(pty_name, "/dev/ptyp0");
  for (ptr1 = "pqrstuvwxyzabcde"; *ptr1; ptr1++) {
    pty_name[8] = *ptr1;
    for (ptr2 = "0123456789abcdef"; *ptr2; ptr2++) {
      pty_name[9] = *ptr2;
      if ((fd = open(pty_name, O_RDWR)) >= 0) {
        pty_name[5] = 't';
        return (fd);
      } else if (errno == ENOENT)
        return (-1);
    }
  }
  return (-1);
}


x86 assembly (clang, -O0):
<pty_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,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     120e <pty_open+0xee>
mov    -0x18(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x8(%rax)
lea    0xeb4(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f8 <pty_open+0xd8>
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x9(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jl     11b7 <pty_open+0x97>
mov    -0x10(%rbp),%rax
movb   $0x74,0x5(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1215 <pty_open+0xf5>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%rax
jne    11dd <pty_open+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <pty_open+0xf5>
jmp    11e2 <pty_open+0xc2>
jmp    11e7 <pty_open+0xc7>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    116c <pty_open+0x4c>
jmp    11fd <pty_open+0xdd>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1147 <pty_open+0x27>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_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 <pty_open+0x9a>
mov    (%r12),%rcx
mov    $0xffffffff,%eax
cmp    0x0(%r13),%rcx
je     11be <pty_open+0x9e>
movzbl 0x1(%r14,%rbp,1),%ecx
add    $0x1,%r14
cmp    $0x10,%r14
jne    1170 <pty_open+0x50>
mov    (%rsp),%rdx
mov    0x1(%rdx),%cl
add    $0x1,%rdx
lea    0xe68(%rip),%rsi
cmp    %rsi,%rdx
jne    115f <pty_open+0x3f>
jmp    11be <pty_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/ptyp0\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzabcde\00"
@.str.2 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @pty_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
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** %4
br label %9
9: ; preds = %46, %1
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %49
13: ; preds = %9
%14 = load i8*, i8** %4
%15 = load i8, i8* %14
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i64 8
store i8 %15, i8* %17
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %5
br label %18
18: ; preds = %42, %13
%19 = load i8*, i8** %5
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %45
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 9
store i8 %24, i8* %26
%27 = load i8*, i8** %3
%28 = load i32, i32* @O_RDWR
%29 = call i32 @open(i8* %27, i32 %28)
store i32 %29, i32* %6
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i64 5
store i8 116, i8* %33
%34 = load i32, i32* %6
store i32 %34, i32* %2
br label %50
35: ; preds = %22
%36 = load i64, i64* @errno
%37 = load i64, i64* @ENOENT
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %2
br label %50
40: ; preds = %35
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %5
br label %18
45: ; preds = %18
br label %46
46: ; preds = %45
%47 = load i8*, i8** %4
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %4
br label %9
49: ; preds = %9
store i32 -1, i32* %2
br label %50
50: ; preds = %49, %39, %31
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/ptyp0\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzabcde\00"
@.str.2 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @pty_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 = %19
%9 = load i8, i8* %23
%10 = icmp eq i8* %23, 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 ], [ %23, %8 ]
store i8 %12, i8* %4
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* %0, i32 %14)
%16 = icmp sgt i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %0, i64 5
store i8 116, i8* %18
br label %28
19: ; preds = %11
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENOENT
%22 = icmp eq i64 %20, %21
%23 = getelementptr inbounds i8, i8* %13, i64 1
br i1 %22, label %28, label %8
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, %19, %17
%29 = phi i32 [ %15, %17 ], [ -1, %19 ], [ -1, %24 ]
ret i32 %29
}