#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 winsize {
  int ws_row;
  int ws_col;
};

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct winsize *);
int open(char *, int);
int /*<<< orphan*/ openpty(int *, int *, int /*<<< orphan*/ *,
                           int /*<<< orphan*/ *, struct winsize *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int compat_getpty(int *ptyfd, int *slave) {
  char name[16];
  int i, j, o_error;
  struct winsize winsz = {
      .ws_row = 25,
      .ws_col = 80,
  };
  if (!openpty(ptyfd, slave, NULL, NULL, &winsz))
    return 0;
  o_error = errno;
  strcpy(name, "/dev/ptyXX");
  for (i = 0; i < 16; i++) {
    name[8] = "pqrstuvwxyzabcde"[i];
    name[9] = '0';
    for (j = 0; j < 16; j++) {
      name[9] = j < 10 ? j + '0' : j - 10 + 'a';
      *ptyfd = open(name, O_RDWR | O_NOCTTY);
      if (*ptyfd >= 0) {
        name[5] = 't';
        *slave = open(name, O_RDWR | O_NOCTTY);
        if (*slave >= 0) {
          if (ioctl(*slave, TIOCSWINSZ, &winsz))
            perror("warning-ioctl(TIOCSWINSZ)");
          return 0;
        } else
          close(*ptyfd);
      }
    }
  }
  fprintf(stderr, "Failed\n");
  errno = o_error;
  return -1;
}


x86 assembly (clang, -O0):
<compat_getpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xe79(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x48(%rbp),%r8
mov    %rcx,%rdx
call   1070 <openpty@plt>
cmp    $0x0,%eax
jne    11b8 <compat_getpty+0x48>
movl   $0x0,-0x4(%rbp)
jmp    1331 <compat_getpty+0x1c1>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x34(%rbp)
cmpl   $0x10,-0x34(%rbp)
jge    1309 <compat_getpty+0x199>
movslq -0x34(%rbp),%rcx
lea    0xe23(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    %al,-0x28(%rbp)
movb   $0x30,-0x27(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x10,-0x38(%rbp)
jge    12f6 <compat_getpty+0x186>
cmpl   $0xa,-0x38(%rbp)
jge    1223 <compat_getpty+0xb3>
mov    -0x38(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0x4c(%rbp)
jmp    122f <compat_getpty+0xbf>
mov    -0x38(%rbp),%eax
sub    $0xa,%eax
add    $0x61,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    %al,-0x27(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2d87(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jl     12e3 <compat_getpty+0x173>
movb   $0x74,-0x2b(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    0x2d53(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jl     12d3 <compat_getpty+0x163>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     12c7 <compat_getpty+0x157>
lea    0xd62(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1331 <compat_getpty+0x1c1>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
jmp    12e3 <compat_getpty+0x173>
jmp    12e8 <compat_getpty+0x178>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1201 <compat_getpty+0x91>
jmp    12fb <compat_getpty+0x18b>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11db <compat_getpty+0x6b>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2c98(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compat_getpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movabs $0x5000000019,%rax
mov    %rax,0x8(%rsp)
xor    %r15d,%r15d
lea    0x8(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <openpty@plt>
test   %eax,%eax
je     128e <compat_getpty+0x11e>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
lea    0xe40(%rip),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
lea    0xe35(%rip),%rcx
mov    0x2e0b(%rip),%r13
mov    0x2dec(%rip),%r12
jmp    120a <compat_getpty+0x9a>
cs nopw 0x0(%rax,%rax,1)
mov    0x20(%rsp),%rax
add    $0x1,%rax
cmp    $0x10,%rax
lea    0xe07(%rip),%rcx
je     12ae <compat_getpty+0x13e>
mov    %rax,0x20(%rsp)
mov    (%rax,%rcx,1),%al
mov    %al,0x18(%rsp)
xor    %ebp,%ebp
jmp    122f <compat_getpty+0xbf>
nopw   0x0(%rax,%rax,1)
mov    (%rbx),%edi
call   1050 <close@plt>
add    $0x1,%ebp
cmp    $0x10,%ebp
je     11f0 <compat_getpty+0x80>
lea    0x57(%rbp),%eax
lea    0x30(%rbp),%ecx
cmp    $0xa,%ebp
cmovae %eax,%ecx
mov    %cl,0x19(%rsp)
mov    (%r12),%esi
or     0x0(%r13),%esi
mov    %r15,%rdi
call   1080 <open@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     1227 <compat_getpty+0xb7>
movb   $0x74,0x15(%rsp)
mov    (%r12),%esi
or     0x0(%r13),%esi
mov    %r15,%rdi
call   1080 <open@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     1220 <compat_getpty+0xb0>
mov    0x2d68(%rip),%rcx
mov    (%rcx),%esi
lea    0x8(%rsp),%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
xor    %r15d,%r15d
test   %rax,%rax
jne    12a0 <compat_getpty+0x130>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd75(%rip),%rdi
call   1090 <perror@plt>
jmp    128e <compat_getpty+0x11e>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xd78(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2cf6(%rip),%rax
mov    0x4(%rsp),%ecx
mov    %ecx,(%rax)
mov    $0xffffffff,%r15d
jmp    128e <compat_getpty+0x11e>

LLVM IR (-O0):
%struct.winsize = type { i32, i32 }
@__const.compat_getpty.winsz = %struct.winsize { i32 25, i32 80 }
@errno = global i32 0
@.str = [11 x i8] c"/dev/ptyXX\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzabcde\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TIOCSWINSZ = global i32 0
@.str.2 = [26 x i8] c"warning-ioctl(TIOCSWINSZ)\00"
@stderr = global i32 0
@.str.3 = [8 x i8] c"Failed\0A\00"
define i32 @compat_getpty(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [16 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.winsize
store i32* %0, i32** %4
store i32* %1, i32** %5
%11 = bitcast %struct.winsize* %10 to i8*
%12 = load i32*, i32** %4
%13 = load i32*, i32** %5
%14 = call i32 @openpty(i32* %12, i32* %13, i32* null, i32* null, %struct.winsize* %10)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 0, i32* %3
br label %94
17: ; preds = %2
%18 = load i32, i32* @errno
store i32 %18, i32* %9
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %21
21: ; preds = %87, %17
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 16
br i1 %23, label %24, label %90
24: ; preds = %21
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 8
store i8 %28, i8* %29
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 9
store i8 48, i8* %30
store i32 0, i32* %8
br label %31
31: ; preds = %83, %24
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 16
br i1 %33, label %34, label %86
34: ; preds = %31
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 10
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* %8
%39 = add nsw i32 %38, 48
br label %44
40: ; preds = %34
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, 10
%43 = add nsw i32 %42, 97
br label %44
44: ; preds = %40, %37
%45 = phi i32 [ %39, %37 ], [ %43, %40 ]
%46 = trunc i32 %45 to i8
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 9
store i8 %46, i8* %47
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %6, 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** %4
store i32 %52, i32* %53
%54 = load i32*, i32** %4
%55 = load i32, i32* %54
%56 = icmp sge i32 %55, 0
br i1 %56, label %57, label %82
57: ; preds = %44
%58 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 5
store i8 116, i8* %58
%59 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%60 = load i32, i32* @O_RDWR
%61 = load i32, i32* @O_NOCTTY
%62 = or i32 %60, %61
%63 = call i32 @open(i8* %59, i32 %62)
%64 = load i32*, i32** %5
store i32 %63, i32* %64
%65 = load i32*, i32** %5
%66 = load i32, i32* %65
%67 = icmp sge i32 %66, 0
br i1 %67, label %68, label %77
68: ; preds = %57
%69 = load i32*, i32** %5
%70 = load i32, i32* %69
%71 = load i32, i32* @TIOCSWINSZ
%72 = call i64 @ioctl(i32 %70, i32 %71, %struct.winsize* %10)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %68
%75 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %76
76: ; preds = %74, %68
store i32 0, i32* %3
br label %94
77: ; preds = %57
%78 = load i32*, i32** %4
%79 = load i32, i32* %78
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %77
br label %82
82: ; preds = %81, %44
br label %83
83: ; preds = %82
%84 = load i32, i32* %8
%85 = add nsw i32 %84, 1
store i32 %85, i32* %8
br label %31
86: ; preds = %31
br label %87
87: ; preds = %86
%88 = load i32, i32* %7
%89 = add nsw i32 %88, 1
store i32 %89, i32* %7
br label %21
90: ; preds = %21
%91 = load i32, i32* @stderr
%92 = call i32 @fprintf(i32 %91, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%93 = load i32, i32* %9
store i32 %93, i32* @errno
store i32 -1, i32* %3
br label %94
94: ; preds = %90, %76, %16
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.winsize = type { i32, i32 }
@errno = global i32 0
@.str = [11 x i8] c"/dev/ptyXX\00"
@.str.1 = [17 x i8] c"pqrstuvwxyzabcde\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TIOCSWINSZ = global i32 0
@.str.2 = [26 x i8] c"warning-ioctl(TIOCSWINSZ)\00"
@stderr = global i32 0
@.str.3 = [8 x i8] c"Failed\0A\00"
define i32 @compat_getpty(i32* %0, i32* %1) {
%3 = alloca [16 x i8]
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.winsize*
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%7 = bitcast i64* %4 to i8*
store i64 343597383705, i64* %4
%8 = call i32 @openpty(i32* %0, i32* %1, i32* null, i32* null, %struct.winsize* %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %55, label %10
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 8
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 9
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 5
br label %16
16: ; preds = %10, %49
%17 = phi i64 [ 0, %10 ], [ %50, %49 ]
%18 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.1, i64 0, i64 %17
%19 = load i8, i8* %18
store i8 %19, i8* %13
br label %20
20: ; preds = %16, %46
%21 = phi i32 [ 0, %16 ], [ %47, %46 ]
%22 = icmp ult i32 %21, 10
%23 = select i1 %22, i32 48, i32 87
%24 = add nuw nsw i32 %23, %21
%25 = trunc i32 %24 to i8
store i8 %25, i8* %14
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_NOCTTY
%28 = or i32 %27, %26
%29 = call i32 @open(i8* %6, i32 %28)
store i32 %29, i32* %0
%30 = icmp sgt i32 %29, -1
br i1 %30, label %31, label %46
31: ; preds = %20
store i8 116, i8* %15
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_NOCTTY
%34 = or i32 %33, %32
%35 = call i32 @open(i8* %6, i32 %34)
store i32 %35, i32* %1
%36 = icmp sgt i32 %35, -1
br i1 %36, label %37, label %43
37: ; preds = %31
%38 = load i32, i32* @TIOCSWINSZ
%39 = call i64 @ioctl(i32 %35, i32 %38, %struct.winsize* %5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %55, label %41
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %55
43: ; preds = %31
%44 = load i32, i32* %0
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %20, %43
%47 = add nuw nsw i32 %21, 1
%48 = icmp eq i32 %47, 16
br i1 %48, label %49, label %20
49: ; preds = %46
%50 = add nuw nsw i64 %17, 1
%51 = icmp eq i64 %50, 16
br i1 %51, label %52, label %16
52: ; preds = %49
%53 = load i32, i32* @stderr
%54 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 %11, i32* @errno
br label %55
55: ; preds = %37, %41, %2, %52
%56 = phi i32 [ -1, %52 ], [ 0, %2 ], [ 0, %41 ], [ 0, %37 ]
ret i32 %56
}