#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*/ close(int);
int getpt();
scalar_t__ grantpt(int);
scalar_t__ ptsname(int);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ unlockpt(int);
int getpty(char *line) {
  int p;
  p = getpt();
  if (p < 0) {
    ;
    close(p);
    return -1;
  }
  if (grantpt(p) < 0 || unlockpt(p) < 0) {
    ;
    close(p);
    return -1;
  };
  strcpy(line, (const char *)ptsname(p));
  return (p);
}


x86 assembly (clang, -O0):
<getpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <getpt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1194 <getpty+0x34>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <getpty+0x8e>
mov    -0x14(%rbp),%edi
call   1080 <grantpt@plt>
cmp    $0x0,%rax
jl     11b8 <getpty+0x58>
mov    -0x14(%rbp),%edi
call   1070 <unlockpt@plt>
cmp    $0x0,%rax
jge    11cc <getpty+0x6c>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <getpty+0x8e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <ptsname@plt>
mov    -0x20(%rbp),%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpty>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1060 <getpt@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11a0 <getpty+0x40>
mov    %ebx,%edi
call   1080 <grantpt@plt>
test   %rax,%rax
js     11a0 <getpty+0x40>
mov    %ebx,%edi
call   1070 <unlockpt@plt>
test   %rax,%rax
js     11a0 <getpty+0x40>
mov    %ebx,%edi
call   1030 <ptsname@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
jmp    11ac <getpty+0x4c>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @getpty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = call i32 (...) @getpt()
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* %4
%10 = call i32 @close(i32 %9)
store i32 -1, i32* %2
br label %29
11: ; preds = %1
%12 = load i32, i32* %4
%13 = call i64 @grantpt(i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* %4
%17 = call i64 @unlockpt(i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15, %11
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %2
br label %29
22: ; preds = %15
%23 = load i8*, i8** %3
%24 = load i32, i32* %4
%25 = call i64 @ptsname(i32 %24)
%26 = inttoptr i64 %25 to i8*
%27 = call i32 @strcpy(i8* %23, i8* %26)
%28 = load i32, i32* %4
store i32 %28, i32* %2
br label %29
29: ; preds = %22, %19, %8
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
define i32 @getpty(i8* %0) {
%2 = tail call i32 (...) @getpt()
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @close(i32 %2)
br label %18
6: ; preds = %1
%7 = tail call i64 @grantpt(i32 %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = tail call i64 @unlockpt(i32 %2)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9, %6
%13 = tail call i32 @close(i32 %2)
br label %18
14: ; preds = %9
%15 = tail call i64 @ptsname(i32 %2)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @strcpy(i8* %0, i8* %16)
br label %18
18: ; preds = %14, %12, %4
%19 = phi i32 [ -1, %4 ], [ -1, %12 ], [ %2, %14 ]
ret i32 %19
}