#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 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 *);
int read(int, char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int write(int, char *, int);
void do_console_mode(int serial_fd) {
  fd_set set;
  int ready_fds;
  char read_buffer[256];
  int 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, 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, 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,-0x115(%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,-0x115(%rbp)
mov    -0x115(%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     128d <do_console_mode+0x11d>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jg     1250 <do_console_mode+0xe0>
lea    0xdba(%rip),%rdi
call   1080 <perror@plt>
jmp    1323 <do_console_mode+0x1b3>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1030 <write@plt>
cmp    -0x114(%rbp),%eax
jge    1288 <do_console_mode+0x118>
lea    0xd87(%rip),%rdi
call   1080 <perror@plt>
jmp    1323 <do_console_mode+0x1b3>
jmp    128d <do_console_mode+0x11d>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     131e <do_console_mode+0x1ae>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jg     12e7 <do_console_mode+0x177>
lea    0xd23(%rip),%rdi
call   1080 <perror@plt>
jmp    1323 <do_console_mode+0x1b3>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1030 <write@plt>
cmp    -0x114(%rbp),%eax
je     1319 <do_console_mode+0x1a9>
lea    0xcf6(%rip),%rdi
call   1080 <perror@plt>
jmp    1323 <do_console_mode+0x1b3>
jmp    131e <do_console_mode+0x1ae>
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     1237 <do_console_mode+0xc7>
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    128a <do_console_mode+0x11a>
mov    %eax,%ebp
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
mov    0x2d93(%rip),%rbp
jl     1281 <do_console_mode+0x111>
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   %eax,%eax
jle    128a <do_console_mode+0x11a>
mov    %eax,%ebp
mov    %r12d,%edi
lea    0x10(%rsp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
mov    0x2d4d(%rip),%rbp
je     11b0 <do_console_mode+0x40>
lea    0xd7d(%rip),%rdi
jmp    1291 <do_console_mode+0x121>
lea    0xd6f(%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 i32
store i32 %0, i32* %2
br label %7
7: ; preds = %1, %69
%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 %47
29: ; preds = %25
%30 = load i32, i32* %2
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 256)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp sle i32 %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 %70
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 i32, i32* %6
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
%42 = load i32, i32* %6
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %70
46: ; preds = %37
br label %47
47: ; preds = %46, %25
%48 = load i32, i32* @STDIN_FILENO
%49 = call i64 @FD_ISSET(i32 %48, i32* %3)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %69
51: ; preds = %47
%52 = load i32, i32* @STDIN_FILENO
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 256)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = icmp sle i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %70
59: ; preds = %51
%60 = load i32, i32* %2
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%62 = load i32, i32* %6
%63 = call i32 @write(i32 %60, i8* %61, i32 %62)
%64 = load i32, i32* %6
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %68
66: ; preds = %59
%67 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %70
68: ; preds = %59
br label %69
69: ; preds = %68, %47
br label %7
70: ; preds = %66, %57, %44, %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 = %40, %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 %29, label %22
22: ; preds = %19
%23 = call i32 @read(i32 %0, i8* %5, i32 256)
%24 = icmp slt i32 %23, 1
br i1 %24, label %41, label %25
25: ; preds = %22
%26 = load i32, i32* @STDIN_FILENO
%27 = call i32 @write(i32 %26, i8* %5, i32 %23)
%28 = icmp slt i32 %27, %23
br i1 %28, label %41, label %29
29: ; preds = %25, %19
%30 = load i32, i32* @STDIN_FILENO
%31 = call i64 @FD_ISSET(i32 %30, i32* %2)
%32 = icmp eq i64 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %29
%34 = load i32, i32* @STDIN_FILENO
%35 = call i32 @read(i32 %34, i8* %5, i32 256)
%36 = icmp slt i32 %35, 1
br i1 %36, label %41, label %37
37: ; preds = %33
%38 = call i32 @write(i32 %0, i8* %5, i32 %35)
%39 = icmp eq i32 %38, %35
br i1 %39, label %40, label %41
40: ; preds = %37, %29
br label %6
41: ; preds = %37, %33, %25, %22
%42 = 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), %33 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %37 ]
%43 = call i32 @perror(i8* %42)
ret void
}