#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*/ CATCH_EINTR(int);
int /*<<< orphan*/ TCSADRAIN;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int errno;
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int raw(int fd) {
  struct termios tt;
  int err;
  CATCH_EINTR(err = tcgetattr(fd, &tt));
  if (err < 0)
    return -errno;
  cfmakeraw(&tt);
  CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
  if (err < 0)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    %eax,%edi
mov    %edi,-0x14(%rbp)
call   1040 <CATCH_EINTR@plt>
cmpl   $0x0,-0x14(%rbp)
jge    117e <raw+0x3e>
xor    %eax,%eax
mov    0x2e64(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <raw+0x8a>
lea    -0x10(%rbp),%rdi
call   1030 <cfmakeraw@plt>
mov    -0x8(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    %eax,%edi
mov    %edi,-0x14(%rbp)
call   1040 <CATCH_EINTR@plt>
cmpl   $0x0,-0x14(%rbp)
jge    11c3 <raw+0x83>
xor    %eax,%eax
mov    0x2e1f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11ca <raw+0x8a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%rsi
call   1050 <tcgetattr@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <CATCH_EINTR@plt>
test   %ebp,%ebp
js     1187 <raw+0x47>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <cfmakeraw@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <tcsetattr@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <CATCH_EINTR@plt>
xor    %eax,%eax
test   %ebx,%ebx
jns    1192 <raw+0x52>
mov    0x2e4a(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@errno = global i32 0
@TCSADRAIN = global i32 0
define i32 @raw(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %4)
store i32 %7, i32* %5
%8 = call i32 @CATCH_EINTR(i32 %7)
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %26
14: ; preds = %1
%15 = call i32 @cfmakeraw(%struct.termios* %4)
%16 = load i32, i32* %3
%17 = load i32, i32* @TCSADRAIN
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %4)
store i32 %18, i32* %5
%19 = call i32 @CATCH_EINTR(i32 %18)
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
store i32 %24, i32* %2
br label %26
25: ; preds = %14
store i32 0, i32* %2
br label %26
26: ; preds = %25, %22, %11
%27 = load i32, i32* %2
ret i32 %27
}

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