#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*/ exit(int);
scalar_t__ fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdin;
int nueva_linea(char *linea, int len) {
  int i = 0;
  while (i < len) {
    if ((linea[i] = (char)fgetc(stdin)) == -1) {
      perror("fgetc");
      exit(-1);
    }
    if (linea[i] == '\n') {
      linea[i] = '\0';
      return (i);
    }
    i++;
    if (i == len)
      linea[i] = '\0';
  }
  return (i);
}


x86 assembly (clang, -O0):
<nueva_linea>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11df <nueva_linea+0xaf>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    -0x10(%rbp),%rcx
movslq -0x18(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
cmp    $0xffffffff,%eax
jne    118d <nueva_linea+0x5d>
lea    0xe82(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11b9 <nueva_linea+0x89>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e5 <nueva_linea+0xb5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11da <nueva_linea+0xaa>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1146 <nueva_linea+0x16>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nueva_linea>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %esi,%esi
jle    118d <nueva_linea+0x5d>
mov    %rdi,%r14
mov    %esi,%r15d
lea    -0x1(%r15),%r12
xor    %ebx,%ebx
mov    0x2e98(%rip),%r13
jmp    1168 <nueva_linea+0x38>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rax,%rbx
cmp    %rax,%r15
je     1198 <nueva_linea+0x68>
mov    0x0(%r13),%edi
call   1030 <fgetc@plt>
mov    %al,(%r14,%rbx,1)
cmp    $0xa,%al
je     1191 <nueva_linea+0x61>
cmp    $0xff,%al
je     11a7 <nueva_linea+0x77>
lea    0x1(%rbx),%rax
cmp    %rbx,%r12
jne    1160 <nueva_linea+0x30>
movb   $0x0,(%r14,%r15,1)
jmp    1160 <nueva_linea+0x30>
xor    %ebx,%ebx
jmp    119b <nueva_linea+0x6b>
movb   $0x0,(%r14,%rbx,1)
jmp    119b <nueva_linea+0x6b>
mov    %r15d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe52(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [6 x i8] c"fgetc\00"
define i32 @nueva_linea(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %49, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %5
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %50
11: ; preds = %7
%12 = load i32, i32* @stdin
%13 = call i64 @fgetc(i32 %12)
%14 = trunc i64 %13 to i8
%15 = load i8*, i8** %4
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 %14, i8* %18
%19 = sext i8 %14 to i32
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %11
%25 = load i8*, i8** %4
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, 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 %38
32: ; preds = %24
%33 = load i8*, i8** %4
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
%37 = load i32, i32* %6
store i32 %37, i32* %3
br label %52
38: ; preds = %24
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = load i32, i32* %5
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i8*, i8** %4
%46 = load i32, i32* %6
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
br label %49
49: ; preds = %44, %38
br label %7
50: ; preds = %7
%51 = load i32, i32* %6
store i32 %51, i32* %3
br label %52
52: ; preds = %50, %32
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [6 x i8] c"fgetc\00"
define i32 @nueva_linea(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %26
4: ; preds = %2
%5 = zext i32 %1 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
br label %7
7: ; preds = %4, %24
%8 = phi i64 [ 0, %4 ], [ %21, %24 ]
%9 = load i32, i32* @stdin
%10 = tail call i64 @fgetc(i32 %9)
%11 = trunc i64 %10 to i8
%12 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %11, i8* %12
switch i8 %11, label %20 [
i8 -1, label %13
i8 10, label %16
]
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %7
%17 = trunc i64 %8 to i32
%18 = and i64 %8, 4294967295
%19 = getelementptr inbounds i8, i8* %0, i64 %18
store i8 0, i8* %19
br label %26
20: ; preds = %7
%21 = add nuw nsw i64 %8, 1
%22 = icmp eq i64 %21, %5
br i1 %22, label %23, label %24
23: ; preds = %20
store i8 0, i8* %6
br label %24
24: ; preds = %23, %20
%25 = icmp eq i64 %21, %5
br i1 %25, label %26, label %7
26: ; preds = %24, %2, %16
%27 = phi i32 [ %17, %16 ], [ 0, %2 ], [ %1, %24 ]
ret i32 %27
}