#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*/ F_SETOWN;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ grantpt(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ ptsname(int);
int /*<<< orphan*/ remove(char const *);
scalar_t__ symlink(char *, char const *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ unlockpt(int);
int ptym_open(const char *local_name) {
  char *slave_name = NULL;
  int fd;
  struct termios termios;
  fd = open("/dev/ptmx", O_RDWR);
  if (fd < 0)
    return -1;
  slave_name = (char *)ptsname(fd);
  if (slave_name == NULL) {
    close(fd);
    return -1;
  }
  grantpt(fd);
  unlockpt(fd);
  remove(local_name);
  if (fcntl(fd, F_SETOWN, getpid()) < 0) {
    close(fd);
    return -1;
  }
  if (symlink(slave_name, local_name) < 0) {
    close(fd);
    return -1;
  }
  if (tcgetattr(fd, &termios) < 0) {
    close(fd);
    return -1;
  }
  cfmakeraw(&termios);
  if (tcsetattr(fd, TCSANOW, &termios) < 0) {
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<ptym_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1202 <ptym_open+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <ptym_open+0x161>
mov    -0x1c(%rbp),%edi
call   1030 <ptsname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122d <ptym_open+0x6d>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <ptym_open+0x161>
mov    -0x1c(%rbp),%edi
call   10e0 <grantpt@plt>
mov    -0x1c(%rbp),%edi
call   1090 <unlockpt@plt>
mov    -0x10(%rbp),%rdi
call   1040 <remove@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x28(%rbp),%edi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
call   1050 <fcntl@plt>
cmp    $0x0,%rax
jge    128a <ptym_open+0xca>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <ptym_open+0x161>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <symlink@plt>
cmp    $0x0,%rax
jge    12b5 <ptym_open+0xf5>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <ptym_open+0x161>
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10b0 <tcgetattr@plt>
cmp    $0x0,%rax
jge    12df <ptym_open+0x11f>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <ptym_open+0x161>
lea    -0x20(%rbp),%rdi
call   10a0 <cfmakeraw@plt>
mov    -0x1c(%rbp),%edi
mov    0x2ce6(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   10c0 <tcsetattr@plt>
cmp    $0x0,%rax
jge    131b <ptym_open+0x15b>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <ptym_open+0x161>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ptym_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r15
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    0xe21(%rip),%rdi
call   10d0 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1291 <ptym_open+0xd1>
mov    %eax,%ebp
mov    %eax,%edi
call   1030 <ptsname@plt>
test   %rax,%rax
je     128a <ptym_open+0xca>
mov    %rax,%rbx
mov    %ebp,%edi
call   10e0 <grantpt@plt>
mov    %ebp,%edi
call   1090 <unlockpt@plt>
mov    %r15,%rdi
call   1040 <remove@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%r12d
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %ebp,%edi
mov    %r12d,%esi
mov    %eax,%edx
call   1050 <fcntl@plt>
test   %rax,%rax
js     128a <ptym_open+0xca>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <symlink@plt>
test   %rax,%rax
js     128a <ptym_open+0xca>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   10b0 <tcgetattr@plt>
test   %rax,%rax
js     128a <ptym_open+0xca>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <cfmakeraw@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   10c0 <tcsetattr@plt>
test   %rax,%rax
js     128a <ptym_open+0xca>
mov    %ebp,%r14d
jmp    1291 <ptym_open+0xd1>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@F_SETOWN = global i32 0
@TCSANOW = global i32 0
define i32 @ptym_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
store i8* %0, i8** %3
store i8* null, i8** %4
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %62
12: ; preds = %1
%13 = load i32, i32* %5
%14 = call i64 @ptsname(i32 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %12
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
store i32 -1, i32* %2
br label %62
21: ; preds = %12
%22 = load i32, i32* %5
%23 = call i32 @grantpt(i32 %22)
%24 = load i32, i32* %5
%25 = call i32 @unlockpt(i32 %24)
%26 = load i8*, i8** %3
%27 = call i32 @remove(i8* %26)
%28 = load i32, i32* %5
%29 = load i32, i32* @F_SETOWN
%30 = call i32 (...) @getpid()
%31 = call i64 @fcntl(i32 %28, i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %21
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %2
br label %62
36: ; preds = %21
%37 = load i8*, i8** %4
%38 = load i8*, i8** %3
%39 = call i64 @symlink(i8* %37, i8* %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32, i32* %5
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %2
br label %62
44: ; preds = %36
%45 = load i32, i32* %5
%46 = call i64 @tcgetattr(i32 %45, %struct.termios* %6)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %2
br label %62
51: ; preds = %44
%52 = call i32 @cfmakeraw(%struct.termios* %6)
%53 = load i32, i32* %5
%54 = load i32, i32* @TCSANOW
%55 = call i64 @tcsetattr(i32 %53, i32 %54, %struct.termios* %6)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = load i32, i32* %5
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %2
br label %62
60: ; preds = %51
%61 = load i32, i32* %5
store i32 %61, i32* %2
br label %62
62: ; preds = %60, %57, %48, %41, %33, %18, %11
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@F_SETOWN = global i32 0
@TCSANOW = global i32 0
define i32 @ptym_open(i8* %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %40, label %7
7: ; preds = %1
%8 = tail call i64 @ptsname(i32 %5)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %8, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @close(i32 %5)
br label %40
13: ; preds = %7
%14 = tail call i32 @grantpt(i32 %5)
%15 = tail call i32 @unlockpt(i32 %5)
%16 = tail call i32 @remove(i8* %0)
%17 = load i32, i32* @F_SETOWN
%18 = tail call i32 (...) @getpid()
%19 = tail call i64 @fcntl(i32 %5, i32 %17, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @close(i32 %5)
br label %40
23: ; preds = %13
%24 = tail call i64 @symlink(i8* %9, i8* %0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = tail call i32 @close(i32 %5)
br label %40
28: ; preds = %23
%29 = call i64 @tcgetattr(i32 %5, %struct.termios* %2)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @close(i32 %5)
br label %40
33: ; preds = %28
%34 = call i32 @cfmakeraw(%struct.termios* %2)
%35 = load i32, i32* @TCSANOW
%36 = call i64 @tcsetattr(i32 %5, i32 %35, %struct.termios* %2)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 @close(i32 %5)
br label %40
40: ; preds = %33, %1, %38, %31, %26, %21, %11
%41 = phi i32 [ -1, %11 ], [ -1, %21 ], [ -1, %26 ], [ -1, %31 ], [ -1, %38 ], [ -1, %1 ], [ %5, %33 ]
ret i32 %41
}