#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 */
char EOF;
scalar_t__ ceil(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char getc(int /*<<< orphan*/ *);
scalar_t__ isspace(char);
int /*<<< orphan*/ log10(int);
int /*<<< orphan*/ printf(char *, int, int, int, ...);
scalar_t__ strcmp(char *, char const *);
void wc(const char *filename) {
  char prev = '\n';
  char curr = '\0';
  int in_word = 0;
  int chars = 0;
  int words = 0;
  int lines = 0;
  FILE *file = fopen(filename, "r");
  do {
    prev = curr;
    curr = getc(file);
    if ('\0' != curr)
      ++chars;
    if ('\n' == curr)
      ++lines;
    if (in_word && isspace(curr)) {
      in_word = 1;
      ++words;
    } else if (!in_word && !isspace(curr)) {
      in_word = 0;
    }
    in_word = !isspace(curr);
  } while (curr != EOF);
  fclose(file);
  int width = (int)(ceil(log10(chars)) - ceil(log10(lines)));
  if (strcmp("/dev/stdin", filename) == 0) {
    printf("%d  %d  %d\n", lines, words, chars);
  } else {
    printf("%d  %d  %d  %s\n", lines, words, chars, filename);
  }
}


x86 assembly (clang, -O0):
<wc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0xa,-0x9(%rbp)
movb   $0x0,-0xa(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0xa(%rbp),%al
mov    %al,-0x9(%rbp)
mov    -0x28(%rbp),%rdi
call   10a0 <getc@plt>
mov    %al,-0xa(%rbp)
movsbl -0xa(%rbp),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     11ed <wc+0x6d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
movsbl -0xa(%rbp),%ecx
mov    $0xa,%eax
cmp    %ecx,%eax
jne    1207 <wc+0x87>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1239 <wc+0xb9>
movsbl -0xa(%rbp),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
je     1239 <wc+0xb9>
movl   $0x1,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1262 <wc+0xe2>
cmpl   $0x0,-0x10(%rbp)
jne    125d <wc+0xdd>
movsbl -0xa(%rbp),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
jne    125d <wc+0xdd>
movl   $0x0,-0x10(%rbp)
jmp    1262 <wc+0xe2>
movsbl -0xa(%rbp),%edi
call   1060 <isspace@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
movsbl -0xa(%rbp),%eax
mov    0x2d61(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11c4 <wc+0x44>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%edi
call   1090 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1090 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xd2d(%rip),%rdi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1300 <wc+0x180>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    131b <wc+0x19b>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x8(%rbp),%r8
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe6e(%rip),%rsi
mov    %rdi,(%rsp)
call   1080 <fopen@plt>
mov    %rax,%r12
xor    %r14d,%r14d
xor    %r15d,%r15d
xor    %r13d,%r13d
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %r12,%rdi
call   10a0 <getc@plt>
cmp    $0x1,%al
sbb    $0xffffffff,%r15d
xor    %ecx,%ecx
cmp    $0xa,%al
sete   %cl
add    %ecx,%ebp
test   %r14d,%r14d
setne  %r14b
movsbl %al,%ebx
mov    %ebx,%edi
call   1060 <isspace@plt>
test   %rax,%rax
setne  %al
and    %r14b,%al
movzbl %al,%eax
add    %eax,%r13d
mov    %ebx,%edi
call   1060 <isspace@plt>
xor    %r14d,%r14d
test   %rax,%rax
sete   %r14b
mov    0x2de9(%rip),%rax
cmp    (%rax),%bl
jne    11b0 <wc+0x30>
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    %r15d,%edi
call   1090 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
mov    %ebp,%edi
call   1090 <log10@plt>
mov    %eax,%edi
call   1030 <ceil@plt>
lea    0xdd3(%rip),%rdi
mov    (%rsp),%rbx
mov    %rbx,%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1267 <wc+0xe7>
lea    0xdd2(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
mov    %r15d,%ecx
mov    %rbx,%r8
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
lea    0xd9f(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
mov    %r15d,%ecx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.1 = [11 x i8] c"/dev/stdin\00"
@.str.2 = [12 x i8] c"%d %d %d\0A\00"
@.str.3 = [16 x i8] c"%d %d %d %s\0A\00"
define void @wc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8
%4 = alloca i8
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %2
store i8 10, i8* %3
store i8 0, i8* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %9
br label %13
13: ; preds = %55, %1
%14 = load i8, i8* %4
store i8 %14, i8* %3
%15 = load i32*, i32** %9
%16 = call signext i8 @getc(i32* %15)
store i8 %16, i8* %4
%17 = load i8, i8* %4
%18 = sext i8 %17 to i32
%19 = icmp ne i32 0, %18
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
store i32 %22, i32* %6
br label %23
23: ; preds = %20, %13
%24 = load i8, i8* %4
%25 = sext i8 %24 to i32
%26 = icmp eq i32 10, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %30
30: ; preds = %27, %23
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %30
%34 = load i8, i8* %4
%35 = call i64 @isspace(i8 signext %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
store i32 1, i32* %5
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
store i32 %39, i32* %7
br label %49
40: ; preds = %33, %30
%41 = load i32, i32* %5
%42 = icmp ne i32 %41, 0
br i1 %42, label %48, label %43
43: ; preds = %40
%44 = load i8, i8* %4
%45 = call i64 @isspace(i8 signext %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %48, label %47
47: ; preds = %43
store i32 0, i32* %5
br label %48
48: ; preds = %47, %43, %40
br label %49
49: ; preds = %48, %37
%50 = load i8, i8* %4
%51 = call i64 @isspace(i8 signext %50)
%52 = icmp ne i64 %51, 0
%53 = xor i1 %52, true
%54 = zext i1 %53 to i32
store i32 %54, i32* %5
br label %55
55: ; preds = %49
%56 = load i8, i8* %4
%57 = sext i8 %56 to i32
%58 = load i8, i8* @EOF
%59 = sext i8 %58 to i32
%60 = icmp ne i32 %57, %59
br i1 %60, label %13, label %61
61: ; preds = %55
%62 = load i32*, i32** %9
%63 = call i32 @fclose(i32* %62)
%64 = load i32, i32* %6
%65 = call i32 @log10(i32 %64)
%66 = call i64 @ceil(i32 %65)
%67 = load i32, i32* %8
%68 = call i32 @log10(i32 %67)
%69 = call i64 @ceil(i32 %68)
%70 = sub nsw i64 %66, %69
%71 = trunc i64 %70 to i32
store i32 %71, i32* %10
%72 = load i8*, i8** %2
%73 = call i64 @strcmp(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %72)
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %61
%76 = load i32, i32* %8
%77 = load i32, i32* %7
%78 = load i32, i32* %6
%79 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %76, i32 %77, i32 %78)
br label %86
80: ; preds = %61
%81 = load i32, i32* %8
%82 = load i32, i32* %7
%83 = load i32, i32* %6
%84 = load i8*, i8** %2
%85 = call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %81, i32 %82, i32 %83, i8* %84)
br label %86
86: ; preds = %80, %75
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
@.str.1 = [11 x i8] c"/dev/stdin\00"
@.str.2 = [12 x i8] c"%d %d %d\0A\00"
@.str.3 = [16 x i8] c"%d %d %d %s\0A\00"
define void @wc(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %3
3: ; preds = %3, %1
%4 = phi i32 [ 0, %1 ], [ %23, %3 ]
%5 = phi i32 [ 0, %1 ], [ %11, %3 ]
%6 = phi i32 [ 0, %1 ], [ %20, %3 ]
%7 = phi i32 [ 0, %1 ], [ %14, %3 ]
%8 = tail call signext i8 @getc(i32* %2)
%9 = icmp ne i8 %8, 0
%10 = zext i1 %9 to i32
%11 = add nuw nsw i32 %5, %10
%12 = icmp eq i8 %8, 10
%13 = zext i1 %12 to i32
%14 = add nuw nsw i32 %7, %13
%15 = icmp ne i32 %4, 0
%16 = tail call i64 @isspace(i8 signext %8)
%17 = icmp ne i64 %16, 0
%18 = select i1 %15, i1 %17, i1 false
%19 = zext i1 %18 to i32
%20 = add nuw nsw i32 %6, %19
%21 = tail call i64 @isspace(i8 signext %8)
%22 = icmp eq i64 %21, 0
%23 = zext i1 %22 to i32
%24 = load i8, i8* @EOF
%25 = icmp eq i8 %8, %24
br i1 %25, label %26, label %3
26: ; preds = %3
%27 = tail call i32 @fclose(i32* %2)
%28 = tail call i32 @log10(i32 %11)
%29 = tail call i64 @ceil(i32 %28)
%30 = tail call i32 @log10(i32 %14)
%31 = tail call i64 @ceil(i32 %30)
%32 = tail call i64 @strcmp(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = tail call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %20, i32 %11)
br label %38
36: ; preds = %26
%37 = tail call i32 (i8*, i32, i32, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %14, i32 %20, i32 %11, i8* %0)
br label %38
38: ; preds = %36, %34
ret void
}