#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int fgetc(int /*<<< orphan*/ *);
int fgets2(FILE *in, char *buf, size_t n) {
  int count = 0, c;
  while (n-- > 0) {
    if ((c = fgetc(in)) == EOF)
      return -1;
    buf[count++] = (char)c;
    if (c == '\n')
      return count;
  }
  return count;
}


x86 assembly (clang, -O0):
<fgets2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
jbe    119f <fgets2+0x8f>
mov    -0x10(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e91(%rip),%rcx
cmp    (%rcx),%eax
jne    116b <fgets2+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a5 <fgets2+0x95>
mov    -0x28(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0xa,-0x28(%rbp)
jne    119a <fgets2+0x8a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11a5 <fgets2+0x95>
jmp    112b <fgets2+0x1b>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets2>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
xor    %ebx,%ebx
mov    0x2ebd(%rip),%r13
nopl   0x0(%rax,%rax,1)
cmp    %rbx,%r15
je     115b <fgets2+0x4b>
mov    %r12,%rdi
call   1030 <fgetc@plt>
cmp    0x0(%r13),%eax
je     1155 <fgets2+0x45>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%eax
jne    1130 <fgets2+0x20>
mov    %ebx,%r15d
jmp    115b <fgets2+0x4b>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EOF = global i32 0
define i32 @fgets2(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
br label %10
10: ; preds = %32, %3
%11 = load i64, i64* %7
%12 = add i64 %11, -1
store i64 %12, i64* %7
%13 = icmp ugt i64 %11, 0
br i1 %13, label %14, label %33
14: ; preds = %10
%15 = load i32*, i32** %5
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %9
%17 = load i32, i32* @EOF
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 -1, i32* %4
br label %35
20: ; preds = %14
%21 = load i32, i32* %9
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %6
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %35
32: ; preds = %20
br label %10
33: ; preds = %10
%34 = load i32, i32* %8
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %30, %19
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@EOF = global i32 0
define i32 @fgets2(i32* nocapture %0, i8* nocapture writeonly %1, i64 %2) {
%4 = trunc i64 %2 to i32
br label %5
5: ; preds = %14, %3
%6 = phi i64 [ %16, %14 ], [ 0, %3 ]
%7 = phi i64 [ %8, %14 ], [ %2, %3 ]
%8 = add i64 %7, -1
%9 = icmp eq i64 %7, 0
br i1 %9, label %21, label %10
10: ; preds = %5
%11 = tail call i32 @fgetc(i32* %0)
%12 = load i32, i32* @EOF
%13 = icmp eq i32 %11, %12
br i1 %13, label %21, label %14
14: ; preds = %10
%15 = trunc i32 %11 to i8
%16 = add nuw i64 %6, 1
%17 = getelementptr inbounds i8, i8* %1, i64 %6
store i8 %15, i8* %17
%18 = icmp eq i32 %11, 10
br i1 %18, label %19, label %5
19: ; preds = %14
%20 = trunc i64 %16 to i32
br label %21
21: ; preds = %10, %5, %19
%22 = phi i32 [ %20, %19 ], [ %4, %5 ], [ -1, %10 ]
ret i32 %22
}