#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
int /*<<< orphan*/ printf(char *, ...);
int count(void) {
  FILE *data_ptr;
  int count;
  printf("Content-type: text/plain\n\n");
  if (!(data_ptr = fopen("tcount.dat", "r"))) {
    printf("Error opening tcount.dat for reading!\n");
    printf("Error 001: Exiting.\n");
    return 1;
  } else {
    fscanf(data_ptr, "%i\n", &count);
    printf("%i\n", ++count);
    fclose(data_ptr);
    if (!(data_ptr = fopen("tcount.dat", "w"))) {
      printf("Error opening tcount.dat for writing!\n");
      printf("Error 002: Exiting.\n");
      return 2;
    } else
      fprintf(data_ptr, "%i\n", count);
  }
  return 0;
}


x86 assembly (clang, -O0):
<count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeae(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11af <count+0x5f>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <count+0xfb>
mov    -0x10(%rbp),%rdi
lea    0xeaa(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe31(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    122c <count+0xdc>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    124b <count+0xfb>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe2a(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1244 <count+0xf4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe85(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f2 <count+0x92>
mov    %rax,%rbx
lea    0xe77(%rip),%r14
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <fscanf@plt>
mov    0xc(%rsp),%esi
add    $0x1,%esi
mov    %esi,0xc(%rsp)
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe34(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1211 <count+0xb1>
mov    0xc(%rsp),%edx
lea    0xe25(%rip),%rsi
mov    %rax,%rdi
call   1070 <fprintf@plt>
jmp    122e <count+0xce>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    122e <count+0xce>
lea    0xe4f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Content-type: text/plain\0A\0A\00"
@.str.1 = [11 x i8] c"tcount.dat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [39 x i8] c"Error opening tcount.dat for reading!\0A\00"
@.str.4 = [21 x i8] c"Error 001: Exiting.\0A\00"
@.str.5 = [4 x i8] c"%i\0A\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [39 x i8] c"Error opening tcount.dat for writing!\0A\00"
@.str.8 = [21 x i8] c"Error 002: Exiting.\0A\00"
define i32 @count() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%5 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp ne i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %29
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @fscanf(i32* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
store i32 %14, i32* %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %14)
%16 = load i32*, i32** %2
%17 = call i32 @fclose(i32* %16)
%18 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %18, i32** %2
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %10
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
store i32 2, i32* %1
br label %29
23: ; preds = %10
%24 = load i32*, i32** %2
%25 = load i32, i32* %3
%26 = call i32 @fprintf(i32* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %23
br label %28
28: ; preds = %27
store i32 0, i32* %1
br label %29
29: ; preds = %28, %20, %7
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"tcount.dat\00"
@.str.2 = [2 x i8] c"r\00"
@.str.5 = [4 x i8] c"%i\0A\00"
@.str.6 = [2 x i8] c"w\00"
@str = [26 x i8] c"Content-type: text/plain\0A\00"
@str.9 = [38 x i8] c"Error opening tcount.dat for reading!\00"
@str.10 = [20 x i8] c"Error 001: Exiting.\00"
@str.11 = [38 x i8] c"Error opening tcount.dat for writing!\00"
@str.12 = [20 x i8] c"Error 002: Exiting.\00"
define i32 @count() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.9, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
br label %23
9: ; preds = %0
%10 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* %1)
%11 = load i32, i32* %1
%12 = add nsw i32 %11, 1
store i32 %12, i32* %1
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %12)
%14 = call i32 @fclose(i32* %4)
%15 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %9
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.11, i64 0, i64 0))
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0))
br label %23
20: ; preds = %9
%21 = load i32, i32* %1
%22 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %17, %6
%24 = phi i32 [ 0, %20 ], [ 2, %17 ], [ 1, %6 ]
ret i32 %24
}