#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *);
scalar_t__ stat(char *, struct stat *);
int open_a_pty(int *pty, int *tty) {
  char line[12];
  char *p1, *p2;
  const char *cp;
  int i;
  sprintf(line, "/dev/ptyXX");
  p1 = &line[8];
  p2 = &line[9];
  for (cp = "pqrstuvwxyzPQRST"; *cp; cp++) {
    struct stat stb;
    *p1 = *cp;
    *p2 = '0';
    if (stat(line, &stb) < 0)
      break;
    for (i = 0; i < 16; i++) {
      *p2 = "0123456789abcdef"[i];
      *pty = open(line, O_RDWR);
      if (*pty > 0) {
        line[5] = 't';
        if ((*tty = open(line, O_RDWR)) < 0) {
          line[5] = 'p';
          close(*pty);
          continue;
        }
        return 0;
      }
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<open_a_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x24(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x30(%rbp)
lea    -0x24(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0x38(%rbp)
lea    0xe8c(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmpb   $0x0,(%rax)
je     1279 <open_a_pty+0x139>
mov    -0x40(%rbp),%rax
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
movb   $0x30,(%rax)
lea    -0x24(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11bf <open_a_pty+0x7f>
jmp    1279 <open_a_pty+0x139>
movl   $0x0,-0x44(%rbp)
cmpl   $0x10,-0x44(%rbp)
jge    1263 <open_a_pty+0x123>
movslq -0x44(%rbp),%rcx
lea    0xe41(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x38(%rbp),%rax
mov    %cl,(%rax)
lea    -0x24(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jle    1250 <open_a_pty+0x110>
movb   $0x74,-0x1f(%rbp)
lea    -0x24(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    -0x18(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    1244 <open_a_pty+0x104>
movb   $0x70,-0x1f(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <close@plt>
jmp    1255 <open_a_pty+0x115>
movl   $0x0,-0x4(%rbp)
jmp    1280 <open_a_pty+0x140>
jmp    1255 <open_a_pty+0x115>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11c6 <open_a_pty+0x86>
jmp    1268 <open_a_pty+0x128>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1183 <open_a_pty+0x43>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_a_pty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movabs $0x7974702f7665642f,%rax
70 74 79
mov    %rax,0x8(%rsp)
movl   $0x585879,0xf(%rsp)
lea    0xea9(%rip),%rbp
mov    $0x70,%al
lea    0x8(%rsp),%r15
lea    0x18(%rsp),%rsi
mov    0x2e73(%rip),%r12
jmp    11a1 <open_a_pty+0x71>
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp),%rbp
mov    0x1(%rbp),%al
add    $0x1,%rbp
lea    0xe88(%rip),%rcx
cmp    %rcx,%rbp
lea    0x18(%rsp),%rsi
je     122d <open_a_pty+0xfd>
mov    %al,0x10(%rsp)
movb   $0x30,0x11(%rsp)
mov    %r15,%rdi
call   1040 <stat@plt>
test   %rax,%rax
js     122d <open_a_pty+0xfd>
mov    %rbp,0x20(%rsp)
xor    %r13d,%r13d
lea    0xe56(%rip),%rbp
jmp    11da <open_a_pty+0xaa>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13
cmp    $0x10,%r13
je     1180 <open_a_pty+0x50>
movzbl 0x0(%r13,%rbp,1),%eax
mov    %al,0x11(%rsp)
mov    (%r12),%esi
mov    %r15,%rdi
call   1050 <open@plt>
mov    %eax,(%rbx)
test   %eax,%eax
jle    11d0 <open_a_pty+0xa0>
movb   $0x74,0xd(%rsp)
mov    (%r12),%esi
mov    %r15,%rdi
call   1050 <open@plt>
mov    %eax,(%r14)
test   %eax,%eax
jns    121c <open_a_pty+0xec>
movb   $0x70,0xd(%rsp)
mov    (%rbx),%edi
call   1030 <close@plt>
jmp    11d0 <open_a_pty+0xa0>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%eax
jmp    121e <open_a_pty+0xee>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"/dev/ptyXX\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzPQRST\00"
@.str.2 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
define i32 @open_a_pty(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [12 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca %struct.stat
store i32* %0, i32** %4
store i32* %1, i32** %5
%12 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%13 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 8
store i8* %14, i8** %7
%15 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 9
store i8* %15, i8** %8
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %9
br label %16
16: ; preds = %64, %2
%17 = load i8*, i8** %9
%18 = load i8, i8* %17
%19 = icmp ne i8 %18, 0
br i1 %19, label %20, label %67
20: ; preds = %16
%21 = load i8*, i8** %9
%22 = load i8, i8* %21
%23 = load i8*, i8** %7
store i8 %22, i8* %23
%24 = load i8*, i8** %8
store i8 48, i8* %24
%25 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%26 = call i64 @stat(i8* %25, %struct.stat* %11)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %20
br label %67
29: ; preds = %20
store i32 0, i32* %10
br label %30
30: ; preds = %60, %29
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 16
br i1 %32, label %33, label %63
33: ; preds = %30
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.2, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %8
store i8 %37, i8* %38
%39 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* @O_RDWR
%41 = call i32 @open(i8* %39, i32 %40)
%42 = load i32*, i32** %4
store i32 %41, i32* %42
%43 = load i32*, i32** %4
%44 = load i32, i32* %43
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %33
%47 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 5
store i8 116, i8* %47
%48 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 0
%49 = load i32, i32* @O_RDWR
%50 = call i32 @open(i8* %48, i32 %49)
%51 = load i32*, i32** %5
store i32 %50, i32* %51
%52 = icmp slt i32 %50, 0
br i1 %52, label %53, label %58
53: ; preds = %46
%54 = getelementptr inbounds [12 x i8], [12 x i8]* %6, i64 0, i64 5
store i8 112, i8* %54
%55 = load i32*, i32** %4
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
br label %60
58: ; preds = %46
store i32 0, i32* %3
br label %68
59: ; preds = %33
br label %60
60: ; preds = %59, %53
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %30
63: ; preds = %30
br label %64
64: ; preds = %63
%65 = load i8*, i8** %9
%66 = getelementptr inbounds i8, i8* %65, i32 1
store i8* %66, i8** %9
br label %16
67: ; preds = %28, %16
store i32 -1, i32* %3
br label %68
68: ; preds = %67, %58
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"/dev/ptyXX\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzPQRST\00"
@.str.2 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
define i32 @open_a_pty(i32* nocapture %0, i32* nocapture writeonly %1) {
%3 = alloca [12 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 8
%7 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 9
%8 = bitcast %struct.stat* %4 to i8*
%9 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 5
br label %10
10: ; preds = %2, %34
%11 = phi i8 [ 112, %2 ], [ %36, %34 ]
%12 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %2 ], [ %35, %34 ]
store i8 %11, i8* %6
store i8 48, i8* %7
%13 = call i64 @stat(i8* %5, %struct.stat* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %10, %30
%16 = phi i64 [ %31, %30 ], [ 0, %10 ]
%17 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.2, i64 0, i64 %16
%18 = load i8, i8* %17
store i8 %18, i8* %7
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %5, i32 %19)
store i32 %20, i32* %0
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %15
store i8 116, i8* %9
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %5, i32 %23)
store i32 %24, i32* %1
%25 = icmp slt i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %22
br label %38
27: ; preds = %22
store i8 112, i8* %9
%28 = load i32, i32* %0
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %15, %27
%31 = add nuw nsw i64 %16, 1
%32 = icmp eq i64 %31, 16
br i1 %32, label %34, label %15
33: ; preds = %10
br label %38
34: ; preds = %30
%35 = getelementptr inbounds i8, i8* %12, i64 1
%36 = load i8, i8* %35
%37 = icmp eq i8* %35, getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 16)
br i1 %37, label %38, label %10
38: ; preds = %34, %33, %26
%39 = phi i32 [ 0, %26 ], [ -1, %33 ], [ -1, %34 ]
ret i32 %39
}