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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int read(int /*<<< orphan*/, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int get_usr_input(char *buff) {
  fd_set rfds;
  struct timeval tv;
  int retval, len;
  static int bytes_so_far = 0;
  FD_ZERO(&rfds);
  FD_SET(0, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  retval = select(1, &rfds, NULL, NULL, &tv);
  if (retval == -1)
    return -1;
  else if (retval) {
    len = read(0, buff, 255);
    if (buff[len - 1] == '\n') {
      if (bytes_so_far == 0) {
        buff[len - 1] = '\0';
      } else {
        buff -= bytes_so_far;
        bytes_so_far = 0;
      }
      return 1;
    } else {
      bytes_so_far += len;
      buff += len;
      return 0;
    }
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<get_usr_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11a2 <get_usr_input+0x62>
movl   $0xffffffff,-0x4(%rbp)
jmp    125e <get_usr_input+0x11e>
cmpl   $0x0,-0x2c(%rbp)
je     1257 <get_usr_input+0x117>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
mov    $0xff,%edx
call   1030 <read@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    122b <get_usr_input+0xeb>
cmpl   $0x0,0x2e64(%rip)
jne    11fc <get_usr_input+0xbc>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    121f <get_usr_input+0xdf>
mov    0x2e42(%rip),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,0x2e25(%rip)
movl   $0x1,-0x4(%rbp)
jmp    125e <get_usr_input+0x11e>
mov    -0x30(%rbp),%eax
add    0x2e10(%rip),%eax
mov    %eax,0x2e0a(%rip)
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    125e <get_usr_input+0x11e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_usr_input>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11d2 <get_usr_input+0x92>
test   %ebp,%ebp
jne    118d <get_usr_input+0x4d>
xor    %ebp,%ebp
jmp    11d2 <get_usr_input+0x92>
xor    %ebp,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1030 <read@plt>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%rcx,%rbx,1)
mov    0x2e98(%rip),%edx
jne    11be <get_usr_input+0x7e>
test   %edx,%edx
je     11c8 <get_usr_input+0x88>
movl   $0x0,0x2e88(%rip)
jmp    11cd <get_usr_input+0x8d>
add    %eax,%edx
mov    %edx,0x2e7e(%rip)
jmp    11d2 <get_usr_input+0x92>
movb   $0x0,-0x1(%rbx,%rcx,1)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@get_usr_input.bytes_so_far = internal global i32 0
define i32 @get_usr_input(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = call i32 @FD_ZERO(i32* %4)
%9 = call i32 @FD_SET(i32 0, i32* %4)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i64 0, i64* %11
%12 = call i32 @select(i32 1, i32* %4, i32* null, i32* null, %struct.timeval* %5)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %55
16: ; preds = %1
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %54
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = call i32 @read(i32 0, i8* %20, i32 255)
store i32 %21, i32* %7
%22 = load i8*, i8** %3
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %46
30: ; preds = %19
%31 = load i32, i32* @get_usr_input.bytes_so_far
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = load i8*, i8** %3
%35 = load i32, i32* %7
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
store i8 0, i8* %38
br label %45
39: ; preds = %30
%40 = load i32, i32* @get_usr_input.bytes_so_far
%41 = load i8*, i8** %3
%42 = sext i32 %40 to i64
%43 = sub i64 0, %42
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8* %44, i8** %3
store i32 0, i32* @get_usr_input.bytes_so_far
br label %45
45: ; preds = %39, %33
store i32 1, i32* %2
br label %55
46: ; preds = %19
%47 = load i32, i32* %7
%48 = load i32, i32* @get_usr_input.bytes_so_far
%49 = add nsw i32 %48, %47
store i32 %49, i32* @get_usr_input.bytes_so_far
%50 = load i32, i32* %7
%51 = load i8*, i8** %3
%52 = sext i32 %50 to i64
%53 = getelementptr inbounds i8, i8* %51, i64 %52
store i8* %53, i8** %3
store i32 0, i32* %2
br label %55
54: ; preds = %16
store i32 0, i32* %2
br label %55
55: ; preds = %54, %46, %45, %15
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@get_usr_input.bytes_so_far = internal unnamed_addr global i32 0
define i32 @get_usr_input(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %3 to i8*
%6 = call i32 @FD_ZERO(i32* %2)
%7 = call i32 @FD_SET(i32 0, i32* %2)
%8 = bitcast %struct.timeval* %3 to i8*
%9 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, %struct.timeval* %3)
switch i32 %9, label %10 [
i32 -1, label %25
i32 0, label %24
]
10: ; preds = %1
%11 = call i32 @read(i32 0, i8* %0, i32 255)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
%17 = load i32, i32* @get_usr_input.bytes_so_far
br i1 %16, label %18, label %22
18: ; preds = %10
%19 = icmp eq i32 %17, 0
br i1 %19, label %20, label %21
20: ; preds = %18
store i8 0, i8* %14
br label %25
21: ; preds = %18
store i32 0, i32* @get_usr_input.bytes_so_far
br label %25
22: ; preds = %10
%23 = add nsw i32 %17, %11
store i32 %23, i32* @get_usr_input.bytes_so_far
br label %25
24: ; preds = %1
br label %25
25: ; preds = %20, %21, %1, %24, %22
%26 = phi i32 [ 0, %22 ], [ 0, %24 ], [ %9, %1 ], [ 1, %21 ], [ 1, %20 ]
ret i32 %26
}