#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 c_iflag;
  int *c_cc;
  int /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int ECHO;
int /*<<< orphan*/ ICANON;
int ICRNL;
int INLCR;
int /*<<< orphan*/ TCSADRAIN;
size_t VEOF;
size_t VMIN;
size_t VTIME;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int pty_setup_slave_term(int slave, int raw) {
  struct termios slave_termios;
  while (-1 == tcgetattr(slave, &slave_termios)) {
    return -1;
  }
  slave_termios.c_lflag = 0;
  if (raw == 0)
    slave_termios.c_lflag |= ICANON;
  slave_termios.c_iflag &= ~(ECHO | INLCR | ICRNL);
  slave_termios.c_cc[VEOF] = 4;
  slave_termios.c_cc[VMIN] = 1;
  slave_termios.c_cc[VTIME] = 0;
  while (-1 == tcsetattr(slave, TCSADRAIN, &slave_termios)) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pty_setup_slave_term>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <tcgetattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1155 <pty_setup_slave_term+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <pty_setup_slave_term+0xef>
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1175 <pty_setup_slave_term+0x55>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    0x2e5b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x4,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2e17(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <tcsetattr@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1208 <pty_setup_slave_term+0xe8>
movl   $0xffffffff,-0x4(%rbp)
jmp    120f <pty_setup_slave_term+0xef>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_setup_slave_term>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    %rsp,%rsi
call   1030 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     11cd <pty_setup_slave_term+0xad>
movl   $0x0,0x10(%rsp)
test   %ebp,%ebp
jne    1154 <pty_setup_slave_term+0x34>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e7d(%rip),%rax
mov    0x2e7e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e7b(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e20(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x4,(%rax,%rcx,4)
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e16(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <tcsetattr@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    11d2 <pty_setup_slave_term+0xb2>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@INLCR = global i32 0
@ICRNL = global i32 0
@VEOF = global i64 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSADRAIN = global i32 0
define i32 @pty_setup_slave_term(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
br label %7
7: ; preds = %2
%8 = load i32, i32* %4
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %6)
%10 = icmp eq i32 -1, %9
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 -1, i32* %3
br label %50
12: ; preds = %7
%13 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
store i32 0, i32* %13
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @ICANON
%18 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 2
%19 = load i32, i32* %18
%20 = or i32 %19, %17
store i32 %20, i32* %18
br label %21
21: ; preds = %16, %12
%22 = load i32, i32* @ECHO
%23 = load i32, i32* @INLCR
%24 = or i32 %22, %23
%25 = load i32, i32* @ICRNL
%26 = or i32 %24, %25
%27 = xor i32 %26, -1
%28 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = and i32 %29, %27
store i32 %30, i32* %28
%31 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%32 = load i32*, i32** %31
%33 = load i64, i64* @VEOF
%34 = getelementptr inbounds i32, i32* %32, i64 %33
store i32 4, i32* %34
%35 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%36 = load i32*, i32** %35
%37 = load i64, i64* @VMIN
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 1, i32* %38
%39 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 1
%40 = load i32*, i32** %39
%41 = load i64, i64* @VTIME
%42 = getelementptr inbounds i32, i32* %40, i64 %41
store i32 0, i32* %42
br label %43
43: ; preds = %21
%44 = load i32, i32* %4
%45 = load i32, i32* @TCSADRAIN
%46 = call i32 @tcsetattr(i32 %44, i32 %45, %struct.termios* %6)
%47 = icmp eq i32 -1, %46
br i1 %47, label %48, label %49
48: ; preds = %43
store i32 -1, i32* %3
br label %50
49: ; preds = %43
store i32 0, i32* %3
br label %50
50: ; preds = %49, %48, %11
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@INLCR = global i32 0
@ICRNL = global i32 0
@VEOF = global i64 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSADRAIN = global i32 0
define i32 @pty_setup_slave_term(i32 %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = call i32 @tcgetattr(i32 %0, %struct.termios* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %34, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i32 0, i32* %8
%9 = icmp eq i32 %1, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = load i32, i32* @ICANON
store i32 %11, i32* %8
br label %12
12: ; preds = %10, %7
%13 = load i32, i32* @ECHO
%14 = load i32, i32* @INLCR
%15 = or i32 %14, %13
%16 = load i32, i32* @ICRNL
%17 = or i32 %15, %16
%18 = xor i32 %17, -1
%19 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = and i32 %20, %18
store i32 %21, i32* %19
%22 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%23 = load i32*, i32** %22
%24 = load i64, i64* @VEOF
%25 = getelementptr inbounds i32, i32* %23, i64 %24
store i32 4, i32* %25
%26 = load i64, i64* @VMIN
%27 = getelementptr inbounds i32, i32* %23, i64 %26
store i32 1, i32* %27
%28 = load i64, i64* @VTIME
%29 = getelementptr inbounds i32, i32* %23, i64 %28
store i32 0, i32* %29
%30 = load i32, i32* @TCSADRAIN
%31 = call i32 @tcsetattr(i32 %0, i32 %30, %struct.termios* %3)
%32 = icmp eq i32 %31, -1
%33 = sext i1 %32 to i32
br label %34
34: ; preds = %12, %2
%35 = phi i32 [ -1, %2 ], [ %33, %12 ]
ret i32 %35
}