#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 */
char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ qfits_error(char *, char const *);
int /*<<< orphan*/ *strstr(char *, char *);
int qfits_is_fits(const char *filename) {
  FILE *fp;
  char *magic;
  int isfits;
  if (filename == NULL)
    return -1;
  if ((fp = fopen(filename, "r")) == NULL) {
    qfits_error("cannot open file [%s]", filename);
    return -1;
  }
  magic = calloc(6 + 1, sizeof(char));
  fread(magic, 1, 6, fp);
  fclose(fp);
  magic[6] = (char)0;
  if (strstr(magic, "SIMPLE") != NULL)
    isfits = 1;
  else
    isfits = 0;
  free(magic);
  return isfits;
}


x86 assembly (clang, -O0):
<qfits_is_fits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1193 <qfits_is_fits+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <qfits_is_fits+0xd4>
mov    -0x10(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11cd <qfits_is_fits+0x5d>
mov    -0x10(%rbp),%rsi
lea    0xe46(%rip),%rdi
call   1040 <qfits_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1244 <qfits_is_fits+0xd4>
mov    $0x7,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x6,%edx
call   1050 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x20(%rbp),%rax
movb   $0x0,0x6(%rax)
mov    -0x20(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     122e <qfits_is_fits+0xbe>
movl   $0x1,-0x24(%rbp)
jmp    1235 <qfits_is_fits+0xc5>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qfits_is_fits>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     11f9 <qfits_is_fits+0x89>
mov    %rdi,%rbx
lea    0xe78(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11ea <qfits_is_fits+0x7a>
mov    %rax,%r14
mov    $0x7,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x6,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
movb   $0x0,0x6(%rbx)
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strstr@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11f9 <qfits_is_fits+0x89>
lea    0xe11(%rip),%rdi
mov    %rbx,%rsi
call   1040 <qfits_error@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"cannot open file [%s]\00"
@.str.2 = [7 x i8] c"SIMPLE\00"
define i32 @qfits_is_fits(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %35
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = call i32 @qfits_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %2
br label %35
17: ; preds = %10
%18 = call i8* @calloc(i32 7, i32 1)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = load i32*, i32** %4
%21 = call i32 @fread(i8* %19, i32 1, i32 6, i32* %20)
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 6
store i8 0, i8* %25
%26 = load i8*, i8** %5
%27 = call i32* @strstr(i8* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp ne i32* %27, null
br i1 %28, label %29, label %30
29: ; preds = %17
store i32 1, i32* %6
br label %31
30: ; preds = %17
store i32 0, i32* %6
br label %31
31: ; preds = %30, %29
%32 = load i8*, i8** %5
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* %6
store i32 %34, i32* %2
br label %35
35: ; preds = %31, %14, %9
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [22 x i8] c"cannot open file [%s]\00"
@.str.2 = [7 x i8] c"SIMPLE\00"
define i32 @qfits_is_fits(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %17, label %3
3: ; preds = %1
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @qfits_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
8: ; preds = %3
%9 = tail call dereferenceable_or_null(7) i8* @calloc(i32 7, i32 1)
%10 = tail call i32 @fread(i8* %9, i32 1, i32 6, i32* %4)
%11 = tail call i32 @fclose(i32* %4)
%12 = getelementptr inbounds i8, i8* %9, i64 6
store i8 0, i8* %12
%13 = tail call i32* @strstr(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp ne i32* %13, null
%15 = zext i1 %14 to i32
%16 = tail call i32 @free(i8* %9)
br label %17
17: ; preds = %1, %8, %6
%18 = phi i32 [ -1, %6 ], [ %15, %8 ], [ -1, %1 ]
ret i32 %18
}