#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*/ fwrite(char const *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strlen(char const *);
int main(int argc, char **argv) {
  char buf[80];
  const char *txt = "Hejsan\n";
  FILE *fp;
  fp = fopen("tst", "w");
  if (!fp) {
    printf("Could not open file for output\n");
    return 1;
  }
  fwrite(txt, sizeof(char), strlen(txt) + 1, fp);
  fclose(fp);
  fp = fopen("tst", "r");
  if (!fp) {
    printf("Could not open file for reading\n");
    return 1;
  }
  fgets(buf, 80, fp);
  fclose(fp);
  printf("Reading from file: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe80(%rip),%rax
mov    %rax,-0x68(%rbp)
lea    0xe7d(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    11c0 <main+0x60>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1263 <main+0x103>
mov    -0x68(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x68(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%rdi
mov    %rax,%rdx
add    $0x1,%rdx
mov    -0x70(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x70(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe0e(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    122f <main+0xcf>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1263 <main+0x103>
lea    -0x60(%rbp),%rdi
mov    -0x70(%rbp),%rdx
mov    $0x50,%esi
call   1060 <fgets@plt>
mov    -0x70(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x60(%rbp),%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
lea    0xe99(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f4 <main+0x94>
mov    %rax,%rbx
lea    0xe76(%rip),%rdi
mov    $0x1,%ebp
mov    $0x8,%edx
mov    $0x1,%esi
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe58(%rip),%rdi
lea    0xe57(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1207 <main+0xa7>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x50,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe2a(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1213 <main+0xb3>
lea    0xe2c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    1213 <main+0xb3>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"Hejsan\0A\00"
@.str.1 = [4 x i8] c"tst\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [32 x i8] c"Could not open file for output\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [33 x i8] c"Could not open file for reading\0A\00"
@.str.6 = [23 x i8] c"Reading from file: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [80 x i8]
%7 = alloca i8*
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %7
%9 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 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* %9, i32** %8
%10 = load i32*, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %36
14: ; preds = %2
%15 = load i8*, i8** %7
%16 = load i8*, i8** %7
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = load i32*, i32** %8
%20 = call i32 @fwrite(i8* %15, i32 1, i64 %18, i32* %19)
%21 = load i32*, i32** %8
%22 = call i32 @fclose(i32* %21)
%23 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp ne i32* %24, null
br i1 %25, label %28, label %26
26: ; preds = %14
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %36
28: ; preds = %14
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%30 = load i32*, i32** %8
%31 = call i32 @fgets(i8* %29, i32 80, i32* %30)
%32 = load i32*, i32** %8
%33 = call i32 @fclose(i32* %32)
%34 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %34)
store i32 0, i32* %3
br label %36
36: ; preds = %28, %26, %12
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@.str = [8 x i8] c"Hejsan\0A\00"
@.str.1 = [4 x i8] c"tst\00"
@.str.2 = [2 x i8] c"w\00"
@.str.4 = [2 x i8] c"r\00"
@.str.6 = [23 x i8] c"Reading from file: %s\0A\00"
@str = [31 x i8] c"Could not open file for output\00"
@str.7 = [32 x i8] c"Could not open file for reading\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %20
9: ; preds = %2
%10 = tail call i32 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 1, i64 8, i32* %5)
%11 = tail call i32 @fclose(i32* %5)
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.7, i64 0, i64 0))
br label %20
16: ; preds = %9
%17 = call i32 @fgets(i8* %4, i32 80, i32* %12)
%18 = call i32 @fclose(i32* %12)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i8* %4)
br label %20
20: ; preds = %16, %14, %7
%21 = phi i32 [ 0, %16 ], [ 1, %14 ], [ 1, %7 ]
ret i32 %21
}