#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 termios {
  int c_lflag;
  int *c_cc;
};

/* Variables and functions */
int ECHO;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int O_NONBLOCK;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ grantpt(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ unlockpt(int);
int createPty() {
  int ptm = open("/dev/ptmx", O_RDWR);
  int flags = fcntl(ptm, F_GETFL, 0);
  fcntl(ptm, F_SETFL, flags | O_NONBLOCK);
  struct termios ptm_attr;
  tcgetattr(ptm, &ptm_attr);
  ptm_attr.c_lflag &= (~(ICANON | ECHO));
  ptm_attr.c_cc[VTIME] = 0;
  ptm_attr.c_cc[VMIN] = 1;
  tcsetattr(ptm, TCSANOW, &ptm_attr);
  grantpt(ptm);
  unlockpt(ptm);
  return ptm;
}


x86 assembly (clang, -O0):
<createPty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2e34(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    0x2e01(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x4(%rbp),%edi
call   1080 <grantpt@plt>
mov    -0x4(%rbp),%edi
call   1040 <unlockpt@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createPty>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e21(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
lea    0x8(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2ddf(%rip),%rax
mov    0x2e08(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x10(%rsp),%rax
mov    0x2dca(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%edi
call   1080 <grantpt@plt>
mov    %ebx,%edi
call   1040 <unlockpt@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @createPty() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.termios
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = load i32, i32* @F_GETFL
%8 = call i32 @fcntl(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* %1
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* %2
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = call i32 @fcntl(i32 %9, i32 %10, i32 %13)
%15 = load i32, i32* %1
%16 = call i32 @tcgetattr(i32 %15, %struct.termios* %3)
%17 = load i32, i32* @ICANON
%18 = load i32, i32* @ECHO
%19 = or i32 %17, %18
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%22 = load i32, i32* %21
%23 = and i32 %22, %20
store i32 %23, i32* %21
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%25 = load i32*, i32** %24
%26 = load i64, i64* @VTIME
%27 = getelementptr inbounds i32, i32* %25, i64 %26
store i32 0, i32* %27
%28 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%29 = load i32*, i32** %28
%30 = load i64, i64* @VMIN
%31 = getelementptr inbounds i32, i32* %29, i64 %30
store i32 1, i32* %31
%32 = load i32, i32* %1
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %3)
%35 = load i32, i32* %1
%36 = call i32 @grantpt(i32 %35)
%37 = load i32, i32* %1
%38 = call i32 @unlockpt(i32 %37)
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @createPty() {
%1 = alloca %struct.termios
%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 = load i32, i32* @F_GETFL
%5 = tail call i32 @fcntl(i32 %3, i32 %4, i32 0)
%6 = load i32, i32* @F_SETFL
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %7, %5
%9 = tail call i32 @fcntl(i32 %3, i32 %6, i32 %8)
%10 = bitcast %struct.termios* %1 to i8*
%11 = call i32 @tcgetattr(i32 %3, %struct.termios* %1)
%12 = load i32, i32* @ICANON
%13 = load i32, i32* @ECHO
%14 = or i32 %13, %12
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%20 = load i32*, i32** %19
%21 = load i64, i64* @VTIME
%22 = getelementptr inbounds i32, i32* %20, i64 %21
store i32 0, i32* %22
%23 = load i64, i64* @VMIN
%24 = getelementptr inbounds i32, i32* %20, i64 %23
store i32 1, i32* %24
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %3, i32 %25, %struct.termios* %1)
%27 = call i32 @grantpt(i32 %3)
%28 = call i32 @unlockpt(i32 %3)
ret i32 %3
}