#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ grantpt(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ unlockpt(int);
int pts_master_open() {
  int fd;
  struct termios attr;
  if ((fd = open("/dev/ptmx", O_RDWR)) == -1) {
    perror("Error abriendo /dev/ptmx");
    exit(1);
  }
  grantpt(fd);
  unlockpt(fd);
  tcgetattr(fd, &attr);
  cfmakeraw(&attr);
  tcsetattr(fd, TCSAFLUSH, &attr);
  return (fd);
}


x86 assembly (clang, -O0):
<pts_master_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11bf <pts_master_open+0x3f>
lea    0xe50(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%edi
call   1090 <grantpt@plt>
mov    -0x4(%rbp),%edi
call   1030 <unlockpt@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
lea    -0x8(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    -0x4(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pts_master_open>:
push   %r14
push   %rbx
push   %rax
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e0 <pts_master_open+0x60>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <grantpt@plt>
mov    %ebx,%edi
call   1030 <unlockpt@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    %r14,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe19(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"Error abriendo /dev/ptmx\00"
@TCSAFLUSH = global i32 0
define i32 @pts_master_open() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = load i32, i32* @O_RDWR
%4 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
store i32 %4, i32* %1
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %1
%11 = call i32 @grantpt(i32 %10)
%12 = load i32, i32* %1
%13 = call i32 @unlockpt(i32 %12)
%14 = load i32, i32* %1
%15 = call i32 @tcgetattr(i32 %14, %struct.termios* %2)
%16 = call i32 @cfmakeraw(%struct.termios* %2)
%17 = load i32, i32* %1
%18 = load i32, i32* @TCSAFLUSH
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %2)
%20 = load i32, i32* %1
ret i32 %20
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"Error abriendo /dev/ptmx\00"
@TCSAFLUSH = global i32 0
define i32 @pts_master_open() {
%1 = alloca %struct.termios
%2 = bitcast %struct.termios* %1 to i8*
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = tail call i32 @grantpt(i32 %4)
%11 = tail call i32 @unlockpt(i32 %4)
%12 = call i32 @tcgetattr(i32 %4, %struct.termios* %1)
%13 = call i32 @cfmakeraw(%struct.termios* %1)
%14 = load i32, i32* @TCSAFLUSH
%15 = call i32 @tcsetattr(i32 %4, i32 %14, %struct.termios* %1)
ret i32 %4
}