#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char getc(int /*<<< orphan*/ *);
char getchar();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int main() {
  FILE *opw, *opr;
  char c;
  int count = 0;
  opw = fopen("count.txt", "w");
  printf("Enter name: ");
  do {
    putc(c = getchar(), opw);
  } while (c != '\n');
  fclose(opw);
  opr = fopen("count.txt", "r");
  while (c = getc(opr) != '\n') {
    count++;
  }
  printf("Number of characters: %d\n", count);
  fclose(opr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe83(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rsi
movsbl %al,%edi
call   1050 <putc@plt>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
jne    119b <main+0x3b>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe32(%rip),%rdi
lea    0xe44(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <getc@plt>
movsbl %al,%eax
cmp    $0xa,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %al,-0x19(%rbp)
cmp    $0x0,%al
je     120e <main+0xae>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11de <main+0x7e>
mov    -0x20(%rbp),%esi
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe96(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0xe8c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1060 <getchar@plt>
movsbl %al,%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <putc@plt>
cmp    $0xa,%bpl
jne    1190 <main+0x30>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe47(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
nopl   (%rax)
mov    %rbx,%rdi
call   1080 <getc@plt>
add    $0x1,%ebp
cmp    $0xa,%al
jne    11d0 <main+0x70>
lea    0xe35(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"count.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"Enter name: \00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [26 x i8] c"Number of characters: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i8
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %5
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %8
8: ; preds = %12, %0
%9 = call signext i8 (...) @getchar()
store i8 %9, i8* %4
%10 = load i32*, i32** %2
%11 = call i32 @putc(i8 signext %9, i32* %10)
br label %12
12: ; preds = %8
%13 = load i8, i8* %4
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 10
br i1 %15, label %8, label %16
16: ; preds = %12
%17 = load i32*, i32** %2
%18 = call i32 @fclose(i32* %17)
%19 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %19, i32** %3
br label %20
20: ; preds = %28, %16
%21 = load i32*, i32** %3
%22 = call signext i8 @getc(i32* %21)
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 10
%25 = zext i1 %24 to i32
%26 = trunc i32 %25 to i8
store i8 %26, i8* %4
%27 = icmp ne i8 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
br label %20
31: ; preds = %20
%32 = load i32, i32* %5
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = load i32*, i32** %3
%35 = call i32 @fclose(i32* %34)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"count.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [13 x i8] c"Enter name: \00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [26 x i8] c"Number of characters: %d\0A\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %3
3: ; preds = %3, %0
%4 = tail call signext i8 (...) @getchar()
%5 = tail call i32 @putc(i8 signext %4, i32* %1)
%6 = icmp eq i8 %4, 10
br i1 %6, label %7, label %3
7: ; preds = %3
%8 = tail call i32 @fclose(i32* %1)
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %10
10: ; preds = %10, %7
%11 = phi i32 [ 0, %7 ], [ %14, %10 ]
%12 = tail call signext i8 @getc(i32* %9)
%13 = icmp eq i8 %12, 10
%14 = add nuw nsw i32 %11, 1
br i1 %13, label %15, label %10
15: ; preds = %10
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %11)
%17 = tail call i32 @fclose(i32* %9)
ret i32 0
}