#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 O_EXCL;
int O_RDWR;
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int stat(char *, struct stat *);
int allocate_pty() {
  int i;
  int fd = -1;
  char dev[20];
  struct stat sb;
  for (i = 0; i < 10; i++) {
    sprintf(dev, "/dev/ptyp%d", i);
    if (stat(dev, &sb) == -1) {
      return -1;
    }
    fd = open(dev, O_RDWR | O_EXCL);
    if (fd > 0) {
      return fd;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<allocate_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    11cb <allocate_pty+0x9b>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xea2(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
jne    1185 <allocate_pty+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <allocate_pty+0xa2>
lea    -0x20(%rbp),%rdi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11b8 <allocate_pty+0x88>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <allocate_pty+0xa2>
jmp    11bd <allocate_pty+0x8d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1146 <allocate_pty+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_pty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %ebx,%ebx
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
mov    0x2e97(%rip),%r13
mov    0x2e80(%rip),%r14
mov    $0xffffffff,%ebp
nopl   (%rax)
mov    %r15,%rdi
lea    0xe96(%rip),%rsi
mov    %ebx,%edx
call   1050 <sprintf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     11a0 <allocate_pty+0x70>
mov    (%r14),%esi
or     0x0(%r13),%esi
mov    %r15,%rdi
call   1040 <open@plt>
test   %eax,%eax
jg     119e <allocate_pty+0x6e>
add    $0x1,%ebx
cmp    $0xa,%ebx
jne    1160 <allocate_pty+0x30>
jmp    11a0 <allocate_pty+0x70>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/dev/ptyp%d\00"
@O_RDWR = global i32 0
@O_EXCL = global i32 0
define i32 @allocate_pty() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca %struct.stat
store i32 -1, i32* %3
store i32 0, i32* %2
br label %6
6: ; preds = %28, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 10
br i1 %8, label %9, label %31
9: ; preds = %6
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* %2
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%14 = call i32 @stat(i8* %13, %struct.stat* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %9
store i32 -1, i32* %1
br label %32
17: ; preds = %9
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_EXCL
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %18, i32 %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = load i32, i32* %3
store i32 %26, i32* %1
br label %32
27: ; preds = %17
br label %28
28: ; preds = %27
%29 = load i32, i32* %2
%30 = add nsw i32 %29, 1
store i32 %30, i32* %2
br label %6
31: ; preds = %6
store i32 -1, i32* %1
br label %32
32: ; preds = %31, %25, %16
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/dev/ptyp%d\00"
@O_RDWR = global i32 0
@O_EXCL = global i32 0
define i32 @allocate_pty() {
%1 = alloca [20 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
br label %8
5: ; preds = %13
%6 = add nuw nsw i32 %9, 1
%7 = icmp eq i32 %6, 10
br i1 %7, label %19, label %8
8: ; preds = %0, %5
%9 = phi i32 [ 0, %0 ], [ %6, %5 ]
%10 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @stat(i8* %3, %struct.stat* %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %19, label %13
13: ; preds = %8
%14 = load i32, i32* @O_RDWR
%15 = load i32, i32* @O_EXCL
%16 = or i32 %15, %14
%17 = call i32 @open(i8* %3, i32 %16)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %5
19: ; preds = %5, %13, %8
%20 = phi i32 [ -1, %8 ], [ %17, %13 ], [ -1, %5 ]
ret i32 %20
}