#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 const *, char *);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
bool isReadWriteable(const char *filename) {
  FILE *fp = fopen(filename, "r+");
  if (fp != NULL) {
    long pos = ftell(fp);
    if (pos < 0L)
      perror("ftell()");
    fclose(fp);
    return true;
  } else {
    return false;
  }
}


x86 assembly (clang, -O0):
<isReadWriteable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a4 <isReadWriteable+0x64>
mov    -0x18(%rbp),%rdi
call   1040 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    118f <isReadWriteable+0x4f>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ab <isReadWriteable+0x6b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isReadWriteable>:
push   %rbx
lea    0xeb8(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1171 <isReadWriteable+0x31>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <ftell@plt>
test   %rax,%rax
js     1175 <isReadWriteable+0x35>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
ret
xor    %eax,%eax
pop    %rbx
ret
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
jmp    1162 <isReadWriteable+0x22>

LLVM IR (-O0):
@.str = [3 x i8] c"r+\00"
@.str.1 = [8 x i8] c"ftell()\00"
define i32 @isReadWriteable(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %20
10: ; preds = %1
%11 = load i32*, i32** %4
%12 = call i64 @ftell(i32* %11)
store i64 %12, i64* %5
%13 = load i64, i64* %5
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = load i32*, i32** %4
%19 = call i32 @fclose(i32* %18)
store i32 1, i32* %2
br label %21
20: ; preds = %1
store i32 0, i32* %2
br label %21
21: ; preds = %20, %17
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
@.str = [3 x i8] c"r+\00"
@.str.1 = [8 x i8] c"ftell()\00"
define i32 @isReadWriteable(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = tail call i64 @ftell(i32* %2)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %4
%10 = tail call i32 @fclose(i32* %2)
br label %11
11: ; preds = %1, %9
%12 = phi i32 [ 1, %9 ], [ 0, %1 ]
ret i32 %12
}