#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 getc(int /*<<< orphan*/ *);
char *fgets2(char *s, int n, FILE *iop) {
  register int c;
  register char *cs;
  cs = s;
  while (--n > 0 && (c = getc(iop)) != EOF)
    if ((*cs++ = c) == '\n')
      break;
  *cs = '\0';
  return (c == EOF && cs == s) ? NULL : s;
}


x86 assembly (clang, -O0):
<fgets2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jle    115d <fgets2+0x4d>
mov    -0x18(%rbp),%rdi
call   1030 <getc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e93(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    116d <fgets2+0x5d>
jmp    1197 <fgets2+0x87>
mov    -0x1c(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0xa,%eax
jne    1192 <fgets2+0x82>
jmp    1197 <fgets2+0x87>
jmp    112b <fgets2+0x1b>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%eax
jne    11c9 <fgets2+0xb9>
mov    -0x28(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11c9 <fgets2+0xb9>
xor    %eax,%eax
mov    %rax,-0x38(%rbp)
jmp    11d1 <fgets2+0xc1>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets2>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    %rdi,%rbx
cmp    $0x2,%esi
jl     1166 <fgets2+0x56>
mov    %rdx,%r15
mov    %esi,%r12d
mov    0x2eb7(%rip),%r13
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
call   1030 <getc@plt>
cmp    0x0(%r13),%eax
je     1166 <fgets2+0x56>
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    $0xa,%al
je     1166 <fgets2+0x56>
lea    -0x1(%r12),%ecx
cmp    $0x2,%r12d
mov    %ecx,%r12d
jg     1140 <fgets2+0x30>
movb   $0x0,(%rbx)
mov    0x2e78(%rip),%rdx
xor    %ecx,%ecx
cmp    (%rdx),%eax
cmovne %r14,%rcx
cmp    %r14,%rbx
cmovne %r14,%rcx
mov    %rcx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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