#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 sscanf(char *, char *, int *, int *, int *);
int rdhdr_c(char *filename, char *binary, int ispec, int filenum, int *npix,
            int *nrast, int *nframe) {
  char buff[120 + 1], *cptr;
  register int i = 0;
  FILE *fp, *fopen();
  if (ispec > 0) {
    cptr = filename;
    *++cptr = (ispec / 10000) + '0';
    *++cptr = ((ispec % 10000) / 1000) + '0';
  }
  if ((fp = fopen(filename, "r")) == NULL)
    return (0);
  else {
    if ((fgets(buff, 120, fp)) == NULL)
      return (0);
    else if ((fgets(buff, 120, fp)) == NULL)
      return (0);
    else {
      do {
        if ((fgets(buff, 120, fp)) == NULL)
          return (0);
        else if (sscanf(buff, "%8d%8d%8d", npix, nrast, nframe) != 3)
          return (0);
        else if ((fgets(binary, 120, fp)) == NULL)
          return (0);
      } while (++i < filenum);
    }
    *(binary + 10) = '\0';
    fclose(fp);
    return (!0);
  }
}


x86 assembly (clang, -O0):
<rdhdr_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0xbc(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    11de <rdhdr_c+0x9e>
mov    -0x10(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2710,%ecx
cltd
idiv   %ecx
add    $0x30,%eax
mov    %al,%cl
mov    -0xb8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0xb8(%rbp)
mov    %cl,0x1(%rax)
mov    -0x1c(%rbp),%eax
mov    $0x2710,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
add    $0x30,%eax
mov    %al,%cl
mov    -0xb8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0xb8(%rbp)
mov    %cl,0x1(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0xc8(%rbp)
cmp    $0x0,%rax
jne    120b <rdhdr_c+0xcb>
movl   $0x0,-0x4(%rbp)
jmp    1345 <rdhdr_c+0x205>
lea    -0xb0(%rbp),%rdi
mov    -0xc8(%rbp),%rdx
mov    $0x78,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1239 <rdhdr_c+0xf9>
movl   $0x0,-0x4(%rbp)
jmp    1345 <rdhdr_c+0x205>
lea    -0xb0(%rbp),%rdi
mov    -0xc8(%rbp),%rdx
mov    $0x78,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    1267 <rdhdr_c+0x127>
movl   $0x0,-0x4(%rbp)
jmp    1345 <rdhdr_c+0x205>
jmp    126c <rdhdr_c+0x12c>
lea    -0xb0(%rbp),%rdi
mov    -0xc8(%rbp),%rdx
mov    $0x78,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    129a <rdhdr_c+0x15a>
movl   $0x0,-0x4(%rbp)
jmp    1345 <rdhdr_c+0x205>
lea    -0xb0(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x10(%rbp),%r8
lea    0xd4e(%rip),%rsi
call   1050 <sscanf@plt>
cmp    $0x3,%eax
je     12ce <rdhdr_c+0x18e>
movl   $0x0,-0x4(%rbp)
jmp    1345 <rdhdr_c+0x205>
mov    -0x18(%rbp),%rdi
mov    -0xc8(%rbp),%rdx
mov    $0x78,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
jne    12f9 <rdhdr_c+0x1b9>
movl   $0x0,-0x4(%rbp)
jmp    1345 <rdhdr_c+0x205>
jmp    12fe <rdhdr_c+0x1be>
jmp    1303 <rdhdr_c+0x1c3>
jmp    1308 <rdhdr_c+0x1c8>
mov    -0xbc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xbc(%rbp)
cmp    -0x20(%rbp),%eax
jl     126c <rdhdr_c+0x12c>
jmp    1325 <rdhdr_c+0x1e5>
jmp    132a <rdhdr_c+0x1ea>
mov    -0x18(%rbp),%rax
movb   $0x0,0xa(%rax)
mov    -0xc8(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdhdr_c>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %r9,0x8(%rsp)
mov    %r8,%r15
mov    %ecx,%r13d
mov    %rsi,%r14
test   %edx,%edx
jle    1194 <rdhdr_c+0x54>
mov    %edx,%eax
mov    $0xd1b71759,%ecx
imul   %rax,%rcx
shr    $0x2d,%rcx
lea    0x30(%rcx),%eax
mov    %al,0x1(%rdi)
imul   $0x2710,%ecx,%eax
sub    %eax,%edx
movzwl %dx,%eax
shr    $0x3,%eax
imul   $0x20c5,%eax,%eax
shr    $0x14,%eax
add    $0x30,%al
mov    %al,0x2(%rdi)
lea    0xe65(%rip),%rsi
call   1060 <fopen@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1268 <rdhdr_c+0x128>
mov    %rax,%rbp
lea    0x10(%rsp),%rdi
mov    $0x78,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1268 <rdhdr_c+0x128>
lea    0x10(%rsp),%rdi
mov    $0x78,%esi
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1268 <rdhdr_c+0x128>
cmp    $0x2,%r13d
mov    $0x1,%r12d
cmovge %r13d,%r12d
lea    0x10(%rsp),%r13
mov    0x8(%rsp),%rbx
nopl   0x0(%rax)
mov    %r13,%rdi
mov    $0x78,%esi
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1266 <rdhdr_c+0x126>
mov    %r13,%rdi
lea    0xde3(%rip),%rsi
mov    %r15,%rdx
mov    %rbx,%rcx
mov    0xd0(%rsp),%r8
call   1050 <sscanf@plt>
cmp    $0x3,%eax
jne    1266 <rdhdr_c+0x126>
mov    %r14,%rdi
mov    $0x78,%esi
mov    %rbp,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1266 <rdhdr_c+0x126>
add    $0xffffffff,%r12d
jne    1200 <rdhdr_c+0xc0>
movb   $0x0,0xa(%r14)
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    $0x1,%ebx
jmp    1268 <rdhdr_c+0x128>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c"%8d%8d%8d\00"
define i32 @rdhdr_c(i8* %0, i8* %1, i32 %2, i32 %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca [121 x i8]
%17 = alloca i8*
%18 = alloca i32
%19 = alloca i32*
store i8* %0, i8** %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i32* %4, i32** %13
store i32* %5, i32** %14
store i32* %6, i32** %15
store i32 0, i32* %18
%20 = load i32, i32* %11
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %37
22: ; preds = %7
%23 = load i8*, i8** %9
store i8* %23, i8** %17
%24 = load i32, i32* %11
%25 = sdiv i32 %24, 10000
%26 = add nsw i32 %25, 48
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %17
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %17
store i8 %27, i8* %29
%30 = load i32, i32* %11
%31 = srem i32 %30, 10000
%32 = sdiv i32 %31, 1000
%33 = add nsw i32 %32, 48
%34 = trunc i32 %33 to i8
%35 = load i8*, i8** %17
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %17
store i8 %34, i8* %36
br label %37
37: ; preds = %22, %7
%38 = load i8*, i8** %9
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %39, i32** %19
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %42
41: ; preds = %37
store i32 0, i32* %8
br label %90
42: ; preds = %37
%43 = getelementptr inbounds [121 x i8], [121 x i8]* %16, i64 0, i64 0
%44 = load i32*, i32** %19
%45 = call i32* @fgets(i8* %43, i32 120, i32* %44)
%46 = icmp eq i32* %45, null
br i1 %46, label %47, label %48
47: ; preds = %42
store i32 0, i32* %8
br label %90
48: ; preds = %42
%49 = getelementptr inbounds [121 x i8], [121 x i8]* %16, i64 0, i64 0
%50 = load i32*, i32** %19
%51 = call i32* @fgets(i8* %49, i32 120, i32* %50)
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %54
53: ; preds = %48
store i32 0, i32* %8
br label %90
54: ; preds = %48
br label %55
55: ; preds = %78, %54
%56 = getelementptr inbounds [121 x i8], [121 x i8]* %16, i64 0, i64 0
%57 = load i32*, i32** %19
%58 = call i32* @fgets(i8* %56, i32 120, i32* %57)
%59 = icmp eq i32* %58, null
br i1 %59, label %60, label %61
60: ; preds = %55
store i32 0, i32* %8
br label %90
61: ; preds = %55
%62 = getelementptr inbounds [121 x i8], [121 x i8]* %16, i64 0, i64 0
%63 = load i32*, i32** %13
%64 = load i32*, i32** %14
%65 = load i32*, i32** %15
%66 = call i32 @sscanf(i8* %62, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %63, i32* %64, i32* %65)
%67 = icmp ne i32 %66, 3
br i1 %67, label %68, label %69
68: ; preds = %61
store i32 0, i32* %8
br label %90
69: ; preds = %61
%70 = load i8*, i8** %10
%71 = load i32*, i32** %19
%72 = call i32* @fgets(i8* %70, i32 120, i32* %71)
%73 = icmp eq i32* %72, null
br i1 %73, label %74, label %75
74: ; preds = %69
store i32 0, i32* %8
br label %90
75: ; preds = %69
br label %76
76: ; preds = %75
br label %77
77: ; preds = %76
br label %78
78: ; preds = %77
%79 = load i32, i32* %18
%80 = add nsw i32 %79, 1
store i32 %80, i32* %18
%81 = load i32, i32* %12
%82 = icmp slt i32 %80, %81
br i1 %82, label %55, label %83
83: ; preds = %78
br label %84
84: ; preds = %83
br label %85
85: ; preds = %84
%86 = load i8*, i8** %10
%87 = getelementptr inbounds i8, i8* %86, i64 10
store i8 0, i8* %87
%88 = load i32*, i32** %19
%89 = call i32 @fclose(i32* %88)
store i32 1, i32* %8
br label %90
90: ; preds = %85, %74, %68, %60, %53, %47, %41
%91 = load i32, i32* %8
ret i32 %91
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [10 x i8] c"%8d%8d%8d\00"
define i32 @rdhdr_c(i8* nocapture %0, i8* %1, i32 %2, i32 %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca [121 x i8]
%9 = getelementptr inbounds [121 x i8], [121 x i8]* %8, i64 0, i64 0
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %22
11: ; preds = %7
%12 = udiv i32 %2, 10000
%13 = trunc i32 %12 to i8
%14 = add i8 %13, 48
%15 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %14, i8* %15
%16 = urem i32 %2, 10000
%17 = trunc i32 %16 to i16
%18 = udiv i16 %17, 1000
%19 = trunc i16 %18 to i8
%20 = add nuw nsw i8 %19, 48
%21 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %20, i8* %21
br label %22
22: ; preds = %11, %7
%23 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %49, label %25
25: ; preds = %22
%26 = call i32* @fgets(i8* %9, i32 120, i32* %23)
%27 = icmp eq i32* %26, null
br i1 %27, label %49, label %28
28: ; preds = %25
%29 = call i32* @fgets(i8* %9, i32 120, i32* %23)
%30 = icmp eq i32* %29, null
br i1 %30, label %49, label %31
31: ; preds = %28
%32 = call i32 @llvm.smax.i32(i32 %3, i32 1)
br label %33
33: ; preds = %31, %43
%34 = phi i32 [ %44, %43 ], [ 0, %31 ]
%35 = call i32* @fgets(i8* %9, i32 120, i32* %23)
%36 = icmp eq i32* %35, null
br i1 %36, label %49, label %37
37: ; preds = %33
%38 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %4, i32* %5, i32* %6)
%39 = icmp eq i32 %38, 3
br i1 %39, label %40, label %49
40: ; preds = %37
%41 = call i32* @fgets(i8* %1, i32 120, i32* %23)
%42 = icmp eq i32* %41, null
br i1 %42, label %49, label %43
43: ; preds = %40
%44 = add nuw nsw i32 %34, 1
%45 = icmp eq i32 %44, %32
br i1 %45, label %46, label %33
46: ; preds = %43
%47 = getelementptr inbounds i8, i8* %1, i64 10
store i8 0, i8* %47
%48 = call i32 @fclose(i32* %23)
br label %49
49: ; preds = %40, %37, %33, %28, %25, %22, %46
%50 = phi i32 [ 1, %46 ], [ 0, %22 ], [ 0, %25 ], [ 0, %28 ], [ 0, %33 ], [ 0, %37 ], [ 0, %40 ]
ret i32 %50
}