#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 */

/* Variables and functions */
scalar_t__ read(int, char *, int);
size_t fgetcounts(int fd, size_t *words, size_t *lines) {
  char ch;
  size_t chars = 0;
  *words = 1;
  *lines = 1;
  while (read(fd, &ch, 1)) {
    if (ch == '\n') {
      (*lines)++;
      (*words)++;
    } else if (ch == ' ')
      (*words)++;
    chars++;
  }
  return chars;
}


x86 assembly (clang, -O0):
<fgetcounts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x1,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x1,(%rax)
mov    -0x4(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
je     11bb <fgetcounts+0xab>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    118a <fgetcounts+0x7a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    11aa <fgetcounts+0x9a>
movsbl -0x19(%rbp),%eax
cmp    $0x20,%eax
jne    11a5 <fgetcounts+0x95>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    11aa <fgetcounts+0x9a>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1141 <fgetcounts+0x31>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetcounts>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
movq   $0x1,(%rsi)
movq   $0x1,(%rdx)
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     117f <fgetcounts+0x6f>
lea    0xf(%rsp),%r12
jmp    116c <fgetcounts+0x5c>
nop
addq   $0x1,(%r15)
add    $0x1,%rbx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     117f <fgetcounts+0x6f>
movzbl 0xf(%rsp),%eax
cmp    $0x20,%al
je     1150 <fgetcounts+0x40>
cmp    $0xa,%al
jne    1154 <fgetcounts+0x44>
addq   $0x1,(%r14)
jmp    1150 <fgetcounts+0x40>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @fgetcounts(i32 %0, i64* nocapture %1, i64* nocapture %2) {
%4 = alloca i8
store i64 1, i64* %1
store i64 1, i64* %2
%5 = call i64 @read(i32 %0, i8* %4, i32 1)
%6 = icmp eq i64 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %3, %16
%8 = phi i64 [ %17, %16 ], [ 0, %3 ]
%9 = load i8, i8* %4
switch i8 %9, label %16 [
i8 10, label %10
i8 32, label %13
]
10: ; preds = %7
%11 = load i64, i64* %2
%12 = add i64 %11, 1
store i64 %12, i64* %2
br label %13
13: ; preds = %7, %10
%14 = load i64, i64* %1
%15 = add i64 %14, 1
store i64 %15, i64* %1
br label %16
16: ; preds = %13, %7
%17 = add i64 %8, 1
%18 = call i64 @read(i32 %0, i8* %4, i32 1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %7
20: ; preds = %16, %3
%21 = phi i64 [ 0, %3 ], [ %17, %16 ]
ret i64 %21
}