#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 */
int /*<<< orphan*/ TCSETAF;
int /*<<< orphan*/ close(int);
int ptym_open(char *);
int ptys_open(int, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int openpty(int *amaster, int *aslave, char *name, struct termios *termp,
            struct winsize *winp) {
  char line[20];
  *amaster = ptym_open(line);
  if (*amaster < 0)
    return -1;
  *aslave = ptys_open(*amaster, line);
  if (*aslave < 0) {
    close(*amaster);
    return -1;
  }
  if (name)
    strcpy(name, line);
  if (termp)
    (void)tcsetattr(*aslave, TCSETAF, termp);
  return 0;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x50(%rbp),%rdi
call   1060 <ptym_open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1196 <openpty+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <openpty+0xc3>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
call   1070 <ptys_open@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    11d1 <openpty+0x81>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <openpty+0xc3>
cmpq   $0x0,-0x20(%rbp)
je     11e9 <openpty+0x99>
mov    -0x20(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x28(%rbp)
je     120c <openpty+0xbc>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1050 <tcsetattr@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1060 <ptym_open@plt>
mov    %eax,(%rbx)
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11c2 <openpty+0x72>
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <ptys_open@plt>
mov    %eax,(%r12)
test   %eax,%eax
js     11bb <openpty+0x6b>
test   %r15,%r15
je     119d <openpty+0x4d>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
test   %r14,%r14
je     11c2 <openpty+0x72>
mov    (%r12),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
jmp    11c2 <openpty+0x72>
mov    (%rbx),%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@TCSETAF = global i32 0
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 [20 x i8]
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
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%14 = call i32 @ptym_open(i8* %13)
%15 = load i32*, i32** %7
store i32 %14, i32* %15
%16 = load i32*, i32** %7
%17 = load i32, i32* %16
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %5
store i32 -1, i32* %6
br label %50
20: ; preds = %5
%21 = load i32*, i32** %7
%22 = load i32, i32* %21
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%24 = call i32 @ptys_open(i32 %22, i8* %23)
%25 = load i32*, i32** %8
store i32 %24, i32* %25
%26 = load i32*, i32** %8
%27 = load i32, i32* %26
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %20
%30 = load i32*, i32** %7
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %6
br label %50
33: ; preds = %20
%34 = load i8*, i8** %9
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i8*, i8** %9
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %12, i64 0, i64 0
%39 = call i32 @strcpy(i8* %37, i8* %38)
br label %40
40: ; preds = %36, %33
%41 = load %struct.termios*, %struct.termios** %10
%42 = icmp ne %struct.termios* %41, null
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = load i32*, i32** %8
%45 = load i32, i32* %44
%46 = load i32, i32* @TCSETAF
%47 = load %struct.termios*, %struct.termios** %10
%48 = call i32 @tcsetattr(i32 %45, i32 %46, %struct.termios* %47)
br label %49
49: ; preds = %43, %40
store i32 0, i32* %6
br label %50
50: ; preds = %49, %29, %19
%51 = load i32, i32* %6
ret i32 %51
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@TCSETAF = global i32 0
define i32 @openpty(i32* nocapture %0, i32* nocapture %1, i8* %2, %struct.termios* %3, %struct.winsize* nocapture readnone %4) {
%6 = alloca [20 x i8]
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%8 = call i32 @ptym_open(i8* %7)
store i32 %8, i32* %0
%9 = icmp slt i32 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %5
%11 = call i32 @ptys_open(i32 %8, i8* %7)
store i32 %11, i32* %1
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* %0
%15 = call i32 @close(i32 %14)
br label %26
16: ; preds = %10
%17 = icmp eq i8* %2, null
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = call i32 @strcpy(i8* %2, i8* %7)
br label %20
20: ; preds = %18, %16
%21 = icmp eq %struct.termios* %3, null
br i1 %21, label %26, label %22
22: ; preds = %20
%23 = load i32, i32* %1
%24 = load i32, i32* @TCSETAF
%25 = call i32 @tcsetattr(i32 %23, i32 %24, %struct.termios* %3)
br label %26
26: ; preds = %20, %22, %5, %13
%27 = phi i32 [ -1, %13 ], [ -1, %5 ], [ 0, %22 ], [ 0, %20 ]
ret i32 %27
}