#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ grantpt(int);
int open(char *, int /*<<< orphan*/);
char *ptsname(int);
char *strdup(char *);
scalar_t__ unlockpt(int);
int get_master_pty(char **name) {
  int master;
  char *slavename;
  master = open("/dev/ptmx", O_RDWR);
  if (master >= 0 && grantpt(master) >= 0 && unlockpt(master) >= 0) {
    slavename = ptsname(master);
    if (slavename != NULL) {
      *name = strdup(slavename);
      return master;
    }
    close(master);
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_master_pty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     11ef <get_master_pty+0x8f>
mov    -0x14(%rbp),%edi
call   1070 <grantpt@plt>
cmp    $0x0,%rax
jl     11ef <get_master_pty+0x8f>
mov    -0x14(%rbp),%edi
call   1050 <unlockpt@plt>
cmp    $0x0,%rax
jl     11ef <get_master_pty+0x8f>
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e7 <get_master_pty+0x87>
mov    -0x20(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <get_master_pty+0x96>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_master_pty>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11ba <get_master_pty+0x5a>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <grantpt@plt>
test   %rax,%rax
js     11ba <get_master_pty+0x5a>
mov    %ebx,%edi
call   1050 <unlockpt@plt>
test   %rax,%rax
js     11ba <get_master_pty+0x5a>
mov    %ebx,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     11b3 <get_master_pty+0x53>
mov    %rax,%rdi
call   1080 <strdup@plt>
mov    %rax,(%r14)
jmp    11bf <get_master_pty+0x5f>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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