#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 */

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/);
char fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
void readstring(char *target, unsigned int size) {
  char *s = target;
  while (((*s = fgetc(stdin)) != '\n') && (*s != '\0') && (!feof(stdin)) &&
         ++s < target + size - 1)
    ;
  *s = 0;
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    -0x18(%rbp),%rcx
mov    %al,(%rcx)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x19(%rbp)
je     11b2 <readstring+0x92>
mov    -0x18(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     11b2 <readstring+0x92>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
call   1040 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
jne    11b2 <readstring+0x92>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%edx
add    %rdx,%rcx
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11c2 <readstring+0xa2>
jmp    11c7 <readstring+0xa7>
jmp    1137 <readstring+0x17>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    %esi,%eax
lea    (%rdi,%rax,1),%r14
add    $0xffffffffffffffff,%r14
mov    0x2eaf(%rip),%r15
nopl   0x0(%rax)
mov    (%r15),%edi
call   1030 <fgetc@plt>
mov    %al,(%rbx)
test   %al,%al
je     1167 <readstring+0x47>
cmp    $0xa,%al
je     1167 <readstring+0x47>
mov    (%r15),%edi
call   1040 <feof@plt>
test   %eax,%eax
jne    1167 <readstring+0x47>
add    $0x1,%rbx
cmp    %r14,%rbx
jb     1140 <readstring+0x20>
movb   $0x0,(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdin = global i32 0
define void @readstring(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
store i8* %6, i8** %5
br label %7
7: ; preds = %33, %2
%8 = load i32, i32* @stdin
%9 = call signext i8 @fgetc(i32 %8)
%10 = load i8*, i8** %5
store i8 %9, i8* %10
%11 = sext i8 %9 to i32
%12 = icmp ne i32 %11, 10
br i1 %12, label %13, label %31
13: ; preds = %7
%14 = load i8*, i8** %5
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = load i32, i32* @stdin
%20 = call i32 @feof(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %31, label %22
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %5
%25 = load i8*, i8** %3
%26 = load i32, i32* %4
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = getelementptr inbounds i8, i8* %28, i64 -1
%30 = icmp ult i8* %24, %29
br label %31
31: ; preds = %22, %18, %13, %7
%32 = phi i1 [ false, %18 ], [ false, %13 ], [ false, %7 ], [ %30, %22 ]
br i1 %32, label %33, label %34
33: ; preds = %31
br label %7
34: ; preds = %31
%35 = load i8*, i8** %5
store i8 0, i8* %35
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
define void @readstring(i8* writeonly %0, i32 %1) {
%3 = zext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = getelementptr inbounds i8, i8* %4, i64 -1
br label %6
6: ; preds = %14, %2
%7 = phi i8* [ %0, %2 ], [ %15, %14 ]
%8 = load i32, i32* @stdin
%9 = tail call signext i8 @fgetc(i32 %8)
store i8 %9, i8* %7
switch i8 %9, label %10 [
i8 10, label %17
i8 0, label %17
]
10: ; preds = %6
%11 = load i32, i32* @stdin
%12 = tail call i32 @feof(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %7, i64 1
%16 = icmp ult i8* %15, %5
br i1 %16, label %6, label %17
17: ; preds = %6, %6, %10, %14
%18 = phi i8* [ %7, %10 ], [ %15, %14 ], [ %7, %6 ], [ %7, %6 ]
store i8 0, i8* %18
ret void
}