#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*/ TCSADRAIN;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int errno;
int jl_uv_handle(void *);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int uv_tty_set_mode(void *, int);
int tty_makeraw(void *tty, int mode) {
  struct termios tmp;
  int fd;
  if (mode == 0)
    return uv_tty_set_mode(tty, 0);
  uv_tty_set_mode(tty, 1);
  fd = jl_uv_handle(tty);
  if (tcgetattr(fd, &tmp))
    return -errno;
  cfmakeraw(&tmp);
  if (tcsetattr(fd, TCSADRAIN, &tmp))
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<tty_makeraw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    117c <tty_makeraw+0x2c>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <uv_tty_set_mode@plt>
mov    %eax,-0x4(%rbp)
jmp    1201 <tty_makeraw+0xb1>
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1030 <uv_tty_set_mode@plt>
mov    -0x10(%rbp),%rdi
call   1040 <jl_uv_handle@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     11bf <tty_makeraw+0x6f>
xor    %eax,%eax
mov    0x2e23(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1201 <tty_makeraw+0xb1>
lea    -0x18(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     11fa <tty_makeraw+0xaa>
xor    %eax,%eax
mov    0x2de8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1201 <tty_makeraw+0xb1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_makeraw>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %esi,%esi
je     11b7 <tty_makeraw+0x67>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <uv_tty_set_mode@plt>
mov    %rbx,%rdi
call   1040 <jl_uv_handle@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <tcgetattr@plt>
test   %rax,%rax
jne    11a4 <tty_makeraw+0x54>
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
je     11c8 <tty_makeraw+0x78>
mov    0x2e2d(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rbx,%rdi
xor    %esi,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <uv_tty_set_mode@plt>
xor    %eax,%eax
jmp    11af <tty_makeraw+0x5f>

LLVM IR (-O0):
%struct.termios = type { i32 }
@errno = global i32 0
@TCSADRAIN = global i32 0
define i32 @tty_makeraw(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.termios
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32 @uv_tty_set_mode(i8* %11, i32 0)
store i32 %12, i32* %3
br label %34
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = call i32 @uv_tty_set_mode(i8* %14, i32 1)
%16 = load i8*, i8** %4
%17 = call i32 @jl_uv_handle(i8* %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = call i64 @tcgetattr(i32 %18, %struct.termios* %6)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %3
br label %34
24: ; preds = %13
%25 = call i32 @cfmakeraw(%struct.termios* %6)
%26 = load i32, i32* %7
%27 = load i32, i32* @TCSADRAIN
%28 = call i64 @tcsetattr(i32 %26, i32 %27, %struct.termios* %6)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %34
33: ; preds = %24
store i32 0, i32* %3
br label %34
34: ; preds = %33, %30, %21, %10
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@errno = global i32 0
@TCSADRAIN = global i32 0
define i32 @tty_makeraw(i8* %0, i32 %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = icmp eq i32 %1, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @uv_tty_set_mode(i8* %0, i32 0)
br label %24
8: ; preds = %2
%9 = tail call i32 @uv_tty_set_mode(i8* %0, i32 1)
%10 = tail call i32 @jl_uv_handle(i8* %0)
%11 = call i64 @tcgetattr(i32 %10, %struct.termios* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
br label %24
16: ; preds = %8
%17 = call i32 @cfmakeraw(%struct.termios* %3)
%18 = load i32, i32* @TCSADRAIN
%19 = call i64 @tcsetattr(i32 %10, i32 %18, %struct.termios* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
br label %24
24: ; preds = %16, %21, %13, %6
%25 = phi i32 [ %7, %6 ], [ %15, %13 ], [ %23, %21 ], [ 0, %16 ]
ret i32 %25
}