#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, int *bytes_so_far) {
  fd_set rfds;
  struct timeval tv;
  int retval, len;
  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 {
        *bytes_so_far = 0;
        buff -= *bytes_so_far;
      }
      return 1;
    } else {
      *bytes_so_far += len;
      buff += len;
    }
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<get_usr_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11a6 <get_usr_input+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <get_usr_input+0x121>
cmpl   $0x0,-0x34(%rbp)
je     1250 <get_usr_input+0x110>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
mov    $0xff,%edx
call   1030 <read@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    122f <get_usr_input+0xef>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1200 <get_usr_input+0xc0>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1223 <get_usr_input+0xe3>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x4(%rbp)
jmp    1261 <get_usr_input+0x121>
mov    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    125c <get_usr_input+0x11c>
movl   $0x0,-0x4(%rbp)
jmp    1261 <get_usr_input+0x121>
jmp    1261 <get_usr_input+0x121>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_usr_input>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     11cd <get_usr_input+0x8d>
test   %eax,%eax
jne    1191 <get_usr_input+0x51>
xor    %eax,%eax
jmp    11cd <get_usr_input+0x8d>
xor    %edi,%edi
mov    %r15,%rsi
mov    $0xff,%edx
call   1030 <read@plt>
movslq %eax,%rcx
cmpb   $0xa,-0x1(%rcx,%r15,1)
mov    (%r14),%edx
jne    11bb <get_usr_input+0x7b>
test   %edx,%edx
je     11c2 <get_usr_input+0x82>
movl   $0x0,(%r14)
jmp    11c8 <get_usr_input+0x88>
add    %eax,%edx
mov    %edx,(%r14)
jmp    11cd <get_usr_input+0x8d>
movb   $0x0,-0x1(%r15,%rcx,1)
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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