char *fgets(buf, count, fd)
char *buf;
{
  static char bf[1024];
  static int cnt = 0;
  static char *pbf = &bf[0];
  register char *c = buf;
  while (--count) {
    if (pbf >= &bf[cnt]) {
      if ((cnt = read(fd, bf, 1024)) <= 0) {
        if (c == buf)
          return (char *)0;
        *c = 0;
        return buf;
      }
      pbf = &bf[0];
    }
    *c = *pbf++;
    if (*c++ == '\n') {
      *c = 0;
      return buf;
    }
  }
  *c = 0;
  return buf;
}


x86 assembly (clang, -O0):
<fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1210 <fgets+0x100>
mov    0x2ee5(%rip),%rax
movslq 0x32f6(%rip),%rdx
lea    0x2eef(%rip),%rcx
add    %rdx,%rcx
cmp    %rcx,%rax
jb     11bf <fgets+0xaf>
mov    -0x18(%rbp),%edi
lea    0x2ed9(%rip),%rsi
mov    $0x400,%edx
mov    $0x0,%al
call   1030 <read@plt>
mov    %eax,0x32c7(%rip)
cmp    $0x0,%eax
jg     11b1 <fgets+0xa1>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    119d <fgets+0x8d>
movq   $0x0,-0x8(%rbp)
jmp    121f <fgets+0x10f>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121f <fgets+0x10f>
lea    0x2e88(%rip),%rax
mov    %rax,0x2e69(%rip)
mov    0x2e62(%rip),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,0x2e54(%rip)
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    120b <fgets+0xfb>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    121f <fgets+0x10f>
jmp    112a <fgets+0x1a>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%r15
add    $0xffffffff,%ebp
lea    0x2f16(%rip),%r12
xor    %ebx,%ebx
jmp    114a <fgets+0x3a>
xchg   %ax,%ax
lea    0x1(%rcx),%rax
mov    %rax,0x2eed(%rip)
movzbl (%rcx),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    $0xa,%al
je     1188 <fgets+0x78>
cmp    %ebx,%ebp
je     1188 <fgets+0x78>
mov    0x2ed3(%rip),%rcx
movslq 0x32e4(%rip),%rax
add    %r12,%rax
cmp    %rax,%rcx
jb     1130 <fgets+0x20>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
xor    %eax,%eax
call   1030 <read@plt>
mov    %eax,0x32c4(%rip)
mov    %r12,%rcx
test   %eax,%eax
jg     1130 <fgets+0x20>
test   %rbx,%rbx
je     119a <fgets+0x8a>
add    %r15,%rbx
movb   $0x0,(%rbx)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
jmp    118e <fgets+0x7e>

LLVM IR (-O0):
@fgets.bf = internal global [1024 x i8] zeroinitializer
@fgets.cnt = internal global i32 0
@fgets.pbf = internal global i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @fgets.bf, i32 0, i32 0)
define i8* @fgets(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 i8*, i8** %5
store i8* %9, i8** %8
br label %10
10: ; preds = %46, %3
%11 = load i32, i32* %6
%12 = add nsw i32 %11, -1
store i32 %12, i32* %6
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %47
14: ; preds = %10
%15 = load i8*, i8** @fgets.pbf
%16 = load i32, i32* @fgets.cnt
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* @fgets.bf, i64 0, i64 %17
%19 = icmp uge i8* %15, %18
br i1 %19, label %20, label %33
20: ; preds = %14
%21 = load i32, i32* %7
%22 = call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %21, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @fgets.bf, i64 0, i64 0), i32 1024)
store i32 %22, i32* @fgets.cnt
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i8*, i8** %8
%26 = load i8*, i8** %5
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24
store i8* null, i8** %4
br label %50
29: ; preds = %24
%30 = load i8*, i8** %8
store i8 0, i8* %30
%31 = load i8*, i8** %5
store i8* %31, i8** %4
br label %50
32: ; preds = %20
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @fgets.bf, i64 0, i64 0), i8** @fgets.pbf
br label %33
33: ; preds = %32, %14
%34 = load i8*, i8** @fgets.pbf
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** @fgets.pbf
%36 = load i8, i8* %34
%37 = load i8*, i8** %8
store i8 %36, i8* %37
%38 = load i8*, i8** %8
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %8
%40 = load i8, i8* %38
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 10
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = load i8*, i8** %8
store i8 0, i8* %44
%45 = load i8*, i8** %5
store i8* %45, i8** %4
br label %50
46: ; preds = %33
br label %10
47: ; preds = %10
%48 = load i8*, i8** %8
store i8 0, i8* %48
%49 = load i8*, i8** %5
store i8* %49, i8** %4
br label %50
50: ; preds = %47, %43, %29, %28
%51 = load i8*, i8** %4
ret i8* %51
}

LLVM IR (-O2):
@fgets.bf = internal global [1024 x i8] zeroinitializer
@fgets.cnt = internal unnamed_addr global i32 0
@fgets.pbf = internal unnamed_addr global i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @fgets.bf, i64 0, i64 0)
define i8* @fgets(i8* writeonly %0, i32 %1, i32 %2) {
br label %4
4: ; preds = %20, %3
%5 = phi i32 [ %1, %3 ], [ %7, %20 ]
%6 = phi i8* [ %0, %3 ], [ %24, %20 ]
%7 = add nsw i32 %5, -1
%8 = icmp eq i32 %7, 0
br i1 %8, label %26, label %9
9: ; preds = %4
%10 = load i8*, i8** @fgets.pbf
%11 = load i32, i32* @fgets.cnt
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* @fgets.bf, i64 0, i64 %12
%14 = icmp ult i8* %10, %13
br i1 %14, label %20, label %15
15: ; preds = %9
%16 = tail call i32 (i32, i8*, i32, ...) bitcast (i32 (...)* @read to i32 (i32, i8*, i32, ...)*)(i32 %2, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @fgets.bf, i64 0, i64 0), i32 1024)
store i32 %16, i32* @fgets.cnt
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = icmp eq i8* %6, %0
br i1 %19, label %28, label %26
20: ; preds = %15, %9
%21 = phi i8* [ %10, %9 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @fgets.bf, i64 0, i64 0), %15 ]
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** @fgets.pbf
%23 = load i8, i8* %21
store i8 %23, i8* %6
%24 = getelementptr inbounds i8, i8* %6, i64 1
%25 = icmp eq i8 %23, 10
br i1 %25, label %26, label %4
26: ; preds = %4, %20, %18
%27 = phi i8* [ %6, %18 ], [ %24, %20 ], [ %6, %4 ]
store i8 0, i8* %27
br label %28
28: ; preds = %26, %18
%29 = phi i8* [ null, %18 ], [ %0, %26 ]
ret i8* %29
}