#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 */
int O_NOCTTY;
int O_RDWR;
int open(char *, int);
scalar_t__ openpty(int *, int *, char *, int /*<<< orphan*/ *,
                   int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int /*<<< orphan*/ strcpy(char *, char *);
int findpty(int *master, int *slave, char *name) {
  char pty[16];
  char tty[16];
  int i, j;
  int found;
  if (openpty(master, slave, name, NULL, NULL) >= 0)
    return 0;
  found = 0;
  for (i = 'p'; i <= 'z'; i++) {
    for (j = '0'; j <= 'f'; j++) {
      if (j == '9' + 1)
        j = 'a';
      sprintf(pty, "/dev/pty%c%c", i, j);
      sprintf(tty, "/dev/tty%c%c", i, j);
      if ((*master = open(pty, O_RDWR | O_NOCTTY)) >= 0) {
        *slave = open(tty, O_RDWR | O_NOCTTY);
        if (*slave >= 0) {
          found = 1;
          break;
        }
      }
    }
    if (found)
      break;
  }
  if (!found)
    return -1;
  if (name)
    strcpy(name, tty);
  return 0;
}


x86 assembly (clang, -O0):
<findpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rcx
call   1040 <openpty@plt>
cmp    $0x0,%rax
jl     1183 <findpty+0x43>
movl   $0x0,-0x4(%rbp)
jmp    12be <findpty+0x17e>
movl   $0x0,-0x4c(%rbp)
movl   $0x70,-0x44(%rbp)
cmpl   $0x7a,-0x44(%rbp)
jg     1289 <findpty+0x149>
movl   $0x30,-0x48(%rbp)
cmpl   $0x66,-0x48(%rbp)
jg     1267 <findpty+0x127>
cmpl   $0x3a,-0x48(%rbp)
jne    11bd <findpty+0x7d>
movl   $0x61,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x48(%rbp),%ecx
lea    0xe32(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x48(%rbp),%ecx
lea    0xe29(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jl     1254 <findpty+0x114>
lea    -0x40(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jl     124f <findpty+0x10f>
movl   $0x1,-0x4c(%rbp)
jmp    1267 <findpty+0x127>
jmp    1254 <findpty+0x114>
jmp    1259 <findpty+0x119>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    11a2 <findpty+0x62>
cmpl   $0x0,-0x4c(%rbp)
je     1276 <findpty+0x136>
jmp    1289 <findpty+0x149>
jmp    127b <findpty+0x13b>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1191 <findpty+0x51>
cmpl   $0x0,-0x4c(%rbp)
jne    129f <findpty+0x15f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <findpty+0x17e>
cmpq   $0x0,-0x20(%rbp)
je     12b7 <findpty+0x177>
mov    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <openpty@plt>
test   %rax,%rax
js     1177 <findpty+0x37>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,0x18(%rsp)
mov    %rbp,0x10(%rsp)
mov    %r14,0x8(%rsp)
mov    $0x70,%ebp
mov    $0x1,%dl
lea    0x30(%rsp),%r13
mov    0x2e3f(%rip),%rbx
jmp    11b5 <findpty+0x75>
nopl   0x0(%rax,%rax,1)
cmp    $0x7a,%ebp
lea    0x1(%rbp),%ecx
setb   %dl
mov    $0xffffffff,%eax
mov    %ecx,%ebp
cmp    $0x7b,%ecx
je     1168 <findpty+0x28>
mov    %dl,0x7(%rsp)
mov    $0x30,%r14d
jmp    11dd <findpty+0x9d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%r14),%eax
cmp    $0x66,%r14d
mov    %eax,%r14d
jge    11a0 <findpty+0x60>
cmp    $0x3a,%r14d
mov    $0x61,%eax
cmove  %eax,%r14d
mov    %r13,%rdi
lea    0xe0c(%rip),%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1060 <sprintf@plt>
lea    0x20(%rsp),%r15
mov    %r15,%rdi
lea    0xe00(%rip),%rsi
mov    %ebp,%edx
mov    %r14d,%ecx
call   1060 <sprintf@plt>
mov    (%rbx),%esi
mov    0x2dc8(%rip),%r12
or     (%r12),%esi
mov    %r13,%rdi
call   1050 <open@plt>
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
js     11d0 <findpty+0x90>
mov    (%rbx),%esi
or     (%r12),%esi
mov    %r15,%rdi
call   1050 <open@plt>
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx)
test   %eax,%eax
js     11d0 <findpty+0x90>
mov    $0xffffffff,%eax
testb  $0x1,0x7(%rsp)
je     1168 <findpty+0x28>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1166 <findpty+0x26>
lea    0x20(%rsp),%rsi
call   1030 <strcpy@plt>
jmp    1166 <findpty+0x26>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/pty%c%c\00"
@.str.1 = [13 x i8] c"/dev/tty%c%c\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
define i32 @findpty(i32* %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [16 x i8]
%9 = alloca [16 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
%13 = load i32*, i32** %5
%14 = load i32*, i32** %6
%15 = load i8*, i8** %7
%16 = call i64 @openpty(i32* %13, i32* %14, i8* %15, i32* null, i32* null)
%17 = icmp sge i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 0, i32* %4
br label %83
19: ; preds = %3
store i32 0, i32* %12
store i32 112, i32* %10
br label %20
20: ; preds = %68, %19
%21 = load i32, i32* %10
%22 = icmp sle i32 %21, 122
br i1 %22, label %23, label %71
23: ; preds = %20
store i32 48, i32* %11
br label %24
24: ; preds = %60, %23
%25 = load i32, i32* %11
%26 = icmp sle i32 %25, 102
br i1 %26, label %27, label %63
27: ; preds = %24
%28 = load i32, i32* %11
%29 = icmp eq i32 %28, 58
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 97, i32* %11
br label %31
31: ; preds = %30, %27
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%33 = load i32, i32* %10
%34 = load i32, i32* %11
%35 = call i32 @sprintf(i8* %32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %33, i32 %34)
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* %10
%38 = load i32, i32* %11
%39 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 %38)
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%41 = load i32, i32* @O_RDWR
%42 = load i32, i32* @O_NOCTTY
%43 = or i32 %41, %42
%44 = call i32 @open(i8* %40, i32 %43)
%45 = load i32*, i32** %5
store i32 %44, i32* %45
%46 = icmp sge i32 %44, 0
br i1 %46, label %47, label %59
47: ; preds = %31
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%49 = load i32, i32* @O_RDWR
%50 = load i32, i32* @O_NOCTTY
%51 = or i32 %49, %50
%52 = call i32 @open(i8* %48, i32 %51)
%53 = load i32*, i32** %6
store i32 %52, i32* %53
%54 = load i32*, i32** %6
%55 = load i32, i32* %54
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %47
store i32 1, i32* %12
br label %63
58: ; preds = %47
br label %59
59: ; preds = %58, %31
br label %60
60: ; preds = %59
%61 = load i32, i32* %11
%62 = add nsw i32 %61, 1
store i32 %62, i32* %11
br label %24
63: ; preds = %57, %24
%64 = load i32, i32* %12
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %63
br label %71
67: ; preds = %63
br label %68
68: ; preds = %67
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 1
store i32 %70, i32* %10
br label %20
71: ; preds = %66, %20
%72 = load i32, i32* %12
%73 = icmp ne i32 %72, 0
br i1 %73, label %75, label %74
74: ; preds = %71
store i32 -1, i32* %4
br label %83
75: ; preds = %71
%76 = load i8*, i8** %7
%77 = icmp ne i8* %76, null
br i1 %77, label %78, label %82
78: ; preds = %75
%79 = load i8*, i8** %7
%80 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%81 = call i32 @strcpy(i8* %79, i8* %80)
br label %82
82: ; preds = %78, %75
store i32 0, i32* %4
br label %83
83: ; preds = %82, %74, %18
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/pty%c%c\00"
@.str.1 = [13 x i8] c"/dev/tty%c%c\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
define i32 @findpty(i32* %0, i32* %1, i8* %2) {
%4 = alloca [16 x i8]
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = tail call i64 @openpty(i32* %0, i32* %1, i8* %2, i32* null, i32* null)
%9 = icmp sgt i64 %8, -1
br i1 %9, label %42, label %10
10: ; preds = %3, %33
%11 = phi i1 [ %35, %33 ], [ true, %3 ]
%12 = phi i32 [ %34, %33 ], [ 112, %3 ]
br label %13
13: ; preds = %10, %30
%14 = phi i32 [ 48, %10 ], [ %31, %30 ]
%15 = icmp eq i32 %14, 58
%16 = select i1 %15, i32 97, i32 %14
%17 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %16)
%18 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %12, i32 %16)
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_NOCTTY
%21 = or i32 %20, %19
%22 = call i32 @open(i8* %6, i32 %21)
store i32 %22, i32* %0
%23 = icmp sgt i32 %22, -1
br i1 %23, label %24, label %30
24: ; preds = %13
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_NOCTTY
%27 = or i32 %26, %25
%28 = call i32 @open(i8* %7, i32 %27)
store i32 %28, i32* %1
%29 = icmp sgt i32 %28, -1
br i1 %29, label %37, label %30
30: ; preds = %13, %24
%31 = add nsw i32 %16, 1
%32 = icmp slt i32 %16, 102
br i1 %32, label %13, label %33
33: ; preds = %30
%34 = add nuw nsw i32 %12, 1
%35 = icmp ult i32 %12, 122
%36 = icmp eq i32 %34, 123
br i1 %36, label %42, label %10
37: ; preds = %24
br i1 %11, label %38, label %42
38: ; preds = %37
%39 = icmp eq i8* %2, null
br i1 %39, label %42, label %40
40: ; preds = %38
%41 = call i32 @strcpy(i8* %2, i8* %7)
br label %42
42: ; preds = %33, %38, %40, %37, %3
%43 = phi i32 [ 0, %3 ], [ -1, %37 ], [ 0, %40 ], [ 0, %38 ], [ -1, %33 ]
ret i32 %43
}