#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;

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, ...);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
void main(void) {
  fd_set fds_read, fds_write;
  int retval;
  char data[1024];
  FD_ZERO(&fds_read);
  FD_ZERO(&fds_write);
  FD_SET(0, &fds_read);
  FD_SET(1, &fds_write);
  while (1) {
    printf("Input some data: ");
    fflush(stdout);
    retval = select(3, &fds_read, NULL, NULL, NULL);
    if (retval < 0) {
      fprintf(stderr, "Error while executing select\n");
      exit(-1);
    }
    printf("select retval: %d\n", retval);
    if (!FD_ISSET(0, &fds_read)) {
      fprintf(stderr, "Error: stdin not ready (?)\n");
      exit(-1);
    }
    printf("gets..\n");
    gets(data);
    printf("gets done..\n");
    if (!strcmp(data, "exit"))
      exit(0);
    printf("Try to write it back\n");
    retval = select(3, NULL, &fds_write, NULL, NULL);
    if (retval < 0) {
      fprintf(stderr, "Error while executing select\n");
      exit(-1);
    }
    if (!FD_ISSET(1, &fds_write)) {
      fprintf(stderr, "Error: stdout not ready (?)\n");
      exit(-1);
    }
    printf("Data: %s\n", data);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
lea    -0x4(%rbp),%rdi
call   1080 <FD_ZERO@plt>
lea    -0x8(%rbp),%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x4(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
call   10c0 <FD_SET@plt>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    $0x3,%edi
lea    -0x4(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1237 <main+0x97>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0xc(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
lea    -0x4(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    127b <main+0xdb>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x410(%rbp),%rdi
call   1070 <gets@plt>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x410(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    12c6 <main+0x126>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x8(%rbp),%rdx
mov    %r8,%rsi
mov    %r8,%rcx
call   1090 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1319 <main+0x179>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
lea    0xd08(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    134f <main+0x1af>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
lea    0xd4f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11d6 <main+0x36>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    $0x3,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %eax,%eax
jns    1235 <main+0x95>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xc(%rsp),%r12
lea    0x10(%rsp),%rbx
lea    0xe58(%rip),%rbp
lea    0x8(%rsp),%r14
lea    0xe2f(%rip),%r15
lea    0xda7(%rip),%r13
nopl   0x0(%rax)
lea    0xdc9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
mov    %r12,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     1335 <main+0x195>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <gets@plt>
lea    0xdf5(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%rsp),%eax
mov    $0x74697865,%ecx
xor    %ecx,%eax
movzbl 0x14(%rsp),%ecx
or     %eax,%ecx
je     134a <main+0x1aa>
mov    %rbp,%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %eax,%eax
js     1216 <main+0x76>
mov    $0x1,%edi
mov    %r14,%rsi
call   1040 <FD_ISSET@plt>
test   %eax,%eax
je     1351 <main+0x1b1>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r13,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    $0x3,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1090 <select@plt>
test   %eax,%eax
jns    1260 <main+0xc0>
jmp    1216 <main+0x76>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
lea    0xcfe(%rip),%rsi
jmp    1226 <main+0x86>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
lea    0xd03(%rip),%rsi
jmp    1226 <main+0x86>

LLVM IR (-O0):
@.str = [18 x i8] c"Input some data: \00"
@stdout = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error while executing select\0A\00"
@.str.2 = [19 x i8] c"select retval: %d\0A\00"
@.str.3 = [28 x i8] c"Error: stdin not ready (?)\0A\00"
@.str.4 = [8 x i8] c"gets..\0A\00"
@.str.5 = [13 x i8] c"gets done..\0A\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.7 = [22 x i8] c"Try to write it back\0A\00"
@.str.8 = [29 x i8] c"Error: stdout not ready (?)\0A\00"
@.str.9 = [10 x i8] c"Data: %s\0A\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = call i32 @FD_ZERO(i32* %1)
%6 = call i32 @FD_ZERO(i32* %2)
%7 = call i32 @FD_SET(i32 0, i32* %1)
%8 = call i32 @FD_SET(i32 1, i32* %2)
br label %9
9: ; preds = %0, %55
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
%13 = call i32 @select(i32 3, i32* %1, i32* null, i32* null, i32* null)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %9
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %9
%21 = load i32, i32* %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i32 @FD_ISSET(i32 0, i32* %1)
%24 = icmp ne i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %20
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = call i32 @gets(i8* %31)
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%35 = call i32 @strcmp(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%36 = icmp ne i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %29
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
%41 = call i32 @select(i32 3, i32* null, i32* %2, i32* null, i32* null)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %39
%49 = call i32 @FD_ISSET(i32 1, i32* %2)
%50 = icmp ne i32 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%54 = call i32 @exit(i32 -1)
unreachable
55: ; preds = %48
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* %56)
br label %9
}

LLVM IR (-O2):
@.str = [18 x i8] c"Input some data: \00"
@stdout = global i32 0
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error while executing select\0A\00"
@.str.2 = [19 x i8] c"select retval: %d\0A\00"
@.str.3 = [28 x i8] c"Error: stdin not ready (?)\0A\00"
@.str.6 = [5 x i8] c"exit\00"
@.str.8 = [29 x i8] c"Error: stdout not ready (?)\0A\00"
@.str.9 = [10 x i8] c"Data: %s\0A\00"
@str = [7 x i8] c"gets..\00"
@str.10 = [12 x i8] c"gets done..\00"
@str.11 = [21 x i8] c"Try to write it back\00"
define void @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = call i32 @FD_ZERO(i32* %1)
%8 = call i32 @FD_ZERO(i32* %2)
%9 = call i32 @FD_SET(i32 0, i32* %1)
%10 = call i32 @FD_SET(i32 1, i32* %2)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stdout
%13 = call i32 @fflush(i32 %12)
%14 = call i32 @select(i32 3, i32* %1, i32* null, i32* null, i32* null)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %52, %0
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %0, %52
%21 = phi i32 [ %57, %52 ], [ %14, %0 ]
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i32 @FD_ISSET(i32 0, i32* %1)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %20
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%31 = call i32 @gets(i8* %6)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0))
%33 = call i32 @bcmp(i8* dereferenceable(5) %6, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64 5)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %29
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.11, i64 0, i64 0))
%39 = call i32 @select(i32 3, i32* null, i32* %2, i32* null, i32* null)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %37
%46 = call i32 @FD_ISSET(i32 1, i32* %2)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %45
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0), i8* %6)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
%57 = call i32 @select(i32 3, i32* %1, i32* null, i32* null, i32* null)
%58 = icmp slt i32 %57, 0
br i1 %58, label %16, label %20
}