#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 */
struct ccd_frame {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ err_printf(char *, ...);
scalar_t__ fits_filename(char *, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *popen(char *, char *);
struct ccd_frame *read_fits_file_from_fd(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
struct ccd_frame *read_gz_fits_file(char *filename, char *ungz,
                                    int force_unsigned) {
  char fn[1024 + 1];
  FILE *fp;
  int zipped;
  char *cmd;
  strncpy(fn, filename, 1024);
  zipped = (fits_filename(fn, 1024) < 0);
  if (zipped && ungz == NULL) {
    err_printf("cannot open non-fits file\n");
    return NULL;
  }
  if (zipped) {
    asprintf(&cmd, "%s %s ", ungz, fn);
    fp = popen(cmd, "r");
    free(cmd);
    if (fp == NULL) {
      err_printf("Cannot open/unzip file: %s with %s\n", fn, ungz);
      return NULL;
    }
  } else {
    fp = fopen(fn, "r");
    if (fp == NULL) {
      err_printf("Cannot open file: %s\n", fn);
      return NULL;
    }
  }
  return read_fits_file_from_fd(fp, fn, force_unsigned);
}


x86 assembly (clang, -O0):
<read_gz_fits_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1040 <strncpy@plt>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   1050 <fits_filename@plt>
cmp    $0x0,%rax
setl   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x42c(%rbp)
cmpl   $0x0,-0x42c(%rbp)
je     1201 <read_gz_fits_file+0x81>
cmpq   $0x0,-0x18(%rbp)
jne    1201 <read_gz_fits_file+0x81>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   10a0 <err_printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f4 <read_gz_fits_file+0x174>
cmpl   $0x0,-0x42c(%rbp)
je     128b <read_gz_fits_file+0x10b>
mov    -0x18(%rbp),%rdx
lea    -0x420(%rbp),%rcx
lea    -0x438(%rbp),%rdi
lea    0xdf4(%rip),%rsi
call   1070 <asprintf@plt>
mov    -0x438(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x428(%rbp)
mov    -0x438(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x428(%rbp)
jne    1286 <read_gz_fits_file+0x106>
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   10a0 <err_printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f4 <read_gz_fits_file+0x174>
jmp    12da <read_gz_fits_file+0x15a>
lea    -0x420(%rbp),%rdi
lea    0xd89(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    12d5 <read_gz_fits_file+0x155>
lea    -0x420(%rbp),%rsi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   10a0 <err_printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f4 <read_gz_fits_file+0x174>
jmp    12da <read_gz_fits_file+0x15a>
mov    -0x428(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read_fits_file_from_fd@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_gz_fits_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%edx
call   1040 <strncpy@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
call   1050 <fits_filename@plt>
test   %rbx,%rbx
jne    11d2 <read_gz_fits_file+0x52>
test   %rax,%rax
jns    11d2 <read_gz_fits_file+0x52>
lea    0xe3c(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   10a0 <err_printf@plt>
jmp    1256 <read_gz_fits_file+0xd6>
test   %rax,%rax
js     1207 <read_gz_fits_file+0x87>
lea    0xe44(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1243 <read_gz_fits_file+0xc3>
lea    0xe51(%rip),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   10a0 <err_printf@plt>
jmp    1256 <read_gz_fits_file+0xd6>
lea    0xe0d(%rip),%rsi
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rcx
mov    %rbx,%rdx
call   1070 <asprintf@plt>
mov    0x8(%rsp),%rdi
lea    0xdf6(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %rbp,%rbp
je     1265 <read_gz_fits_file+0xe5>
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
call   1060 <read_fits_file_from_fd@plt>
mov    %rax,%rbp
mov    %rbp,%rax
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb8(%rip),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <err_printf@plt>
jmp    1256 <read_gz_fits_file+0xd6>

LLVM IR (-O0):
%struct.ccd_frame = type { i32 }
@.str = [27 x i8] c"cannot open non-fits file\0A\00"
@.str.1 = [7 x i8] c"%s %s \00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [36 x i8] c"Cannot open/unzip file: %s with %s\0A\00"
@.str.4 = [22 x i8] c"Cannot open file: %s\0A\00"
define %struct.ccd_frame* @read_gz_fits_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ccd_frame*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [1025 x i8]
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = call i32 @strncpy(i8* %12, i8* %13, i32 1024)
%15 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%16 = call i64 @fits_filename(i8* %15, i32 1024)
%17 = icmp slt i64 %16, 0
%18 = zext i1 %17 to i32
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %3
%22 = load i8*, i8** %6
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store %struct.ccd_frame* null, %struct.ccd_frame** %4
br label %58
26: ; preds = %21, %3
%27 = load i32, i32* %10
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %44
29: ; preds = %26
%30 = load i8*, i8** %6
%31 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%32 = call i32 @asprintf(i8** %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* %31)
%33 = load i8*, i8** %11
%34 = call i32* @popen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %9
%35 = load i8*, i8** %11
%36 = call i32 @free(i8* %35)
%37 = load i32*, i32** %9
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %43
39: ; preds = %29
%40 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%41 = load i8*, i8** %6
%42 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %40, i8* %41)
store %struct.ccd_frame* null, %struct.ccd_frame** %4
br label %58
43: ; preds = %29
br label %53
44: ; preds = %26
%45 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %46, i32** %9
%47 = load i32*, i32** %9
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%51 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %50)
store %struct.ccd_frame* null, %struct.ccd_frame** %4
br label %58
52: ; preds = %44
br label %53
53: ; preds = %52, %43
%54 = load i32*, i32** %9
%55 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%56 = load i32, i32* %7
%57 = call %struct.ccd_frame* @read_fits_file_from_fd(i32* %54, i8* %55, i32 %56)
store %struct.ccd_frame* %57, %struct.ccd_frame** %4
br label %58
58: ; preds = %53, %49, %39, %24
%59 = load %struct.ccd_frame*, %struct.ccd_frame** %4
ret %struct.ccd_frame* %59
}

LLVM IR (-O2):
%struct.ccd_frame = type { i32 }
@.str = [27 x i8] c"cannot open non-fits file\0A\00"
@.str.1 = [7 x i8] c"%s %s \00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [36 x i8] c"Cannot open/unzip file: %s with %s\0A\00"
@.str.4 = [22 x i8] c"Cannot open file: %s\0A\00"
define %struct.ccd_frame* @read_gz_fits_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca [1025 x i8]
%5 = alloca i8*
%6 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%7 = bitcast i8** %5 to i8*
%8 = call i32 @strncpy(i8* %6, i8* %0, i32 1024)
%9 = call i64 @fits_filename(i8* %6, i32 1024)
%10 = icmp slt i64 %9, 0
%11 = icmp eq i8* %1, null
%12 = and i1 %11, %10
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %33
15: ; preds = %3
br i1 %10, label %16, label %25
16: ; preds = %15
%17 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %6)
%18 = load i8*, i8** %5
%19 = call i32* @popen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = load i8*, i8** %5
%21 = call i32 @free(i8* %20)
%22 = icmp eq i32* %19, null
br i1 %22, label %23, label %30
23: ; preds = %16
%24 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %1)
br label %33
25: ; preds = %15
%26 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @err_printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %33
30: ; preds = %25, %16
%31 = phi i32* [ %19, %16 ], [ %26, %25 ]
%32 = call %struct.ccd_frame* @read_fits_file_from_fd(i32* %31, i8* %6, i32 %2)
br label %33
33: ; preds = %30, %28, %23, %13
%34 = phi %struct.ccd_frame* [ null, %13 ], [ null, %23 ], [ %32, %30 ], [ null, %28 ]
ret %struct.ccd_frame* %34
}