#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 /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int openpty(int *master, int *slave, char *name, void *unused1, void *unused2) {
  int devindex = 0, letter = 0;
  int fd1, fd2;
  char ttydev[11], ptydev[11];
  syslog(LOG_DEBUG, "CTRL: Allocating pty/tty pair");
  strcpy(ttydev, "/dev/ttyxx");
  strcpy(ptydev, "/dev/ptyxx");
  while ("pqrstuvwxyzabcde"[letter]) {
    ttydev[11 - 3] = ptydev[11 - 3] = "pqrstuvwxyzabcde"[letter];
    while ("0123456789abcdef"[devindex]) {
      ttydev[11 - 2] = ptydev[11 - 2] = "0123456789abcdef"[devindex];
      if ((fd1 = open(ptydev, O_RDWR)) >= 0) {
        if ((fd2 = open(ttydev, O_RDWR)) >= 0) {
          goto out;
        } else {
          close(fd1);
        }
      }
      devindex++;
    }
    devindex = 0;
    letter++;
  }
  syslog(LOG_ERR, "CTRL: Failed to allocate pty");
  return -1;
out:
  syslog(LOG_INFO, "CTRL: Allocated pty/tty pair (%s,%s)", ptydev, ttydev);
  if (master)
    *master = fd1;
  if (slave)
    *slave = fd2;
  if (name)
    strcpy(name, ttydev);
  return 0;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
lea    0xe86(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
lea    -0x4b(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x56(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1030 <strcpy@plt>
movslq -0x38(%rbp),%rcx
lea    0xe88(%rip),%rax
cmpb   $0x0,(%rax,%rcx,1)
je     1264 <openpty+0x124>
movslq -0x38(%rbp),%rcx
lea    0xe73(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    %al,-0x4e(%rbp)
mov    %al,-0x43(%rbp)
movslq -0x34(%rbp),%rcx
lea    0xe70(%rip),%rax
cmpb   $0x0,(%rax,%rcx,1)
je     124f <openpty+0x10f>
movslq -0x34(%rbp),%rcx
lea    0xe5b(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    %al,-0x4d(%rbp)
mov    %al,-0x42(%rbp)
lea    -0x56(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jl     1241 <openpty+0x101>
lea    -0x4b(%rbp),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jl     1234 <openpty+0xf4>
jmp    1287 <openpty+0x147>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
jmp    1241 <openpty+0x101>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11ca <openpty+0x8a>
movl   $0x0,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11a1 <openpty+0x61>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <openpty+0x1ad>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    -0x56(%rbp),%rdx
lea    -0x4b(%rbp),%rcx
lea    0xdd4(%rip),%rsi
mov    $0x0,%al
call   1050 <syslog@plt>
cmpq   $0x0,-0x10(%rbp)
je     12ba <openpty+0x17a>
mov    -0x3c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     12ce <openpty+0x18e>
mov    -0x40(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     12e6 <openpty+0x1a6>
mov    -0x20(%rbp),%rdi
lea    -0x4b(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,0x28(%rsp)
mov    %rsi,0x20(%rsp)
mov    %rdi,0x18(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <syslog@plt>
lea    0xea1(%rip),%rsi
lea    0x2(%rsp),%rdi
call   1030 <strcpy@plt>
lea    0xe9b(%rip),%rsi
lea    0xd(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    $0x70,%al
mov    0x2e44(%rip),%r12
lea    0xe9a(%rip),%r15
jmp    11ce <openpty+0x8e>
nopl   (%rax)
mov    0x30(%rsp),%rbp
lea    0xe78(%rip),%rax
mov    0x1(%rbp,%rax,1),%al
add    $0x1,%rbp
cmp    $0x10,%rbp
je     1297 <openpty+0x157>
mov    %rbp,0x30(%rsp)
mov    %al,0x15(%rsp)
mov    %al,0xa(%rsp)
mov    $0x30,%al
xor    %r13d,%r13d
jmp    1207 <openpty+0xc7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
call   1040 <close@plt>
movzbl 0x1(%r13,%r15,1),%eax
add    $0x1,%r13
cmp    $0x10,%r13
je     11b0 <openpty+0x70>
mov    %al,0x16(%rsp)
mov    %al,0xb(%rsp)
mov    (%r12),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f7 <openpty+0xb7>
mov    %eax,%ebp
mov    (%r12),%esi
lea    0x2(%rsp),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f0 <openpty+0xb0>
mov    %eax,%r14d
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
lea    0xd(%rsp),%rdx
lea    0x2(%rsp),%rcx
xor    %eax,%eax
call   1050 <syslog@plt>
mov    0x18(%rsp),%rax
test   %rax,%rax
je     1263 <openpty+0x123>
mov    %ebp,(%rax)
mov    0x20(%rsp),%rax
test   %rax,%rax
mov    0x28(%rsp),%rdi
je     1275 <openpty+0x135>
mov    %r14d,(%rax)
xor    %ebx,%ebx
test   %rdi,%rdi
je     1286 <openpty+0x146>
lea    0x2(%rsp),%rsi
call   1030 <strcpy@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
xor    %eax,%eax
call   1050 <syslog@plt>
mov    $0xffffffff,%ebx
jmp    1286 <openpty+0x146>

LLVM IR (-O0):
@LOG_DEBUG = global i32 0
@.str = [30 x i8] c"CTRL: Allocating pty/tty pair\00"
@.str.1 = [11 x i8] c"/dev/ttyxx\00"
@.str.2 = [11 x i8] c"/dev/ptyxx\00"
@.str.3 = [17 x i8] c"pqrstuvwxyzabcde\00"
@.str.4 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
@LOG_ERR = global i32 0
@.str.5 = [29 x i8] c"CTRL: Failed to allocate pty\00"
@LOG_INFO = global i32 0
@.str.6 = [37 x i8] c"CTRL: Allocated pty/tty pair (%s,%s)\00"
define i32 @openpty(i32* %0, i32* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca [11 x i8]
%17 = alloca [11 x i8]
store i32* %0, i32** %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i32 0, i32* %12
store i32 0, i32* %13
%18 = load i32, i32* @LOG_DEBUG
%19 = call i32 (i32, i8*, ...) @syslog(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%20 = getelementptr inbounds [11 x i8], [11 x i8]* %16, i64 0, i64 0
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [11 x i8], [11 x i8]* %17, i64 0, i64 0
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %67, %5
%25 = load i32, i32* %13
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.3, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %70
30: ; preds = %24
%31 = load i32, i32* %13
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.3, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = getelementptr inbounds [11 x i8], [11 x i8]* %17, i64 0, i64 8
store i8 %34, i8* %35
%36 = getelementptr inbounds [11 x i8], [11 x i8]* %16, i64 0, i64 8
store i8 %34, i8* %36
br label %37
37: ; preds = %64, %30
%38 = load i32, i32* %12
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.4, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = icmp ne i8 %41, 0
br i1 %42, label %43, label %67
43: ; preds = %37
%44 = load i32, i32* %12
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.4, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = getelementptr inbounds [11 x i8], [11 x i8]* %17, i64 0, i64 9
store i8 %47, i8* %48
%49 = getelementptr inbounds [11 x i8], [11 x i8]* %16, i64 0, i64 9
store i8 %47, i8* %49
%50 = getelementptr inbounds [11 x i8], [11 x i8]* %17, i64 0, i64 0
%51 = load i32, i32* @O_RDWR
%52 = call i32 @open(i8* %50, i32 %51)
store i32 %52, i32* %14
%53 = icmp sge i32 %52, 0
br i1 %53, label %54, label %64
54: ; preds = %43
%55 = getelementptr inbounds [11 x i8], [11 x i8]* %16, i64 0, i64 0
%56 = load i32, i32* @O_RDWR
%57 = call i32 @open(i8* %55, i32 %56)
store i32 %57, i32* %15
%58 = icmp sge i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %54
br label %73
60: ; preds = %54
%61 = load i32, i32* %14
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %60
br label %64
64: ; preds = %63, %43
%65 = load i32, i32* %12
%66 = add nsw i32 %65, 1
store i32 %66, i32* %12
br label %37
67: ; preds = %37
store i32 0, i32* %12
%68 = load i32, i32* %13
%69 = add nsw i32 %68, 1
store i32 %69, i32* %13
br label %24
70: ; preds = %24
%71 = load i32, i32* @LOG_ERR
%72 = call i32 (i32, i8*, ...) @syslog(i32 %71, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %6
br label %97
73: ; preds = %59
%74 = load i32, i32* @LOG_INFO
%75 = getelementptr inbounds [11 x i8], [11 x i8]* %17, i64 0, i64 0
%76 = getelementptr inbounds [11 x i8], [11 x i8]* %16, i64 0, i64 0
%77 = call i32 (i32, i8*, ...) @syslog(i32 %74, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %75, i8* %76)
%78 = load i32*, i32** %7
%79 = icmp ne i32* %78, null
br i1 %79, label %80, label %83
80: ; preds = %73
%81 = load i32, i32* %14
%82 = load i32*, i32** %7
store i32 %81, i32* %82
br label %83
83: ; preds = %80, %73
%84 = load i32*, i32** %8
%85 = icmp ne i32* %84, null
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i32, i32* %15
%88 = load i32*, i32** %8
store i32 %87, i32* %88
br label %89
89: ; preds = %86, %83
%90 = load i8*, i8** %9
%91 = icmp ne i8* %90, null
br i1 %91, label %92, label %96
92: ; preds = %89
%93 = load i8*, i8** %9
%94 = getelementptr inbounds [11 x i8], [11 x i8]* %16, i64 0, i64 0
%95 = call i32 @strcpy(i8* %93, i8* %94)
br label %96
96: ; preds = %92, %89
store i32 0, i32* %6
br label %97
97: ; preds = %96, %70
%98 = load i32, i32* %6
ret i32 %98
}

LLVM IR (-O2):
@LOG_DEBUG = global i32 0
@.str = [30 x i8] c"CTRL: Allocating pty/tty pair\00"
@.str.1 = [11 x i8] c"/dev/ttyxx\00"
@.str.2 = [11 x i8] c"/dev/ptyxx\00"
@.str.3 = [17 x i8] c"pqrstuvwxyzabcde\00"
@.str.4 = [17 x i8] c"0123456789abcdef\00"
@O_RDWR = global i32 0
@LOG_ERR = global i32 0
@.str.5 = [29 x i8] c"CTRL: Failed to allocate pty\00"
@LOG_INFO = global i32 0
@.str.6 = [37 x i8] c"CTRL: Allocated pty/tty pair (%s,%s)\00"
define i32 @openpty(i32* writeonly %0, i32* writeonly %1, i8* %2, i8* nocapture readnone %3, i8* nocapture readnone %4) {
%6 = alloca [11 x i8]
%7 = alloca [11 x i8]
%8 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 0
%10 = load i32, i32* @LOG_DEBUG
%11 = tail call i32 (i32, i8*, ...) @syslog(i32 %10, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 8
%15 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 8
%16 = getelementptr inbounds [11 x i8], [11 x i8]* %7, i64 0, i64 9
%17 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 9
br label %18
18: ; preds = %5, %38
%19 = phi i64 [ 0, %5 ], [ %39, %38 ]
%20 = phi i8 [ 112, %5 ], [ %41, %38 ]
store i8 %20, i8* %14
store i8 %20, i8* %15
br label %21
21: ; preds = %18, %33
%22 = phi i64 [ 0, %18 ], [ %34, %33 ]
%23 = phi i8 [ 48, %18 ], [ %36, %33 ]
store i8 %23, i8* %16
store i8 %23, i8* %17
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %9, i32 %24)
%26 = icmp sgt i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %21
%28 = load i32, i32* @O_RDWR
%29 = call i32 @open(i8* %8, i32 %28)
%30 = icmp sgt i32 %29, -1
br i1 %30, label %46, label %31
31: ; preds = %27
%32 = call i32 @close(i32 %25)
br label %33
33: ; preds = %31, %21
%34 = add nuw nsw i64 %22, 1
%35 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.4, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i64 %34, 16
br i1 %37, label %38, label %21
38: ; preds = %33
%39 = add nuw nsw i64 %19, 1
%40 = getelementptr inbounds [17 x i8], [17 x i8]* @.str.3, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = icmp eq i64 %39, 16
br i1 %42, label %43, label %18
43: ; preds = %38
%44 = load i32, i32* @LOG_ERR
%45 = call i32 (i32, i8*, ...) @syslog(i32 %44, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %58
46: ; preds = %27
%47 = load i32, i32* @LOG_INFO
%48 = call i32 (i32, i8*, ...) @syslog(i32 %47, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i8* %9, i8* %8)
%49 = icmp eq i32* %0, null
br i1 %49, label %51, label %50
50: ; preds = %46
store i32 %25, i32* %0
br label %51
51: ; preds = %50, %46
%52 = icmp eq i32* %1, null
br i1 %52, label %54, label %53
53: ; preds = %51
store i32 %29, i32* %1
br label %54
54: ; preds = %53, %51
%55 = icmp eq i8* %2, null
br i1 %55, label %58, label %56
56: ; preds = %54
%57 = call i32 @strcpy(i8* %2, i8* %8)
br label %58
58: ; preds = %54, %56, %43
%59 = phi i32 [ -1, %43 ], [ 0, %56 ], [ 0, %54 ]
ret i32 %59
}