#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 dummy;
};
struct termios {
  int dummy;
};

/* Variables and functions */
scalar_t__ _internal_get_pty(int *, char **);
scalar_t__ _internal_get_tty(int *, char *, struct termios *, struct winsize *,
                             int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ strcpy(char *, char *);
int openpty(int *amaster, int *aslave, char *name, struct termios *termp,
            struct winsize *winp) {
  char *path = NULL;
  int master = -1, slave = -1;
  if (amaster == NULL || aslave == NULL)
    return -1;
  if (_internal_get_pty(&master, &path) != 0)
    goto out;
  if (_internal_get_tty(&slave, path, termp, winp, 0) != 0)
    goto out;
  if (name != NULL)
    strcpy(name, path);
  *amaster = master;
  *aslave = slave;
  return 0;
out:
  if (master != -1)
    close(master);
  if (slave != -1)
    close(slave);
  return -1;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
movl   $0xffffffff,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1188 <openpty+0x48>
cmpq   $0x0,-0x18(%rbp)
jne    1194 <openpty+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <openpty+0xf8>
lea    -0x3c(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <_internal_get_pty@plt>
cmp    $0x0,%rax
je     11b0 <openpty+0x70>
jmp    120d <openpty+0xcd>
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    -0x40(%rbp),%rdi
xor    %r8d,%r8d
call   1060 <_internal_get_tty@plt>
cmp    $0x0,%rax
je     11d7 <openpty+0x97>
jmp    120d <openpty+0xcd>
cmpq   $0x0,-0x20(%rbp)
je     11ef <openpty+0xaf>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x3c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1238 <openpty+0xf8>
cmpl   $0xffffffff,-0x3c(%rbp)
je     121f <openpty+0xdf>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x40(%rbp)
je     1231 <openpty+0xf1>
mov    -0x40(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
movl   $0xffffffff,0xc(%rsp)
ff
movl   $0xffffffff,0x8(%rsp)
ff
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     11fb <openpty+0xbb>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11fb <openpty+0xbb>
mov    %r8,%r12
mov    %rcx,%r13
mov    %rdx,%r15
mov    %rdi,%rbp
lea    0xc(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1050 <_internal_get_pty@plt>
test   %rax,%rax
jne    11bb <openpty+0x7b>
mov    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdi
mov    %r13,%rdx
mov    %r12,%rcx
xor    %r8d,%r8d
call   1060 <_internal_get_tty@plt>
test   %rax,%rax
je     11d9 <openpty+0x99>
mov    0xc(%rsp),%edi
cmp    $0xffffffff,%edi
je     11c9 <openpty+0x89>
call   1040 <close@plt>
mov    0x8(%rsp),%edi
cmp    $0xffffffff,%edi
je     11fb <openpty+0xbb>
call   1040 <close@plt>
jmp    11fb <openpty+0xbb>
test   %r15,%r15
je     11eb <openpty+0xab>
mov    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x0(%rbp)
mov    0x8(%rsp),%eax
mov    %eax,(%rbx)
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
define i32 @openpty(i32* %0, i32* %1, i8* %2, %struct.termios* %3, %struct.winsize* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca %struct.termios*
%11 = alloca %struct.winsize*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i32* %0, i32** %7
store i32* %1, i32** %8
store i8* %2, i8** %9
store %struct.termios* %3, %struct.termios** %10
store %struct.winsize* %4, %struct.winsize** %11
store i8* null, i8** %12
store i32 -1, i32* %13
store i32 -1, i32* %14
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %5
%18 = load i32*, i32** %8
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %17, %5
store i32 -1, i32* %6
br label %57
21: ; preds = %17
%22 = call i64 @_internal_get_pty(i32* %13, i8** %12)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
br label %44
25: ; preds = %21
%26 = load i8*, i8** %12
%27 = load %struct.termios*, %struct.termios** %10
%28 = load %struct.winsize*, %struct.winsize** %11
%29 = call i64 @_internal_get_tty(i32* %14, i8* %26, %struct.termios* %27, %struct.winsize* %28, i32 0)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
br label %44
32: ; preds = %25
%33 = load i8*, i8** %9
%34 = icmp ne i8* %33, null
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = load i8*, i8** %9
%37 = load i8*, i8** %12
%38 = call i32 @strcpy(i8* %36, i8* %37)
br label %39
39: ; preds = %35, %32
%40 = load i32, i32* %13
%41 = load i32*, i32** %7
store i32 %40, i32* %41
%42 = load i32, i32* %14
%43 = load i32*, i32** %8
store i32 %42, i32* %43
store i32 0, i32* %6
br label %57
44: ; preds = %31, %24
%45 = load i32, i32* %13
%46 = icmp ne i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* %13
%49 = call i32 @close(i32 %48)
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* %14
%52 = icmp ne i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i32, i32* %14
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %50
store i32 -1, i32* %6
br label %57
57: ; preds = %56, %39, %20
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
define i32 @openpty(i32* writeonly %0, i32* writeonly %1, i8* %2, %struct.termios* %3, %struct.winsize* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%10 = bitcast i32* %7 to i8*
store i32 -1, i32* %7
%11 = bitcast i32* %8 to i8*
store i32 -1, i32* %8
%12 = icmp eq i32* %0, null
%13 = icmp eq i32* %1, null
%14 = or i1 %12, %13
br i1 %14, label %40, label %15
15: ; preds = %5
%16 = call i64 @_internal_get_pty(i32* %7, i8** %6)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = call i64 @_internal_get_tty(i32* %8, i8* %19, %struct.termios* %3, %struct.winsize* %4, i32 0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %18
%23 = icmp eq i8* %2, null
br i1 %23, label %27, label %24
24: ; preds = %22
%25 = load i8*, i8** %6
%26 = call i32 @strcpy(i8* %2, i8* %25)
br label %27
27: ; preds = %24, %22
%28 = load i32, i32* %7
store i32 %28, i32* %0
%29 = load i32, i32* %8
store i32 %29, i32* %1
br label %40
30: ; preds = %18, %15
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @close(i32 %31)
br label %35
35: ; preds = %33, %30
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @close(i32 %36)
br label %40
40: ; preds = %35, %38, %5, %27
%41 = phi i32 [ 0, %27 ], [ -1, %5 ], [ -1, %38 ], [ -1, %35 ]
ret i32 %41
}