#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*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ perror(char *);
int tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int tty_init(int fd) {
  int ret = 0;
  struct termios term;
  tcgetattr(fd, &term);
  cfmakeraw(&term);
  ret = tcflush(fd, TCIOFLUSH);
  if (ret < 0) {
    perror("tcflush");
    goto out;
  }
  ret = tcsetattr(fd, TCSANOW, &term);
  if (ret < 0) {
    perror("tcsetattr");
    goto out;
  }
out:
  return ret;
}


x86 assembly (clang, -O0):
<tty_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    -0x4(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a6 <tty_init+0x56>
lea    0xe64(%rip),%rdi
call   1070 <perror@plt>
jmp    11de <tty_init+0x8e>
mov    -0x4(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d9 <tty_init+0x89>
lea    0xe39(%rip),%rdi
call   1070 <perror@plt>
jmp    11de <tty_init+0x8e>
jmp    11de <tty_init+0x8e>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_init>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
mov    %rbx,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <tcflush@plt>
test   %eax,%eax
js     119e <tty_init+0x4e>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1060 <tcsetattr@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11a9 <tty_init+0x59>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %eax,%ebx
lea    0xe59(%rip),%rdi
jmp    11b0 <tty_init+0x60>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
jmp    1195 <tty_init+0x45>

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCIOFLUSH = global i32 0
@.str = [8 x i8] c"tcflush\00"
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @tty_init(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %2
store i32 0, i32* %3
%5 = load i32, i32* %2
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %4)
%7 = call i32 @cfmakeraw(%struct.termios* %4)
%8 = load i32, i32* %2
%9 = load i32, i32* @TCIOFLUSH
%10 = call i32 @tcflush(i32 %8, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %24
15: ; preds = %1
%16 = load i32, i32* %2
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %4)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %24
23: ; preds = %15
br label %24
24: ; preds = %23, %21, %13
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCIOFLUSH = global i32 0
@.str = [8 x i8] c"tcflush\00"
@TCSANOW = global i32 0
@.str.1 = [10 x i8] c"tcsetattr\00"
define i32 @tty_init(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 @cfmakeraw(%struct.termios* %2)
%6 = load i32, i32* @TCIOFLUSH
%7 = call i32 @tcflush(i32 %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %1
%10 = load i32, i32* @TCSANOW
%11 = call i32 @tcsetattr(i32 %0, i32 %10, %struct.termios* %2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9, %1
%14 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %9 ]
%15 = phi i32 [ %7, %1 ], [ %11, %9 ]
%16 = call i32 @perror(i8* %14)
br label %17
17: ; preds = %13, %9
%18 = phi i32 [ %11, %9 ], [ %15, %13 ]
ret i32 %18
}