#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
scalar_t__ strcmp(char *, char *);
int strlen(char *);
int tell(void) {
  FILE *data, *counter;
  char *joke;
  unsigned int a;
  counter = fopen("counter.txt", "r");
  fscanf(counter, "%d", &a);
  fclose(counter);
  if ((joke = malloc(101 * sizeof(char))) == NULL) {
    return 1;
  }
  fgets(joke, 101, stdin);
  if (0 == strcmp(joke, "Knock knock?\n")) {
    data = fopen("data.txt", "a");
    fprintf(data, "-Knock knock?\n-Who's there?\n");
    printf("Who's there?\n");
    fgets(joke, 101, stdin);
    joke[strlen(joke) - 1] = '\0';
    fprintf(data, "-%s.\n-%s who?\n", joke, joke);
    printf("%s who?\n", joke);
    fgets(joke, 101, stdin);
    fprintf(data, "-%s#\n", joke);
    fclose(data);
  } else {
    data = fopen("data.txt", "a");
    fprintf(data, "%s#\n", joke);
    fclose(data);
  }
  a++;
  free(joke);
  counter = fopen("counter.txt", "w");
  fprintf(counter, "%d", a);
  fclose(counter);
  return 0;
}


x86 assembly (clang, -O0):
<tell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe51(%rip),%rdi
lea    0xe56(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe44(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1070 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x65,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1200 <tell+0x60>
movl   $0x1,-0x4(%rbp)
jmp    138f <tell+0x1ef>
mov    -0x20(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
mov    $0x65,%esi
call   1080 <fgets@plt>
mov    -0x20(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1090 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    130b <tell+0x16b>
lea    0xde3(%rip),%rdi
lea    0xde5(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdd3(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
mov    $0x65,%esi
call   1080 <fgets@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xd90(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x20(%rbp),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
mov    $0x65,%esi
call   1080 <fgets@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd60(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1341 <tell+0x1a1>
lea    0xd0d(%rip),%rdi
lea    0xd0f(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd26(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xca6(%rip),%rdi
lea    0xcfb(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xc96(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tell>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe32(%rip),%rdi
lea    0xe37(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbx
lea    0xe2a(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1080 <fscanf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x65,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     125b <tell+0x9b>
mov    %rax,%rbx
mov    0x2dd6(%rip),%r15
mov    (%r15),%edx
mov    %rax,%rdi
mov    $0x65,%esi
call   1090 <fgets@plt>
lea    0xde8(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strcmp@plt>
mov    %rax,%rbp
lea    0xde4(%rip),%rdi
lea    0xde6(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%r14
test   %rbp,%rbp
je     1265 <tell+0xa5>
lea    0xe01(%rip),%rsi
jmp    12e8 <tell+0x128>
mov    $0x1,%ebp
jmp    133f <tell+0x17f>
lea    0xdbe(%rip),%rdi
mov    $0x1c,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10e0 <fwrite@plt>
lea    0xdd9(%rip),%rdi
call   1040 <puts@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x65,%esi
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xd97(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
lea    0xd86(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x65,%esi
call   1090 <fgets@plt>
lea    0xd6e(%rip),%rsi
xor    %ebp,%ebp
mov    %r14,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
addl   $0x1,0x4(%rsp)
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xced(%rip),%rdi
lea    0xd42(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%rbx
mov    0x4(%rsp),%edx
lea    0xce1(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"counter.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"Knock knock?\0A\00"
@.str.4 = [9 x i8] c"data.txt\00"
@.str.5 = [2 x i8] c"a\00"
@.str.6 = [29 x i8] c"-Knock knock?\0A-Who's there?\0A\00"
@.str.7 = [14 x i8] c"Who's there?\0A\00"
@.str.8 = [15 x i8] c"-%s.\0A-%s who?\0A\00"
@.str.9 = [9 x i8] c"%s who?\0A\00"
@.str.10 = [6 x i8] c"-%s#\0A\00"
@.str.11 = [5 x i8] c"%s#\0A\00"
@.str.12 = [2 x i8] c"w\00"
define i32 @tell() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 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** %3
%7 = load i32*, i32** %3
%8 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%9 = load i32*, i32** %3
%10 = call i32 @fclose(i32* %9)
%11 = call i8* @malloc(i32 101)
store i8* %11, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 1, i32* %1
br label %67
14: ; preds = %0
%15 = load i8*, i8** %4
%16 = load i32, i32* @stdin
%17 = call i32 @fgets(i8* %15, i32 101, i32 %16)
%18 = load i8*, i8** %4
%19 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i64 0, %19
br i1 %20, label %21, label %49
21: ; preds = %14
%22 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %22, i32** %2
%23 = load i32*, i32** %2
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%26 = load i8*, i8** %4
%27 = load i32, i32* @stdin
%28 = call i32 @fgets(i8* %26, i32 101, i32 %27)
%29 = load i8*, i8** %4
%30 = load i8*, i8** %4
%31 = call i32 @strlen(i8* %30)
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %29, i64 %33
store i8 0, i8* %34
%35 = load i32*, i32** %2
%36 = load i8*, i8** %4
%37 = load i8*, i8** %4
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %36, i8* %37)
%39 = load i8*, i8** %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i8* %39)
%41 = load i8*, i8** %4
%42 = load i32, i32* @stdin
%43 = call i32 @fgets(i8* %41, i32 101, i32 %42)
%44 = load i32*, i32** %2
%45 = load i8*, i8** %4
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), i8* %45)
%47 = load i32*, i32** %2
%48 = call i32 @fclose(i32* %47)
br label %56
49: ; preds = %14
%50 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %50, i32** %2
%51 = load i32*, i32** %2
%52 = load i8*, i8** %4
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %51, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0), i8* %52)
%54 = load i32*, i32** %2
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %49, %21
%57 = load i32, i32* %5
%58 = add i32 %57, 1
store i32 %58, i32* %5
%59 = load i8*, i8** %4
%60 = call i32 @free(i8* %59)
%61 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
store i32* %61, i32** %3
%62 = load i32*, i32** %3
%63 = load i32, i32* %5
%64 = call i32 (i32*, i8*, ...) @fprintf(i32* %62, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %63)
%65 = load i32*, i32** %3
%66 = call i32 @fclose(i32* %65)
store i32 0, i32* %1
br label %67
67: ; preds = %56, %13
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@.str = [12 x i8] c"counter.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [3 x i8] c"%d\00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"Knock knock?\0A\00"
@.str.4 = [9 x i8] c"data.txt\00"
@.str.5 = [2 x i8] c"a\00"
@.str.6 = [29 x i8] c"-Knock knock?\0A-Who's there?\0A\00"
@.str.8 = [15 x i8] c"-%s.\0A-%s who?\0A\00"
@.str.9 = [9 x i8] c"%s who?\0A\00"
@.str.10 = [6 x i8] c"-%s#\0A\00"
@.str.11 = [5 x i8] c"%s#\0A\00"
@.str.12 = [2 x i8] c"w\00"
@str = [13 x i8] c"Who's there?\00"
define i32 @tell() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%5 = call i32 @fclose(i32* %3)
%6 = call dereferenceable_or_null(101) i8* @malloc(i32 101)
%7 = icmp eq i8* %6, null
br i1 %7, label %38, label %8
8: ; preds = %0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %6, i32 101, i32 %9)
%11 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
%13 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br i1 %12, label %14, label %27
14: ; preds = %8
%15 = call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i64 28, i64 1, i32* %13)
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @stdin
%18 = call i32 @fgets(i8* %6, i32 101, i32 %17)
%19 = call i32 @strlen(i8* %6)
%20 = add nsw i32 %19, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %6, i64 %21
store i8 0, i8* %22
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %6, i8* %6)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i8* %6)
%25 = load i32, i32* @stdin
%26 = call i32 @fgets(i8* %6, i32 101, i32 %25)
br label %27
27: ; preds = %8, %14
%28 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.10, i64 0, i64 0), %14 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.11, i64 0, i64 0), %8 ]
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* %28, i8* %6)
%30 = call i32 @fclose(i32* %13)
%31 = load i32, i32* %1
%32 = add i32 %31, 1
store i32 %32, i32* %1
%33 = call i32 @free(i8* %6)
%34 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%35 = load i32, i32* %1
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = call i32 @fclose(i32* %34)
br label %38
38: ; preds = %0, %27
%39 = phi i32 [ 0, %27 ], [ 1, %0 ]
ret i32 %39
}