#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 /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
long strlen(char *);
int main() {
  char *buffer = malloc(50);
  char buff1[50] = "How now brown cow.";
  FILE *fp;
  if (fp = fopen("test.txt", "a+")) {
    fprintf(fp, "Testing...\n");
    printf("Wrote \"Testing...to test.txt\n");
  } else {
    printf("File doesn't exist or file problem\n");
  }
  fclose(fp);
  if (fp = fopen("test.txt", "r")) {
    fscanf(fp, "%s", buffer);
    printf("We read %s from test.txt\n", buffer);
  } else {
    printf("File doesn't exist or file problem\n");
  }
  fclose(fp);
  buffer = buff1;
  if (fp = fopen("test.txt", "a+")) {
    for (long x = strlen(buffer); x != 0; x--) {
      fputc(*buffer, fp);
      printf("Wrote %c to test.txt\n", *buffer);
      buffer++;
    }
    fputc('\n', fp);
  } else {
    printf("File doesn't exist or file problem\n");
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x32,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe48(%rip),%rsi
mov    $0x32,%edx
call   1090 <memcpy@plt>
lea    0xe69(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
je     1206 <main+0x76>
mov    -0x58(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xe50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1214 <main+0x84>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe0e(%rip),%rdi
lea    0xe61(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
je     1269 <main+0xd9>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe41(%rip),%rsi
call   1060 <fscanf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1277 <main+0xe7>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x10(%rbp)
lea    0xda3(%rip),%rdi
lea    0xda5(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
je     1318 <main+0x188>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     1305 <main+0x175>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    -0x58(%rbp),%rsi
movsbl %al,%edi
call   1070 <fputc@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x60(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x60(%rbp)
jmp    12b6 <main+0x126>
mov    -0x58(%rbp),%rsi
mov    $0xa,%edi
call   1070 <fputc@plt>
jmp    1326 <main+0x196>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    $0x32,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
movaps 0xe71(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe55(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe39(%rip),%xmm0
movaps %xmm0,(%rsp)
movw   $0x0,0x30(%rsp)
lea    0xed4(%rip),%rdi
lea    0xe52(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120f <main+0x7f>
lea    0xe41(%rip),%rdi
mov    $0xb,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
lea    0xe8c(%rip),%rdi
jmp    1216 <main+0x86>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe83(%rip),%rdi
lea    0xe10(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1263 <main+0xd3>
lea    0xdfe(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1070 <fscanf@plt>
lea    0xdef(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    126f <main+0xdf>
lea    0xe0c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe2f(%rip),%rdi
lea    0xdad(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1312 <main+0x182>
mov    %rsp,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     1303 <main+0x173>
mov    %rax,%r12
mov    $0x48,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
movsbl (%rsp),%esi
lea    0xda2(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x1,%r12
je     1303 <main+0x173>
mov    $0x1,%ebx
lea    0xd89(%rip),%r15
nopw   0x0(%rax,%rax,1)
movsbl (%rsp,%rbx,1),%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
movsbl (%rsp,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    12e0 <main+0x150>
mov    $0xa,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
jmp    131e <main+0x18e>
lea    0xd5d(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.buff1 = [50 x i8] c"How now brown cow.\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [12 x i8] c"Testing...\0A\00"
@.str.3 = [30 x i8] c"Wrote \22Testing...to test.txt\0A\00"
@.str.4 = [36 x i8] c"File doesn't exist or file problem\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [26 x i8] c"We read %s from test.txt\0A\00"
@.str.8 = [22 x i8] c"Wrote %c to test.txt\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [50 x i8]
%4 = alloca i32*
%5 = alloca i64
store i32 0, i32* %1
%6 = call i8* @malloc(i32 50)
store i8* %6, i8** %2
%7 = bitcast [50 x i8]* %3 to i8*
%8 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32*, i32** %4
%12 = call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %16
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i32*, i32** %4
%18 = call i32 @fclose(i32* %17)
%19 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %19, i32** %4
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i32*, i32** %4
%23 = load i8*, i8** %2
%24 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %25)
br label %29
27: ; preds = %16
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
%32 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
store i8* %32, i8** %2
%33 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %33, i32** %4
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %58
35: ; preds = %29
%36 = load i8*, i8** %2
%37 = call i64 @strlen(i8* %36)
store i64 %37, i64* %5
br label %38
38: ; preds = %52, %35
%39 = load i64, i64* %5
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %55
41: ; preds = %38
%42 = load i8*, i8** %2
%43 = load i8, i8* %42
%44 = load i32*, i32** %4
%45 = call i32 @fputc(i8 signext %43, i32* %44)
%46 = load i8*, i8** %2
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %48)
%50 = load i8*, i8** %2
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %2
br label %52
52: ; preds = %41
%53 = load i64, i64* %5
%54 = add nsw i64 %53, -1
store i64 %54, i64* %5
br label %38
55: ; preds = %38
%56 = load i32*, i32** %4
%57 = call i32 @fputc(i8 signext 10, i32* %56)
br label %60
58: ; preds = %29
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = load i32*, i32** %4
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
@__const.main.buff1 = [50 x i8] c"How now brown cow.\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [12 x i8] c"Testing...\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [26 x i8] c"We read %s from test.txt\0A\00"
@.str.8 = [22 x i8] c"Wrote %c to test.txt\0A\00"
@str.10 = [35 x i8] c"File doesn't exist or file problem\00"
@str.11 = [29 x i8] c"Wrote \22Testing...to test.txt\00"
define i32 @main() {
%1 = alloca [50 x i8]
%2 = tail call dereferenceable_or_null(50) i8* @malloc(i32 50)
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 11, i64 1, i32* %4)
br label %8
8: ; preds = %0, %6
%9 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @str.11, i64 0, i64 0), %6 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str.10, i64 0, i64 0), %0 ]
%10 = tail call i32 @puts(i8* dereferenceable(1) %9)
%11 = tail call i32 @fclose(i32* %4)
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = tail call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %2)
br label %19
17: ; preds = %8
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.10, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = tail call i32 @fclose(i32* %12)
%21 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %48, label %23
23: ; preds = %19
%24 = call i64 @strlen(i8* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %23
%27 = tail call i32 @fputc(i8 signext 72, i32* %21)
%28 = load i8, i8* %3
%29 = sext i8 %28 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %29)
%31 = add nsw i64 %24, -1
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %26
%34 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 1
br label %37
35: ; preds = %37, %26, %23
%36 = tail call i32 @fputc(i8 signext 10, i32* %21)
br label %50
37: ; preds = %33, %37
%38 = phi i64 [ %31, %33 ], [ %46, %37 ]
%39 = phi i8* [ %34, %33 ], [ %45, %37 ]
%40 = load i8, i8* %39
%41 = tail call i32 @fputc(i8 signext %40, i32* %21)
%42 = load i8, i8* %39
%43 = sext i8 %42 to i32
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.8, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds i8, i8* %39, i64 1
%46 = add nsw i64 %38, -1
%47 = icmp eq i64 %46, 0
br i1 %47, label %35, label %37
48: ; preds = %19
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.10, i64 0, i64 0))
br label %50
50: ; preds = %48, %35
%51 = tail call i32 @fclose(i32* %21)
ret i32 0
}