#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 tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_CLR(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error_log(char *, ...);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int *);
scalar_t__ isatty(int /*<<< orphan*/);
unsigned char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int read(int /*<<< orphan*/, unsigned char *, int);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, void *);
int /*<<< orphan*/ stdout;
int input_get(unsigned char **dest, char *prompt) {
  int r, input_len;
  fd_set input_stream;
  struct timeval timeout;
  void *timeout_p;
  FD_ZERO(&input_stream);
  input_len = 0;
  timeout.tv_sec = 10;
  timeout.tv_usec = 0;
  if (isatty(STDIN_FILENO)) {
    timeout_p = NULL;
    if (prompt != NULL) {
      printf("%s", prompt);
      fflush(stdout);
    }
  } else {
    timeout_p = &timeout;
  }
  FD_SET(STDIN_FILENO, &input_stream);
  r = select(FD_SETSIZE, &input_stream, NULL, NULL, timeout_p);
  if (r < 0) {
    error_log("Error while waiting for input data. Errno: %i", errno);
    return -1;
  }
  if (r > 0) {
    r = ioctl(STDIN_FILENO, FIONREAD, &input_len);
    if (r < 0) {
      error_log("Could not determine length of input. Errno: %i", errno);
      return -1;
    }
    if (input_len > 0) {
      *dest = malloc(input_len);
      if (*dest == NULL) {
        error_log("Memory allocation error.");
        return -1;
      }
      r = read(STDIN_FILENO, *dest, input_len);
      if (r < 0) {
        error_log("Input read error. Errno: %i", errno);
        return -1;
      }
    }
  }
  FD_CLR(STDIN_FILENO, &input_stream);
  return input_len;
}


x86 assembly (clang, -O0):
<input_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1070 <FD_ZERO@plt>
movl   $0x0,-0x20(%rbp)
movl   $0xa,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     122d <input_get+0x7d>
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1228 <input_get+0x78>
mov    -0x18(%rbp),%rsi
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    1235 <input_get+0x85>
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%r8
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1294 <input_get+0xe4>
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   10b0 <error_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <input_get+0x1e7>
cmpl   $0x0,-0x1c(%rbp)
jle    137f <input_get+0x1cf>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12e9 <input_get+0x139>
mov    0x2d03(%rip),%rax
mov    (%rax),%esi
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   10b0 <error_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <input_get+0x1e7>
cmpl   $0x0,-0x20(%rbp)
jle    137a <input_get+0x1ca>
mov    -0x20(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    132d <input_get+0x17d>
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   10b0 <error_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <input_get+0x1e7>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1375 <input_get+0x1c5>
mov    0x2c77(%rip),%rax
mov    (%rax),%esi
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   10b0 <error_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1397 <input_get+0x1e7>
jmp    137a <input_get+0x1ca>
jmp    137f <input_get+0x1cf>
mov    0x2c3a(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   10c0 <FD_CLR@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<input_get>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0x4(%rsp),%rdi
call   1070 <FD_ZERO@plt>
movl   $0x0,(%rsp)
movl   $0xa,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2dd6(%rip),%r12
mov    (%r12),%edi
call   1030 <isatty@plt>
test   %rax,%rax
je     121f <input_get+0x6f>
test   %rbx,%rbx
je     121a <input_get+0x6a>
lea    0xdfc(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
xor    %r15d,%r15d
jmp    1224 <input_get+0x74>
lea    0x8(%rsp),%r15
mov    (%r12),%edi
lea    0x4(%rsp),%rbx
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1080 <select@plt>
test   %eax,%eax
js     12a7 <input_get+0xf7>
je     1294 <input_get+0xe4>
mov    (%r12),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     12b9 <input_get+0x109>
mov    (%rsp),%ebx
test   %ebx,%ebx
jle    1294 <input_get+0xe4>
mov    %ebx,%edi
call   1090 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     12cb <input_get+0x11b>
mov    (%r12),%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
test   %eax,%eax
js     12db <input_get+0x12b>
mov    (%r12),%edi
lea    0x4(%rsp),%rsi
call   10c0 <FD_CLR@plt>
mov    (%rsp),%eax
jmp    12f7 <input_get+0x147>
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
lea    0xd4c(%rip),%rdi
jmp    12eb <input_get+0x13b>
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
lea    0xd68(%rip),%rdi
jmp    12eb <input_get+0x13b>
lea    0xd8e(%rip),%rdi
xor    %eax,%eax
call   10b0 <error_log@plt>
jmp    12f2 <input_get+0x142>
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
lea    0xd8e(%rip),%rdi
xor    %eax,%eax
call   10b0 <error_log@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i32 0
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [46 x i8] c"Error while waiting for input data. Errno: %i\00"
@errno = global i32 0
@FIONREAD = global i32 0
@.str.2 = [47 x i8] c"Could not determine length of input. Errno: %i\00"
@.str.3 = [25 x i8] c"Memory allocation error.\00"
@.str.4 = [28 x i8] c"Input read error. Errno: %i\00"
define i32 @input_get(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i8*
store i8** %0, i8*** %4
store i8* %1, i8** %5
%11 = call i32 @FD_ZERO(i32* %8)
store i32 0, i32* %7
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 10, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %13
%14 = load i32, i32* @STDIN_FILENO
%15 = call i64 @isatty(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %2
store i8* null, i8** %10
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8*, i8** %5
%22 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
br label %25
25: ; preds = %20, %17
br label %28
26: ; preds = %2
%27 = bitcast %struct.timeval* %9 to i8*
store i8* %27, i8** %10
br label %28
28: ; preds = %26, %25
%29 = load i32, i32* @STDIN_FILENO
%30 = call i32 @FD_SET(i32 %29, i32* %8)
%31 = load i32, i32* @FD_SETSIZE
%32 = load i8*, i8** %10
%33 = call i32 @select(i32 %31, i32* %8, i32* null, i32* null, i8* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = load i32, i32* @errno
%38 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %37)
store i32 -1, i32* %3
br label %80
39: ; preds = %28
%40 = load i32, i32* %6
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %76
42: ; preds = %39
%43 = load i32, i32* @STDIN_FILENO
%44 = load i32, i32* @FIONREAD
%45 = call i32 @ioctl(i32 %43, i32 %44, i32* %7)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = load i32, i32* @errno
%50 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %49)
store i32 -1, i32* %3
br label %80
51: ; preds = %42
%52 = load i32, i32* %7
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %75
54: ; preds = %51
%55 = load i32, i32* %7
%56 = call i8* @malloc(i32 %55)
%57 = load i8**, i8*** %4
store i8* %56, i8** %57
%58 = load i8**, i8*** %4
%59 = load i8*, i8** %58
%60 = icmp eq i8* %59, null
br i1 %60, label %61, label %63
61: ; preds = %54
%62 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %80
63: ; preds = %54
%64 = load i32, i32* @STDIN_FILENO
%65 = load i8**, i8*** %4
%66 = load i8*, i8** %65
%67 = load i32, i32* %7
%68 = call i32 @read(i32 %64, i8* %66, i32 %67)
store i32 %68, i32* %6
%69 = load i32, i32* %6
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %63
%72 = load i32, i32* @errno
%73 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %72)
store i32 -1, i32* %3
br label %80
74: ; preds = %63
br label %75
75: ; preds = %74, %51
br label %76
76: ; preds = %75, %39
%77 = load i32, i32* @STDIN_FILENO
%78 = call i32 @FD_CLR(i32 %77, i32* %8)
%79 = load i32, i32* %7
store i32 %79, i32* %3
br label %80
80: ; preds = %76, %71, %61, %48, %36
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i32 0
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
@FD_SETSIZE = global i32 0
@.str.1 = [46 x i8] c"Error while waiting for input data. Errno: %i\00"
@errno = global i32 0
@FIONREAD = global i32 0
@.str.2 = [47 x i8] c"Could not determine length of input. Errno: %i\00"
@.str.3 = [25 x i8] c"Memory allocation error.\00"
@.str.4 = [28 x i8] c"Input read error. Errno: %i\00"
define i32 @input_get(i8** nocapture writeonly %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = call i32 @FD_ZERO(i32* %4)
store i32 0, i32* %3
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 10, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* @STDIN_FILENO
%13 = call i64 @isatty(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %21, label %15
15: ; preds = %2
%16 = icmp eq i8* %1, null
br i1 %16, label %21, label %17
17: ; preds = %15
%18 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%19 = load i32, i32* @stdout
%20 = call i32 @fflush(i32 %19)
br label %21
21: ; preds = %2, %15, %17
%22 = phi i8* [ null, %17 ], [ null, %15 ], [ %8, %2 ]
%23 = load i32, i32* @STDIN_FILENO
%24 = call i32 @FD_SET(i32 %23, i32* %4)
%25 = load i32, i32* @FD_SETSIZE
%26 = call i32 @select(i32 %25, i32* %4, i32* null, i32* null, i8* %22)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = load i32, i32* @errno
%30 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %60
31: ; preds = %21
%32 = icmp eq i32 %26, 0
br i1 %32, label %56, label %33
33: ; preds = %31
%34 = load i32, i32* @STDIN_FILENO
%35 = load i32, i32* @FIONREAD
%36 = call i32 @ioctl(i32 %34, i32 %35, i32* %3)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* @errno
%40 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i32 %39)
br label %60
41: ; preds = %33
%42 = load i32, i32* %3
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %56
44: ; preds = %41
%45 = call i8* @malloc(i32 %42)
store i8* %45, i8** %0
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %60
49: ; preds = %44
%50 = load i32, i32* @STDIN_FILENO
%51 = call i32 @read(i32 %50, i8* %45, i32 %42)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* @errno
%55 = call i32 (i8*, ...) @error_log(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %54)
br label %60
56: ; preds = %41, %49, %31
%57 = load i32, i32* @STDIN_FILENO
%58 = call i32 @FD_CLR(i32 %57, i32* %4)
%59 = load i32, i32* %3
br label %60
60: ; preds = %56, %53, %47, %38, %28
%61 = phi i32 [ -1, %28 ], [ -1, %38 ], [ -1, %47 ], [ -1, %53 ], [ %59, %56 ]
ret i32 %61
}