#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 dummy;
};

/* Variables and functions */
int O_NOCTTY;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCSADRAIN;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ exit(int);
scalar_t__ grantpt(int);
int /*<<< orphan*/ perror(char *);
int posix_openpt(int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
scalar_t__ unlockpt(int);
int newpty() {
  int fd;
  struct termios tio;
  if ((fd = posix_openpt(O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) {
    perror("Can't open new pty");
    exit(1);
  }
  if (tcgetattr(fd, &tio)) {
    perror("Can't get PTY communication parameters");
    exit(1);
  }
  cfmakeraw(&tio);
  if (tcsetattr(fd, TCSADRAIN, &tio)) {
    perror("Can't set PTY communication parameters");
    exit(1);
  }
  if (grantpt(fd)) {
    perror("Can't grant PTY access");
    exit(1);
  }
  if (unlockpt(fd)) {
    perror("Can't unlock PTY slave");
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<newpty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e38(%rip),%rax
or     (%rax),%edi
mov    0x2e3f(%rip),%rax
or     (%rax),%edi
call   10a0 <posix_openpt@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11ca <newpty+0x4a>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     11f6 <newpty+0x76>
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0x8(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    -0x4(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     1234 <newpty+0xb4>
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
call   1080 <grantpt@plt>
cmp    $0x0,%rax
je     125c <newpty+0xdc>
lea    0xe14(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
call   1030 <unlockpt@plt>
cmp    $0x0,%rax
je     1284 <newpty+0x104>
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newpty>:
push   %r14
push   %rbx
push   %rax
mov    0x2e5d(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
mov    0x2e43(%rip),%rax
or     (%rax),%edi
call   10a0 <posix_openpt@plt>
cmp    $0xffffffff,%eax
je     11ff <newpty+0x7f>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
jne    1208 <newpty+0x88>
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
test   %rax,%rax
jne    1211 <newpty+0x91>
mov    %ebx,%edi
call   1080 <grantpt@plt>
test   %rax,%rax
jne    121a <newpty+0x9a>
mov    %ebx,%edi
call   1030 <unlockpt@plt>
test   %rax,%rax
jne    1223 <newpty+0xa3>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdfa(%rip),%rdi
jmp    122a <newpty+0xaa>
lea    0xe04(%rip),%rdi
jmp    122a <newpty+0xaa>
lea    0xe22(%rip),%rdi
jmp    122a <newpty+0xaa>
lea    0xe40(%rip),%rdi
jmp    122a <newpty+0xaa>
lea    0xe4e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [19 x i8] c"Can't open new pty\00"
@.str.1 = [39 x i8] c"Can't get PTY communication parameters\00"
@TCSADRAIN = global i32 0
@.str.2 = [39 x i8] c"Can't set PTY communication parameters\00"
@.str.3 = [23 x i8] c"Can't grant PTY access\00"
@.str.4 = [23 x i8] c"Can't unlock PTY slave\00"
define i32 @newpty() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %3, %4
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %5, %6
%8 = call i32 @posix_openpt(i32 %7)
store i32 %8, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %1
%15 = call i64 @tcgetattr(i32 %14, %struct.termios* %2)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = call i32 @cfmakeraw(%struct.termios* %2)
%22 = load i32, i32* %1
%23 = load i32, i32* @TCSADRAIN
%24 = call i64 @tcsetattr(i32 %22, i32 %23, %struct.termios* %2)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = load i32, i32* %1
%31 = call i64 @grantpt(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = load i32, i32* %1
%38 = call i64 @unlockpt(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = load i32, i32* %1
ret i32 %44
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [19 x i8] c"Can't open new pty\00"
@.str.1 = [39 x i8] c"Can't get PTY communication parameters\00"
@TCSADRAIN = global i32 0
@.str.2 = [39 x i8] c"Can't set PTY communication parameters\00"
@.str.3 = [23 x i8] c"Can't grant PTY access\00"
@.str.4 = [23 x i8] c"Can't unlock PTY slave\00"
define i32 @newpty() {
%1 = alloca %struct.termios
%2 = bitcast %struct.termios* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_NOCTTY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %5, %6
%8 = tail call i32 @posix_openpt(i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i64 @tcgetattr(i32 %8, %struct.termios* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = call i32 @cfmakeraw(%struct.termios* %1)
%21 = load i32, i32* @TCSADRAIN
%22 = call i64 @tcsetattr(i32 %8, i32 %21, %struct.termios* %1)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = call i64 @grantpt(i32 %8)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = call i64 @unlockpt(i32 %8)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %33
ret i32 %8
}