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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/);
int read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, int);
void serial_mode(int fd) {
  int bytes;
  char buf[256];
  while (1) {
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    FD_SET(fileno(stdin), &fds);
    if (select(fd + 1, &fds, NULL, NULL, NULL) < 0) {
      return;
    }
    if (FD_ISSET(fd, &fds)) {
      bytes = read(fd, buf, sizeof(buf));
      if (bytes > 0)
        write(fileno(stdout), buf, bytes);
    }
    if (FD_ISSET(fileno(stdin), &fds)) {
      bytes = read(fileno(stdin), buf, sizeof(buf));
      if (bytes > 0)
        write(fd, buf, bytes);
    }
  }
}


x86 assembly (clang, -O0):
<serial_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x114(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x114(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x114(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x114(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1080 <select@plt>
cmp    $0x0,%rax
jge    11e5 <serial_mode+0x75>
add    $0x120,%rsp
pop    %rbp
ret
mov    -0x4(%rbp),%edi
lea    -0x114(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1243 <serial_mode+0xd3>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    123e <serial_mode+0xce>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x110(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
jmp    1243 <serial_mode+0xd3>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x114(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ae <serial_mode+0x13e>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    12a9 <serial_mode+0x139>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1030 <write@plt>
jmp    12ae <serial_mode+0x13e>
jmp    117e <serial_mode+0xe>

x86 assembly (clang, -O2):
<serial_mode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r12d
lea    0x1(%r12),%r14d
lea    0xc(%rsp),%rbp
mov    0x2e53(%rip),%r13
lea    0x10(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1070 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    0x0(%r13),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1080 <select@plt>
test   %rax,%rax
js     126f <serial_mode+0xff>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1221 <serial_mode+0xb1>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    1221 <serial_mode+0xb1>
mov    %eax,%ebx
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    0x0(%r13),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11a0 <serial_mode+0x30>
mov    0x0(%r13),%edi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11a0 <serial_mode+0x30>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11a0 <serial_mode+0x30>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@stdout = global i32 0
define void @serial_mode(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %53
%7 = call i32 @FD_ZERO(i32* %5)
%8 = load i32, i32* %2
%9 = call i32 @FD_SET(i32 %8, i32* %5)
%10 = load i32, i32* @stdin
%11 = call i32 @fileno(i32 %10)
%12 = call i32 @FD_SET(i32 %11, i32* %5)
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
%15 = call i64 @select(i32 %14, i32* %5, i32* null, i32* null, i32* null)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %6
ret void
18: ; preds = %6
%19 = load i32, i32* %2
%20 = call i64 @FD_ISSET(i32 %19, i32* %5)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = load i32, i32* %2
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 256)
store i32 %25, i32* %3
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* @stdout
%30 = call i32 @fileno(i32 %29)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* %3
%33 = call i32 @write(i32 %30, i8* %31, i32 %32)
br label %34
34: ; preds = %28, %22
br label %35
35: ; preds = %34, %18
%36 = load i32, i32* @stdin
%37 = call i32 @fileno(i32 %36)
%38 = call i64 @FD_ISSET(i32 %37, i32* %5)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %53
40: ; preds = %35
%41 = load i32, i32* @stdin
%42 = call i32 @fileno(i32 %41)
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 256)
store i32 %44, i32* %3
%45 = load i32, i32* %3
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %40
%48 = load i32, i32* %2
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%50 = load i32, i32* %3
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
br label %52
52: ; preds = %47, %40
br label %53
53: ; preds = %52, %35
br label %6
}

LLVM IR (-O2):
@stdin = global i32 0
@stdout = global i32 0
define void @serial_mode(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = add nsw i32 %0, 1
br label %7
7: ; preds = %37, %1
%8 = call i32 @FD_ZERO(i32* %3)
%9 = call i32 @FD_SET(i32 %0, i32* %3)
%10 = load i32, i32* @stdin
%11 = call i32 @fileno(i32 %10)
%12 = call i32 @FD_SET(i32 %11, i32* %3)
%13 = call i64 @select(i32 %6, i32* %3, i32* null, i32* null, i32* null)
%14 = icmp slt i64 %13, 0
br i1 %14, label %38, label %15
15: ; preds = %7
%16 = call i64 @FD_ISSET(i32 %0, i32* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = call i32 @read(i32 %0, i8* %4, i32 256)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i32, i32* @stdout
%23 = call i32 @fileno(i32 %22)
%24 = call i32 @write(i32 %23, i8* %4, i32 %19)
br label %25
25: ; preds = %18, %21, %15
%26 = load i32, i32* @stdin
%27 = call i32 @fileno(i32 %26)
%28 = call i64 @FD_ISSET(i32 %27, i32* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %25
%31 = load i32, i32* @stdin
%32 = call i32 @fileno(i32 %31)
%33 = call i32 @read(i32 %32, i8* %4, i32 256)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @write(i32 %0, i8* %4, i32 %33)
br label %37
37: ; preds = %25, %35, %30
br label %7
38: ; preds = %7
ret void
}