#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;
scalar_t__ grantpt(int);
int open(char *, int);
int ptsname_compat(int, char **);
scalar_t__ unlockpt(int);
int _internal_get_pty(int *master, char **path) {
  if ((*master = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
    return -1;
  if (grantpt(*master) != 0)
    return -1;
  if (unlockpt(*master) != 0)
    return -1;
  if (ptsname_compat(*master, path) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<_internal_get_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
or     (%rax),%esi
lea    0xe97(%rip),%rdi
call   1050 <open@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    1189 <_internal_get_pty+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <_internal_get_pty+0xb6>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <grantpt@plt>
cmp    $0x0,%rax
je     11aa <_internal_get_pty+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <_internal_get_pty+0xb6>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <unlockpt@plt>
cmp    $0x0,%rax
je     11cb <_internal_get_pty+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <_internal_get_pty+0xb6>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
call   1030 <ptsname_compat@plt>
cmp    $0xffffffff,%eax
jne    11ef <_internal_get_pty+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <_internal_get_pty+0xb6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_internal_get_pty>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e97(%rip),%rax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1050 <open@plt>
mov    %eax,(%rbx)
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11a1 <_internal_get_pty+0x61>
mov    %eax,%edi
call   1060 <grantpt@plt>
test   %rax,%rax
jne    11a1 <_internal_get_pty+0x61>
mov    (%rbx),%edi
call   1040 <unlockpt@plt>
test   %rax,%rax
jne    11a1 <_internal_get_pty+0x61>
mov    (%rbx),%edi
mov    %r14,%rsi
call   1030 <ptsname_compat@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
define i32 @_internal_get_pty(i32* nocapture %0, i8** %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %0
%7 = icmp eq i32 %6, -1
br i1 %7, label %20, label %8
8: ; preds = %2
%9 = tail call i64 @grantpt(i32 %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = load i32, i32* %0
%13 = tail call i64 @unlockpt(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* %0
%17 = tail call i32 @ptsname_compat(i32 %16, i8** %1)
%18 = icmp eq i32 %17, -1
%19 = sext i1 %18 to i32
br label %20
20: ; preds = %15, %11, %8, %2
%21 = phi i32 [ -1, %2 ], [ -1, %8 ], [ -1, %11 ], [ %19, %15 ]
ret i32 %21
}