#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 FILENAME_MAX;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCGWINSZ;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ exit(int);
scalar_t__ grantpt(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct winsize *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ ptsname(int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
scalar_t__ unlockpt(int);
int pty_get(char *name) {
  int fd;
  fd = open("/dev/ptmx", O_RDWR);
  if (fd < 0) {
    perror("/dev/ptmx: open");
    exit(1);
  }
  if (grantpt(fd) < 0) {
    perror("grantpt");
    exit(1);
  }
  if (unlockpt(fd) < 0) {
    perror("unlockpt");
    exit(1);
  }
  name[FILENAME_MAX - 1] = '\0';
  strncpy(name, ptsname(fd), FILENAME_MAX - 1);
  {
    struct winsize ws;
    struct termios ts;
    if (!ioctl(0, TIOCGWINSZ, &ws))
      ioctl(fd, TIOCSWINSZ, &ws);
    if (!tcgetattr(0, &ts))
      tcsetattr(fd, TCSANOW, &ts);
  }
  return fd;
}


x86 assembly (clang, -O0):
<pty_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e4 <pty_get+0x44>
lea    0xe35(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0xc(%rbp),%edi
call   10b0 <grantpt@plt>
cmp    $0x0,%rax
jge    120c <pty_get+0x6c>
lea    0xe1d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0xc(%rbp),%edi
call   1060 <unlockpt@plt>
cmp    $0x0,%rax
jge    1234 <pty_get+0x94>
lea    0xdfd(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    0x2d99(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <ptsname@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1040 <strncpy@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%eax
jne    12a4 <pty_get+0x104>
mov    -0xc(%rbp),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%eax
jne    12cd <pty_get+0x12d>
mov    -0xc(%rbp),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_get>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
lea    0xe44(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     126d <pty_get+0xcd>
mov    %eax,%ebx
mov    %eax,%edi
call   10b0 <grantpt@plt>
test   %rax,%rax
js     1276 <pty_get+0xd6>
mov    %ebx,%edi
call   1060 <unlockpt@plt>
test   %rax,%rax
js     127f <pty_get+0xdf>
mov    0x2de6(%rip),%r15
movslq (%r15),%rax
movb   $0x0,-0x1(%rax,%r14,1)
mov    %ebx,%edi
call   1030 <ptsname@plt>
mov    (%r15),%edx
add    $0xffffffff,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <strncpy@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1050 <ioctl@plt>
test   %eax,%eax
jne    1240 <pty_get+0xa0>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    %rsp,%rsi
xor    %edi,%edi
call   1070 <tcgetattr@plt>
test   %eax,%eax
jne    1261 <pty_get+0xc1>
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1080 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd96(%rip),%rdi
jmp    1286 <pty_get+0xe6>
lea    0xd9d(%rip),%rdi
jmp    1286 <pty_get+0xe6>
lea    0xd9c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.winsize = type { i32 }
%struct.termios = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"/dev/ptmx: open\00"
@.str.2 = [8 x i8] c"grantpt\00"
@.str.3 = [9 x i8] c"unlockpt\00"
@FILENAME_MAX = global i32 0
@TIOCGWINSZ = global i32 0
@TIOCSWINSZ = global i32 0
@TCSANOW = global i32 0
define i32 @pty_get(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.winsize
%5 = alloca %struct.termios
store i8* %0, i8** %2
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i64 @grantpt(i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i32, i32* %3
%22 = call i64 @unlockpt(i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = load i8*, i8** %2
%29 = load i32, i32* @FILENAME_MAX
%30 = sub nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %2
%34 = load i32, i32* %3
%35 = call i32 @ptsname(i32 %34)
%36 = load i32, i32* @FILENAME_MAX
%37 = sub nsw i32 %36, 1
%38 = call i32 @strncpy(i8* %33, i32 %35, i32 %37)
%39 = load i32, i32* @TIOCGWINSZ
%40 = call i32 @ioctl(i32 0, i32 %39, %struct.winsize* %4)
%41 = icmp ne i32 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %27
%43 = load i32, i32* %3
%44 = load i32, i32* @TIOCSWINSZ
%45 = call i32 @ioctl(i32 %43, i32 %44, %struct.winsize* %4)
br label %46
46: ; preds = %42, %27
%47 = call i32 @tcgetattr(i32 0, %struct.termios* %5)
%48 = icmp ne i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %46
%50 = load i32, i32* %3
%51 = load i32, i32* @TCSANOW
%52 = call i32 @tcsetattr(i32 %50, i32 %51, %struct.termios* %5)
br label %53
53: ; preds = %49, %46
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.winsize = type { i32 }
%struct.termios = type { i32 }
@.str = [10 x i8] c"/dev/ptmx\00"
@O_RDWR = global i32 0
@.str.1 = [16 x i8] c"/dev/ptmx: open\00"
@.str.2 = [8 x i8] c"grantpt\00"
@.str.3 = [9 x i8] c"unlockpt\00"
@FILENAME_MAX = global i32 0
@TIOCGWINSZ = global i32 0
@TIOCSWINSZ = global i32 0
@TCSANOW = global i32 0
define i32 @pty_get(i8* %0) {
%2 = alloca %struct.winsize
%3 = alloca %struct.termios
%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 %7, label %10
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i64 @grantpt(i32 %5)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i64 @unlockpt(i32 %5)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = load i32, i32* @FILENAME_MAX
%24 = add nsw i32 %23, -1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @ptsname(i32 %5)
%28 = load i32, i32* @FILENAME_MAX
%29 = add nsw i32 %28, -1
%30 = tail call i32 @strncpy(i8* %0, i32 %27, i32 %29)
%31 = bitcast %struct.winsize* %2 to i8*
%32 = bitcast %struct.termios* %3 to i8*
%33 = load i32, i32* @TIOCGWINSZ
%34 = call i32 @ioctl(i32 0, i32 %33, %struct.winsize* %2)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %22
%37 = load i32, i32* @TIOCSWINSZ
%38 = call i32 @ioctl(i32 %5, i32 %37, %struct.winsize* %2)
br label %39
39: ; preds = %36, %22
%40 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i32, i32* @TCSANOW
%44 = call i32 @tcsetattr(i32 %5, i32 %43, %struct.termios* %3)
br label %45
45: ; preds = %42, %39
ret i32 %5
}