#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 timeval {
  int dummy;
};
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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(struct timeval *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(struct timeval *, struct timeval *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int select_on(int fd, char *buffer, int bufsize, struct timeval *timeout) {
  struct timeval *to = NULL;
  fd_set readfds;
  int retval = 0;
  if (!buffer || bufsize <= 0)
    return -2;
  if (timeout != NULL) {
    if (NULL == (to = (struct timeval *)malloc(sizeof(struct timeval)))) {
      fprintf(stderr, "Out of Memory in select_on!\n");
      exit(1);
    }
    memcpy(to, timeout, sizeof(struct timeval));
  }
  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);
  if (0 > (retval = select(fd + 1, &readfds, NULL, NULL, to))) {
    perror("select_on: select");
    exit(1);
  }
  if (to != NULL)
    free(to);
  if (0 == retval)
    return 0;
  if (!FD_ISSET(fd, &readfds)) {
    printf("fd not set true. Err... I don't want to know what "
           "went wrong.\n");
    return -3;
  }
  if (0 > (retval = read(fd, buffer, bufsize - 1))) {
    perror("select_on: read");
    exit(1);
  }
  buffer[retval] = '\0';
  return retval + 1;
}


x86 assembly (clang, -O0):
<select_on>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11fa <select_on+0x3a>
cmpl   $0x0,-0x14(%rbp)
jg     1206 <select_on+0x46>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1356 <select_on+0x196>
cmpq   $0x0,-0x20(%rbp)
je     125e <select_on+0x9e>
mov    $0x4,%edi
call   10b0 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x28(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    124c <select_on+0x8c>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
lea    -0x2c(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10e0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%r8
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   10a0 <select@plt>
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    12b2 <select_on+0xf2>
lea    0xd7a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
je     12c6 <select_on+0x106>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
cmp    -0x30(%rbp),%eax
jne    12dd <select_on+0x11d>
movl   $0x0,-0x4(%rbp)
jmp    1356 <select_on+0x196>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%eax
jne    130a <select_on+0x14a>
lea    0xd36(%rip),%rdi
call   1050 <printf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1356 <select_on+0x196>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x30(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jle    1341 <select_on+0x181>
lea    0xd3c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select_on>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0xfffffffe,%eax
test   %rsi,%rsi
je     12a8 <select_on+0xe8>
mov    %edx,%r15d
test   %edx,%edx
jle    12a8 <select_on+0xe8>
mov    %rcx,%rbx
mov    %rsi,%r14
mov    %edi,%r12d
test   %rcx,%rcx
je     121b <select_on+0x5b>
mov    $0x4,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12be <select_on+0xfe>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <memcpy@plt>
jmp    121d <select_on+0x5d>
xor    %ebp,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10e0 <FD_SET@plt>
lea    0x1(%r12),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
call   10a0 <select@plt>
test   %eax,%eax
js     12b5 <select_on+0xf5>
mov    %eax,%ebx
test   %rbp,%rbp
je     125c <select_on+0x9c>
mov    %rbp,%rdi
call   1030 <free@plt>
test   %ebx,%ebx
je     1293 <select_on+0xd3>
lea    0xc(%rsp),%rsi
mov    %r12d,%edi
call   1050 <FD_ISSET@plt>
test   %eax,%eax
je     1297 <select_on+0xd7>
add    $0xffffffff,%r15d
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
test   %eax,%eax
js     12dd <select_on+0x11d>
mov    %eax,%ecx
movb   $0x0,(%r14,%rcx,1)
add    $0x1,%eax
jmp    12a8 <select_on+0xe8>
xor    %eax,%eax
jmp    12a8 <select_on+0xe8>
lea    0xda1(%rip),%rdi
call   1040 <puts@plt>
mov    $0xfffffffd,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
jmp    12e4 <select_on+0x124>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd4b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"Out of Memory in select_on!\0A\00"
@.str.1 = [18 x i8] c"select_on: select\00"
@.str.2 = [63 x i8] c"fd not set true. Err... I don't want to know what went wrong.\0A\00"
@.str.3 = [16 x i8] c"select_on: read\00"
define i32 @select_on(i32 %0, i8* %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.timeval*
%10 = alloca %struct.timeval*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.timeval* %3, %struct.timeval** %9
store %struct.timeval* null, %struct.timeval** %10
store i32 0, i32* %12
%13 = load i8*, i8** %7
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = load i32, i32* %8
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15, %4
store i32 -2, i32* %5
br label %79
19: ; preds = %15
%20 = load %struct.timeval*, %struct.timeval** %9
%21 = icmp ne %struct.timeval* %20, null
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = call i64 @malloc(i32 4)
%24 = inttoptr i64 %23 to %struct.timeval*
store %struct.timeval* %24, %struct.timeval** %10
%25 = icmp eq %struct.timeval* null, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = load %struct.timeval*, %struct.timeval** %10
%32 = load %struct.timeval*, %struct.timeval** %9
%33 = call i32 @memcpy(%struct.timeval* %31, %struct.timeval* %32, i32 4)
br label %34
34: ; preds = %30, %19
%35 = call i32 @FD_ZERO(i32* %11)
%36 = load i32, i32* %6
%37 = call i32 @FD_SET(i32 %36, i32* %11)
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
%40 = load %struct.timeval*, %struct.timeval** %10
%41 = call i32 @select(i32 %39, i32* %11, i32* null, i32* null, %struct.timeval* %40)
store i32 %41, i32* %12
%42 = icmp sgt i32 0, %41
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %34
%47 = load %struct.timeval*, %struct.timeval** %10
%48 = icmp ne %struct.timeval* %47, null
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = load %struct.timeval*, %struct.timeval** %10
%51 = call i32 @free(%struct.timeval* %50)
br label %52
52: ; preds = %49, %46
%53 = load i32, i32* %12
%54 = icmp eq i32 0, %53
br i1 %54, label %55, label %56
55: ; preds = %52
store i32 0, i32* %5
br label %79
56: ; preds = %52
%57 = load i32, i32* %6
%58 = call i32 @FD_ISSET(i32 %57, i32* %11)
%59 = icmp ne i32 %58, 0
br i1 %59, label %62, label %60
60: ; preds = %56
%61 = call i32 @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %5
br label %79
62: ; preds = %56
%63 = load i32, i32* %6
%64 = load i8*, i8** %7
%65 = load i32, i32* %8
%66 = sub nsw i32 %65, 1
%67 = call i32 @read(i32 %63, i8* %64, i32 %66)
store i32 %67, i32* %12
%68 = icmp sgt i32 0, %67
br i1 %68, label %69, label %72
69: ; preds = %62
%70 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %62
%73 = load i8*, i8** %7
%74 = load i32, i32* %12
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
%77 = load i32, i32* %12
%78 = add nsw i32 %77, 1
store i32 %78, i32* %5
br label %79
79: ; preds = %72, %60, %55, %18
%80 = load i32, i32* %5
ret i32 %80
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@stderr = global i32 0
@.str = [29 x i8] c"Out of Memory in select_on!\0A\00"
@.str.1 = [18 x i8] c"select_on: select\00"
@.str.3 = [16 x i8] c"select_on: read\00"
@str = [62 x i8] c"fd not set true. Err... I don't want to know what went wrong.\00"
define i32 @select_on(i32 %0, i8* %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = icmp eq i8* %1, null
%8 = icmp slt i32 %2, 1
%9 = or i1 %7, %8
br i1 %9, label %54, label %10
10: ; preds = %4
%11 = icmp eq %struct.timeval* %3, null
br i1 %11, label %22, label %12
12: ; preds = %10
%13 = tail call i64 @malloc(i32 4)
%14 = inttoptr i64 %13 to %struct.timeval*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = tail call i32 @memcpy(%struct.timeval* %14, %struct.timeval* %3, i32 4)
br label %22
22: ; preds = %20, %10
%23 = phi %struct.timeval* [ %14, %20 ], [ null, %10 ]
%24 = call i32 @FD_ZERO(i32* %5)
%25 = call i32 @FD_SET(i32 %0, i32* %5)
%26 = add nsw i32 %0, 1
%27 = call i32 @select(i32 %26, i32* %5, i32* null, i32* null, %struct.timeval* %23)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %22
%33 = icmp eq %struct.timeval* %23, null
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = call i32 @free(%struct.timeval* %23)
br label %36
36: ; preds = %34, %32
%37 = icmp eq i32 %27, 0
br i1 %37, label %54, label %38
38: ; preds = %36
%39 = call i32 @FD_ISSET(i32 %0, i32* %5)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @str, i64 0, i64 0))
br label %54
43: ; preds = %38
%44 = add nsw i32 %2, -1
%45 = call i32 @read(i32 %0, i8* %1, i32 %44)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %43
%51 = zext i32 %45 to i64
%52 = getelementptr inbounds i8, i8* %1, i64 %51
store i8 0, i8* %52
%53 = add nuw nsw i32 %45, 1
br label %54
54: ; preds = %36, %4, %50, %41
%55 = phi i32 [ %53, %50 ], [ -3, %41 ], [ -2, %4 ], [ 0, %36 ]
ret i32 %55
}