#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 */
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ read_buffer;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int STDIN_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ write(int, char *, unsigned int);
void do_console_mode(int serial_fd) {
  fd_set set;
  int ready_fds;
  char read_buffer[256];
  ssize_t got;
  while (1) {
    FD_ZERO(&set);
    FD_SET(serial_fd, &set);
    FD_SET(STDIN_FILENO, &set);
    do {
      ready_fds = select(FD_SETSIZE, &set, NULL, NULL, NULL);
    } while (ready_fds < 0 && errno == EINTR);
    if (FD_ISSET(serial_fd, &set)) {
      got = read(serial_fd, read_buffer, sizeof(read_buffer));
      if (got <= 0) {
        perror("read");
        return;
      }
      if (write(STDIN_FILENO, read_buffer, (unsigned int)got) < got) {
        perror("write");
        return;
      }
    }
    if (FD_ISSET(STDIN_FILENO, &set)) {
      got = read(STDIN_FILENO, read_buffer, sizeof(read_buffer));
      if (got <= 0) {
        perror("read");
        return;
      }
      if (write(serial_fd, read_buffer, (unsigned int)got) != got) {
        perror("write");
        return;
      }
    }
  }
}


x86 assembly (clang, -O0):
<do_console_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1070 <select@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x119(%rbp)
jge    11f4 <do_console_mode+0x84>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rax
mov    0x2de8(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x119(%rbp)
mov    -0x119(%rbp),%al
test   $0x1,%al
jne    11a5 <do_console_mode+0x35>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1293 <do_console_mode+0x123>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jg     1252 <do_console_mode+0xe2>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
jmp    132f <do_console_mode+0x1bf>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%rax
mov    %eax,%edx
call   1030 <write@plt>
cmp    -0x118(%rbp),%rax
jge    128e <do_console_mode+0x11e>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
jmp    132f <do_console_mode+0x1bf>
jmp    1293 <do_console_mode+0x123>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     132a <do_console_mode+0x1ba>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jg     12ef <do_console_mode+0x17f>
lea    0xd1b(%rip),%rdi
call   1080 <perror@plt>
jmp    132f <do_console_mode+0x1bf>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x118(%rbp),%rax
mov    %eax,%edx
call   1030 <write@plt>
cmp    -0x118(%rbp),%rax
je     1325 <do_console_mode+0x1b5>
lea    0xcea(%rip),%rdi
call   1080 <perror@plt>
jmp    132f <do_console_mode+0x1bf>
jmp    132a <do_console_mode+0x1ba>
jmp    117e <do_console_mode+0xe>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_console_mode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r12d
lea    0xc(%rsp),%rbx
mov    0x2e38(%rip),%rbp
mov    0x2e49(%rip),%r14
mov    0x2e3a(%rip),%r15
mov    0x2e2b(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
xchg   %ax,%ax
mov    (%r14),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
test   %eax,%eax
jns    11ef <do_console_mode+0x7f>
mov    0x0(%r13),%rax
cmp    %rax,(%r15)
je     11d0 <do_console_mode+0x60>
mov    %r12d,%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     123a <do_console_mode+0xca>
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    1290 <do_console_mode+0x120>
mov    %rax,%rbp
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %rbp,%rax
mov    0x2d90(%rip),%rbp
jl     1287 <do_console_mode+0x117>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11b0 <do_console_mode+0x40>
mov    0x0(%rbp),%edi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    1290 <do_console_mode+0x120>
mov    %rax,%rbp
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %rbp,%rax
mov    0x2d47(%rip),%rbp
je     11b0 <do_console_mode+0x40>
lea    0xd77(%rip),%rdi
jmp    1297 <do_console_mode+0x127>
lea    0xd69(%rip),%rdi
call   1080 <perror@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define void @do_console_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [256 x i8]
%6 = alloca i64
store i32 %0, i32* %2
br label %7
7: ; preds = %1, %71
%8 = call i32 @FD_ZERO(i32* %3)
%9 = load i32, i32* %2
%10 = call i32 @FD_SET(i32 %9, i32* %3)
%11 = load i32, i32* @STDIN_FILENO
%12 = call i32 @FD_SET(i32 %11, i32* %3)
br label %13
13: ; preds = %23, %7
%14 = load i32, i32* @FD_SETSIZE
%15 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, i32* null)
store i32 %15, i32* %4
br label %16
16: ; preds = %13
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %13, label %25
25: ; preds = %23
%26 = load i32, i32* %2
%27 = call i64 @FD_ISSET(i32 %26, i32* %3)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %48
29: ; preds = %25
%30 = load i32, i32* %2
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%32 = call i64 @read(i32 %30, i8* %31, i32 256)
store i64 %32, i64* %6
%33 = load i64, i64* %6
%34 = icmp sle i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %72
37: ; preds = %29
%38 = load i32, i32* @STDIN_FILENO
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%40 = load i64, i64* %6
%41 = trunc i64 %40 to i32
%42 = call i64 @write(i32 %38, i8* %39, i32 %41)
%43 = load i64, i64* %6
%44 = icmp slt i64 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %37
%46 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %72
47: ; preds = %37
br label %48
48: ; preds = %47, %25
%49 = load i32, i32* @STDIN_FILENO
%50 = call i64 @FD_ISSET(i32 %49, i32* %3)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %71
52: ; preds = %48
%53 = load i32, i32* @STDIN_FILENO
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%55 = call i64 @read(i32 %53, i8* %54, i32 256)
store i64 %55, i64* %6
%56 = load i64, i64* %6
%57 = icmp sle i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %72
60: ; preds = %52
%61 = load i32, i32* %2
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%63 = load i64, i64* %6
%64 = trunc i64 %63 to i32
%65 = call i64 @write(i32 %61, i8* %62, i32 %64)
%66 = load i64, i64* %6
%67 = icmp ne i64 %65, %66
br i1 %67, label %68, label %70
68: ; preds = %60
%69 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %72
70: ; preds = %60
br label %71
71: ; preds = %70, %48
br label %7
72: ; preds = %68, %58, %45, %35
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [5 x i8] c"read\00"
@.str.1 = [6 x i8] c"write\00"
define void @do_console_mode(i32 %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = bitcast i32* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
br label %6
6: ; preds = %42, %1
%7 = call i32 @FD_ZERO(i32* %2)
%8 = call i32 @FD_SET(i32 %0, i32* %2)
%9 = load i32, i32* @STDIN_FILENO
%10 = call i32 @FD_SET(i32 %9, i32* %2)
br label %11
11: ; preds = %11, %6
%12 = load i32, i32* @FD_SETSIZE
%13 = call i32 @select(i32 %12, i32* %2, i32* null, i32* null, i32* null)
%14 = icmp slt i32 %13, 0
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
br i1 %18, label %11, label %19
19: ; preds = %11
%20 = call i64 @FD_ISSET(i32 %0, i32* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %19
%23 = call i64 @read(i32 %0, i8* %5, i32 256)
%24 = icmp slt i64 %23, 1
br i1 %24, label %43, label %25
25: ; preds = %22
%26 = load i32, i32* @STDIN_FILENO
%27 = trunc i64 %23 to i32
%28 = call i64 @write(i32 %26, i8* %5, i32 %27)
%29 = icmp slt i64 %28, %23
br i1 %29, label %43, label %30
30: ; preds = %25, %19
%31 = load i32, i32* @STDIN_FILENO
%32 = call i64 @FD_ISSET(i32 %31, i32* %2)
%33 = icmp eq i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %30
%35 = load i32, i32* @STDIN_FILENO
%36 = call i64 @read(i32 %35, i8* %5, i32 256)
%37 = icmp slt i64 %36, 1
br i1 %37, label %43, label %38
38: ; preds = %34
%39 = trunc i64 %36 to i32
%40 = call i64 @write(i32 %0, i8* %5, i32 %39)
%41 = icmp eq i64 %40, %36
br i1 %41, label %42, label %43
42: ; preds = %38, %30
br label %6
43: ; preds = %38, %34, %25, %22
%44 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %22 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %25 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %34 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %38 ]
%45 = call i32 @perror(i8* %44)
ret void
}