#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*/ clearerr(int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int main() {
  FILE *fp;
  char c;
  fp = fopen("file.txt", "w");
  c = fgetc(fp);
  if (ferror(fp))
    printf("Error in reading from file : file.txt\n");
  clearerr(fp);
  if (ferror(fp))
    printf("Error in reading from file : file.txt\n");
  fclose(fp);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11b1 <main+0x51>
lea    0xe5f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1070 <clearerr@plt>
mov    -0x10(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     11d9 <main+0x79>
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xeb7(%rip),%rdi
lea    0xe91(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     1198 <main+0x38>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <clearerr@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11b9 <main+0x59>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [39 x i8] c"Error in reading from file : file.txt\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8
store i32 0, i32* %1
%4 = 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.1, i64 0, i64 0))
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = call signext i8 @fgetc(i32* %5)
store i8 %6, i8* %3
%7 = load i32*, i32** %2
%8 = call i64 @ferror(i32* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32*, i32** %2
%14 = call i32 @clearerr(i32* %13)
%15 = load i32*, i32** %2
%16 = call i64 @ferror(i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@str.3 = [38 x i8] c"Error in reading from file : file.txt\00"
define i32 @main() {
%1 = 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.1, i64 0, i64 0))
%2 = tail call signext i8 @fgetc(i32* %1)
%3 = tail call i64 @ferror(i32* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.3, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call i32 @clearerr(i32* %1)
%9 = tail call i64 @ferror(i32* %1)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.3, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = tail call i32 @fclose(i32* %1)
ret i32 0
}