#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 /*<<< orphan*/ SEEK_CUR;
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
void l9_fgets(char *s, int n, FILE *f) {
  int c = '\0';
  int count = 0;
  while ((c != '\n') && (c != '\r') && (c != EOF) && (count < n - 1)) {
    c = fgetc(f);
    *s++ = c;
    count++;
  }
  *s = '\0';
  if (c == EOF) {
    s--;
    *s = '\n';
  } else if (c == '\r') {
    s--;
    *s = '\n';
    c = fgetc(f);
    if ((c != '\r') && (c != EOF))
      fseek(f, -1, SEEK_CUR);
  }
}


x86 assembly (clang, -O0):
<l9_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0xa,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
je     1187 <l9_fgets+0x67>
xor    %eax,%eax
cmpl   $0xd,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
je     1187 <l9_fgets+0x67>
mov    -0x1c(%rbp),%ecx
xor    %eax,%eax
mov    0x2e75(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x21(%rbp)
je     1187 <l9_fgets+0x67>
mov    -0x20(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1197 <l9_fgets+0x77>
jmp    11c7 <l9_fgets+0xa7>
mov    -0x18(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1141 <l9_fgets+0x21>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%eax
jne    11f8 <l9_fgets+0xd8>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0xa,(%rax)
jmp    125e <l9_fgets+0x13e>
cmpl   $0xd,-0x1c(%rbp)
jne    1259 <l9_fgets+0x139>
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movb   $0xa,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xd,-0x1c(%rbp)
je     1254 <l9_fgets+0x134>
mov    -0x1c(%rbp),%eax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%eax
je     1254 <l9_fgets+0x134>
mov    -0x18(%rbp),%rdi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1040 <fseek@plt>
jmp    1259 <l9_fgets+0x139>
jmp    125e <l9_fgets+0x13e>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<l9_fgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r12
add    $0xffffffff,%ebp
xor    %ebx,%ebx
mov    0x2ea4(%rip),%r15
xor    %eax,%eax
cmp    $0xa,%eax
je     1173 <l9_fgets+0x53>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0xd,%eax
je     1173 <l9_fgets+0x53>
cmp    (%r15),%eax
je     1173 <l9_fgets+0x53>
cmp    %ebp,%ebx
jge    1173 <l9_fgets+0x53>
mov    %r14,%rdi
call   1030 <fgetc@plt>
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%eax
jne    1150 <l9_fgets+0x30>
movb   $0x0,(%r12,%rbx,1)
cmp    (%r15),%eax
jne    1185 <l9_fgets+0x65>
movb   $0xa,-0x1(%r12,%rbx,1)
jmp    11c0 <l9_fgets+0xa0>
cmp    $0xd,%eax
jne    11c0 <l9_fgets+0xa0>
movb   $0xa,-0x1(%r12,%rbx,1)
mov    %r14,%rdi
call   1030 <fgetc@plt>
cmp    $0xd,%eax
je     11c0 <l9_fgets+0xa0>
cmp    (%r15),%eax
je     11c0 <l9_fgets+0xa0>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0xffffffff,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fseek@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@SEEK_CUR = global i32 0
define void @l9_fgets(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %26, %3
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %24
12: ; preds = %9
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 13
br i1 %14, label %15, label %24
15: ; preds = %12
%16 = load i32, i32* %7
%17 = load i32, i32* @EOF
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* %8
%21 = load i32, i32* %5
%22 = sub nsw i32 %21, 1
%23 = icmp slt i32 %20, %22
br label %24
24: ; preds = %19, %15, %12, %9
%25 = phi i1 [ false, %15 ], [ false, %12 ], [ false, %9 ], [ %23, %19 ]
br i1 %25, label %26, label %35
26: ; preds = %24
%27 = load i32*, i32** %6
%28 = call i32 @fgetc(i32* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = trunc i32 %29 to i8
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %4
store i8 %30, i8* %31
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
store i32 %34, i32* %8
br label %9
35: ; preds = %24
%36 = load i8*, i8** %4
store i8 0, i8* %36
%37 = load i32, i32* %7
%38 = load i32, i32* @EOF
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %35
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 -1
store i8* %42, i8** %4
%43 = load i8*, i8** %4
store i8 10, i8* %43
br label %65
44: ; preds = %35
%45 = load i32, i32* %7
%46 = icmp eq i32 %45, 13
br i1 %46, label %47, label %64
47: ; preds = %44
%48 = load i8*, i8** %4
%49 = getelementptr inbounds i8, i8* %48, i32 -1
store i8* %49, i8** %4
%50 = load i8*, i8** %4
store i8 10, i8* %50
%51 = load i32*, i32** %6
%52 = call i32 @fgetc(i32* %51)
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = icmp ne i32 %53, 13
br i1 %54, label %55, label %63
55: ; preds = %47
%56 = load i32, i32* %7
%57 = load i32, i32* @EOF
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = load i32*, i32** %6
%61 = load i32, i32* @SEEK_CUR
%62 = call i32 @fseek(i32* %60, i32 -1, i32 %61)
br label %63
63: ; preds = %59, %55, %47
br label %64
64: ; preds = %63, %44
br label %65
65: ; preds = %64, %40
ret void
}

LLVM IR (-O2):
@EOF = global i32 0
@SEEK_CUR = global i32 0
define void @l9_fgets(i8* nocapture writeonly %0, i32 %1, i32* nocapture %2) {
%4 = add nsw i32 %1, -1
br label %5
5: ; preds = %14, %3
%6 = phi i8* [ %0, %3 ], [ %17, %14 ]
%7 = phi i32 [ 0, %3 ], [ %15, %14 ]
%8 = phi i32 [ 0, %3 ], [ %18, %14 ]
switch i32 %7, label %9 [
i32 13, label %19
i32 10, label %19
]
9: ; preds = %5
%10 = load i32, i32* @EOF
%11 = icmp ne i32 %7, %10
%12 = icmp slt i32 %8, %4
%13 = select i1 %11, i1 %12, i1 false
br i1 %13, label %14, label %19
14: ; preds = %9
%15 = tail call i32 @fgetc(i32* %2)
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds i8, i8* %6, i64 1
store i8 %16, i8* %6
%18 = add nuw nsw i32 %8, 1
br label %5
19: ; preds = %5, %5, %9
store i8 0, i8* %6
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %7, %20
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = getelementptr inbounds i8, i8* %6, i64 -1
store i8 10, i8* %23
br label %36
24: ; preds = %19
%25 = icmp eq i32 %7, 13
br i1 %25, label %26, label %36
26: ; preds = %24
%27 = getelementptr inbounds i8, i8* %6, i64 -1
store i8 10, i8* %27
%28 = tail call i32 @fgetc(i32* %2)
%29 = icmp eq i32 %28, 13
%30 = load i32, i32* @EOF
%31 = icmp eq i32 %28, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %36, label %33
33: ; preds = %26
%34 = load i32, i32* @SEEK_CUR
%35 = tail call i32 @fseek(i32* %2, i32 -1, i32 %34)
br label %36
36: ; preds = %24, %33, %26, %22
ret void
}