#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 O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TIOCSWINSZ;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct winsize *);
int open(char const *, int);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int _internal_get_tty(int *slave, const char *path, struct termios *termp,
                      struct winsize *winp, int ctty) {
  if (path != NULL) {
    if ((*slave = open(path, O_RDWR | O_NOCTTY)) == -1)
      return -1;
  }
  if (termp != NULL)
    tcsetattr(*slave, TCSAFLUSH, termp);
  if (winp != NULL)
    ioctl(*slave, TIOCSWINSZ, winp);
  return 0;
}


x86 assembly (clang, -O0):
<_internal_get_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1192 <_internal_get_tty+0x62>
mov    -0x18(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    -0x10(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0xffffffff,%eax
jne    118d <_internal_get_tty+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <_internal_get_tty+0xaf>
jmp    1192 <_internal_get_tty+0x62>
cmpq   $0x0,-0x20(%rbp)
je     11b5 <_internal_get_tty+0x85>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1040 <tcsetattr@plt>
cmpq   $0x0,-0x28(%rbp)
je     11d8 <_internal_get_tty+0xa8>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_internal_get_tty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rdi,%rbx
test   %rsi,%rsi
je     1168 <_internal_get_tty+0x38>
mov    0x2e9c(%rip),%rcx
mov    0x2e75(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rsi,%rdi
mov    %eax,%esi
call   1050 <open@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     119c <_internal_get_tty+0x6c>
test   %r15,%r15
je     1180 <_internal_get_tty+0x50>
mov    (%rbx),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdx
call   1040 <tcsetattr@plt>
xor    %ebp,%ebp
test   %r14,%r14
je     11a1 <_internal_get_tty+0x71>
mov    (%rbx),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdx
call   1030 <ioctl@plt>
jmp    11a1 <_internal_get_tty+0x71>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSAFLUSH = global i32 0
@TIOCSWINSZ = global i32 0
define i32 @_internal_get_tty(i32* %0, i8* %1, %struct.termios* %2, %struct.winsize* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca %struct.termios*
%10 = alloca %struct.winsize*
%11 = alloca i32
store i32* %0, i32** %7
store i8* %1, i8** %8
store %struct.termios* %2, %struct.termios** %9
store %struct.winsize* %3, %struct.winsize** %10
store i32 %4, i32* %11
%12 = load i8*, i8** %8
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %24
14: ; preds = %5
%15 = load i8*, i8** %8
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* @O_NOCTTY
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %15, i32 %18)
%20 = load i32*, i32** %7
store i32 %19, i32* %20
%21 = icmp eq i32 %19, -1
br i1 %21, label %22, label %23
22: ; preds = %14
store i32 -1, i32* %6
br label %43
23: ; preds = %14
br label %24
24: ; preds = %23, %5
%25 = load %struct.termios*, %struct.termios** %9
%26 = icmp ne %struct.termios* %25, null
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i32*, i32** %7
%29 = load i32, i32* %28
%30 = load i32, i32* @TCSAFLUSH
%31 = load %struct.termios*, %struct.termios** %9
%32 = call i32 @tcsetattr(i32 %29, i32 %30, %struct.termios* %31)
br label %33
33: ; preds = %27, %24
%34 = load %struct.winsize*, %struct.winsize** %10
%35 = icmp ne %struct.winsize* %34, null
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = load i32*, i32** %7
%38 = load i32, i32* %37
%39 = load i32, i32* @TIOCSWINSZ
%40 = load %struct.winsize*, %struct.winsize** %10
%41 = call i32 @ioctl(i32 %38, i32 %39, %struct.winsize* %40)
br label %42
42: ; preds = %36, %33
store i32 0, i32* %6
br label %43
43: ; preds = %42, %22
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.winsize = type { i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCSAFLUSH = global i32 0
@TIOCSWINSZ = global i32 0
define i32 @_internal_get_tty(i32* nocapture %0, i8* %1, %struct.termios* %2, %struct.winsize* %3, i32 %4) {
%6 = icmp eq i8* %1, null
br i1 %6, label %13, label %7
7: ; preds = %5
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* %1, i32 %10)
store i32 %11, i32* %0
%12 = icmp eq i32 %11, -1
br i1 %12, label %25, label %13
13: ; preds = %7, %5
%14 = icmp eq %struct.termios* %2, null
br i1 %14, label %19, label %15
15: ; preds = %13
%16 = load i32, i32* %0
%17 = load i32, i32* @TCSAFLUSH
%18 = tail call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %2)
br label %19
19: ; preds = %15, %13
%20 = icmp eq %struct.winsize* %3, null
br i1 %20, label %25, label %21
21: ; preds = %19
%22 = load i32, i32* %0
%23 = load i32, i32* @TIOCSWINSZ
%24 = tail call i32 @ioctl(i32 %22, i32 %23, %struct.winsize* %3)
br label %25
25: ; preds = %19, %21, %7
%26 = phi i32 [ -1, %7 ], [ 0, %21 ], [ 0, %19 ]
ret i32 %26
}