#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*/ TCSANOW;
int ppty(int *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios const *);
int openpty(int *amaster, int *aslave, char *name, const struct termios *termp,
            const struct winsize *winp) {
  int fds[2];
  int ret;
  ret = ppty(fds);
  *amaster = fds[0];
  *aslave = fds[1];
  if (!ret && termp) {
    tcsetattr(fds[0], TCSANOW, termp);
  }
  return ret;
}


x86 assembly (clang, -O0):
<openpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1030 <ppty@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x34(%rbp)
jne    1184 <openpty+0x64>
cmpq   $0x0,-0x20(%rbp)
je     1184 <openpty+0x64>
mov    -0x30(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openpty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1030 <ppty@plt>
mov    %eax,%ebp
mov    (%rsp),%edi
mov    %edi,(%rbx)
mov    0x4(%rsp),%eax
mov    %eax,(%r15)
test   %r14,%r14
je     1160 <openpty+0x40>
test   %ebp,%ebp
jne    1160 <openpty+0x40>
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
call   1040 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@TCSANOW = 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 i8*
%9 = alloca %struct.termios*
%10 = alloca %struct.winsize*
%11 = alloca [2 x i32]
%12 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store %struct.termios* %3, %struct.termios** %9
store %struct.winsize* %4, %struct.winsize** %10
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%14 = call i32 @ppty(i32* %13)
store i32 %14, i32* %12
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%16 = load i32, i32* %15
%17 = load i32*, i32** %6
store i32 %16, i32* %17
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32*, i32** %7
store i32 %19, i32* %20
%21 = load i32, i32* %12
%22 = icmp ne i32 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %5
%24 = load %struct.termios*, %struct.termios** %9
%25 = icmp ne %struct.termios* %24, null
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%28 = load i32, i32* %27
%29 = load i32, i32* @TCSANOW
%30 = load %struct.termios*, %struct.termios** %9
%31 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %30)
br label %32
32: ; preds = %26, %23, %5
%33 = load i32, i32* %12
ret i32 %33
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@TCSANOW = global i32 0
define i32 @openpty(i32* nocapture writeonly %0, i32* nocapture writeonly %1, i8* nocapture readnone %2, %struct.termios* %3, %struct.winsize* nocapture readnone %4) {
%6 = alloca [2 x i32]
%7 = bitcast [2 x i32]* %6 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%9 = call i32 @ppty(i32* %8)
%10 = load i32, i32* %8
store i32 %10, i32* %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%12 = load i32, i32* %11
store i32 %12, i32* %1
%13 = icmp eq i32 %9, 0
%14 = icmp ne %struct.termios* %3, null
%15 = and i1 %14, %13
br i1 %15, label %16, label %19
16: ; preds = %5
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %10, i32 %17, %struct.termios* %3)
br label %19
19: ; preds = %16, %5
ret i32 %9
}