#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 */
scalar_t__ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
char *fgetstr(char *line, unsigned int maxsize, FILE *f) {
  char *p;
  if (feof(f) || maxsize == 0)
    return (NULL);
  for (p = line; !feof(f) && maxsize > 1; maxsize--)
    if ((*p++ = fgetc(f)) == '\n')
      break;
  *p = '\0';
  return (line);
}


x86 assembly (clang, -O0):
<fgetstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <feof@plt>
cmp    $0x0,%rax
jne    1150 <fgetstr+0x30>
cmpl   $0x0,-0x14(%rbp)
jne    115d <fgetstr+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <fgetstr+0xc7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <feof@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x29(%rbp)
jne    118a <fgetstr+0x6a>
cmpl   $0x1,-0x14(%rbp)
seta   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    119a <fgetstr+0x7a>
jmp    11d8 <fgetstr+0xb8>
mov    -0x20(%rbp),%rdi
call   1030 <fgetc@plt>
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0xa,%eax
jne    11c5 <fgetstr+0xa5>
jmp    11d8 <fgetstr+0xb8>
jmp    11ca <fgetstr+0xaa>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1165 <fgetstr+0x45>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetstr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%r14
mov    %rdx,%rdi
call   1040 <feof@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %r12d,%r12d
je     11a7 <fgetstr+0x87>
test   %rcx,%rcx
jne    11a7 <fgetstr+0x87>
mov    %r15,%rdi
call   1040 <feof@plt>
mov    %r14,%rbx
test   %rax,%rax
jne    11a1 <fgetstr+0x81>
mov    %r14,%rbx
cmp    $0x2,%r12d
jb     11a1 <fgetstr+0x81>
lea    0x1(%r14),%rax
add    $0xffffffff,%r12d
nopl   0x0(%rax)
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <fgetc@plt>
mov    %al,-0x1(%rbx)
cmp    $0xa,%al
je     11a1 <fgetstr+0x81>
mov    %r15,%rdi
call   1040 <feof@plt>
test   %rax,%rax
jne    11a1 <fgetstr+0x81>
lea    0x1(%rbx),%rax
lea    -0x1(%r12),%ecx
cmp    $0x2,%r12d
mov    %ecx,%r12d
jae    1170 <fgetstr+0x50>
movb   $0x0,(%rbx)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @fgetstr(i8* writeonly %0, i32 %1, i32* nocapture %2) {
%4 = tail call i64 @feof(i32* %2)
%5 = icmp ne i64 %4, 0
%6 = icmp eq i32 %1, 0
%7 = or i1 %6, %5
br i1 %7, label %27, label %8
8: ; preds = %3
%9 = tail call i64 @feof(i32* %2)
%10 = icmp eq i64 %9, 0
%11 = icmp ugt i32 %1, 1
%12 = and i1 %10, %11
br i1 %12, label %18, label %25
13: ; preds = %18
%14 = tail call i64 @feof(i32* %2)
%15 = icmp eq i64 %14, 0
%16 = icmp ugt i32 %24, 1
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %25
18: ; preds = %8, %13
%19 = phi i8* [ %22, %13 ], [ %0, %8 ]
%20 = phi i32 [ %24, %13 ], [ %1, %8 ]
%21 = tail call signext i8 @fgetc(i32* %2)
%22 = getelementptr inbounds i8, i8* %19, i64 1
store i8 %21, i8* %19
%23 = icmp eq i8 %21, 10
%24 = add i32 %20, -1
br i1 %23, label %25, label %13
25: ; preds = %13, %18, %8
%26 = phi i8* [ %0, %8 ], [ %22, %18 ], [ %22, %13 ]
store i8 0, i8* %26
br label %27
27: ; preds = %3, %25
%28 = phi i8* [ %0, %25 ], [ null, %3 ]
ret i8* %28
}