#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi_next(char **);
int /*<<< orphan*/ chomp(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *iscmd(char *, char *);
int check_bdfsize(char *filename) {
  FILE *fp;
  fp = fopen(filename, "rb");
  if (!fp)
    return 0;
  else {
    int pixel_size = 0;
    int size = 0;
    char buf[8192], *cmd;
    while (fgets(buf, sizeof(buf), fp)) {
      chomp(buf);
      if (cmd = iscmd(buf, "SIZE"))
        size = atoi_next(&cmd);
      else if (cmd = iscmd(buf, "PIXEL_SIZE")) {
        pixel_size = atoi_next(&cmd);
        break;
      }
    }
    fclose(fp);
    return pixel_size > 0 ? pixel_size : size;
  }
}


x86 assembly (clang, -O0):
<check_bdfsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe86(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119a <check_bdfsize+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    1280 <check_bdfsize+0x120>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x2020(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x2000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     124d <check_bdfsize+0xed>
lea    -0x2020(%rbp),%rdi
call   1070 <chomp@plt>
lea    -0x2020(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1040 <iscmd@plt>
mov    %rax,-0x2028(%rbp)
cmp    $0x0,%rax
je     120b <check_bdfsize+0xab>
lea    -0x2028(%rbp),%rdi
call   1060 <atoi_next@plt>
mov    %eax,-0x20(%rbp)
jmp    1248 <check_bdfsize+0xe8>
lea    -0x2020(%rbp),%rdi
lea    0xdea(%rip),%rsi
call   1040 <iscmd@plt>
mov    %rax,-0x2028(%rbp)
cmp    $0x0,%rax
je     1243 <check_bdfsize+0xe3>
lea    -0x2028(%rbp),%rdi
call   1060 <atoi_next@plt>
mov    %eax,-0x1c(%rbp)
jmp    124d <check_bdfsize+0xed>
jmp    1248 <check_bdfsize+0xe8>
jmp    11a8 <check_bdfsize+0x48>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    126e <check_bdfsize+0x10e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x202c(%rbp)
jmp    1277 <check_bdfsize+0x117>
mov    -0x20(%rbp),%eax
mov    %eax,-0x202c(%rbp)
mov    -0x202c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_bdfsize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2018,%rsp
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1248 <check_bdfsize+0xe8>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x2000,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     123e <check_bdfsize+0xde>
lea    0x10(%rsp),%rbp
lea    0xe56(%rip),%r15
lea    0xe49(%rip),%r12
lea    0x8(%rsp),%r13
jmp    11f0 <check_bdfsize+0x90>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1060 <atoi_next@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
mov    $0x2000,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     123e <check_bdfsize+0xde>
mov    %rbp,%rdi
call   1070 <chomp@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1040 <iscmd@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
jne    11d0 <check_bdfsize+0x70>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1040 <iscmd@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     11db <check_bdfsize+0x7b>
lea    0x8(%rsp),%rdi
call   1060 <atoi_next@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %ebp,%ebp
cmovg  %ebp,%r14d
jmp    124b <check_bdfsize+0xeb>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    124b <check_bdfsize+0xeb>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x2018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c"SIZE\00"
@.str.2 = [11 x i8] c"PIXEL_SIZE\00"
define i32 @check_bdfsize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [8192 x i8]
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %1
store i32 0, i32* %2
br label %47
14: ; preds = %1
store i32 0, i32* %5
store i32 0, i32* %6
br label %15
15: ; preds = %35, %14
%16 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i64 @fgets(i8* %16, i32 8192, i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %36
20: ; preds = %15
%21 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%22 = call i32 @chomp(i8* %21)
%23 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%24 = call i8* @iscmd(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %24, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @atoi_next(i8** %8)
store i32 %27, i32* %6
br label %35
28: ; preds = %20
%29 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%30 = call i8* @iscmd(i8* %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i8* %30, i8** %8
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @atoi_next(i8** %8)
store i32 %33, i32* %5
br label %36
34: ; preds = %28
br label %35
35: ; preds = %34, %26
br label %15
36: ; preds = %32, %15
%37 = load i32*, i32** %4
%38 = call i32 @fclose(i32* %37)
%39 = load i32, i32* %5
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = load i32, i32* %5
br label %45
43: ; preds = %36
%44 = load i32, i32* %6
br label %45
45: ; preds = %43, %41
%46 = phi i32 [ %42, %41 ], [ %44, %43 ]
store i32 %46, i32* %2
br label %47
47: ; preds = %45, %13
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [5 x i8] c"SIZE\00"
@.str.2 = [11 x i8] c"PIXEL_SIZE\00"
define i32 @check_bdfsize(i8* nocapture %0) {
%2 = alloca [8192 x i8]
%3 = alloca i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %35, label %6
6: ; preds = %1
%7 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%8 = bitcast i8** %3 to i8*
%9 = call i64 @fgets(i8* %7, i32 8192, i32* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %24, %6
%12 = phi i32 [ 0, %6 ], [ %25, %24 ]
%13 = call i32 @fclose(i32* %4)
br label %33
14: ; preds = %6, %24
%15 = phi i32 [ %25, %24 ], [ 0, %6 ]
%16 = call i32 @chomp(i8* %7)
%17 = call i8* @iscmd(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %3
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = call i32 @atoi_next(i8** %3)
br label %24
21: ; preds = %14
%22 = call i8* @iscmd(i8* %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i8* %22, i8** %3
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %21, %19
%25 = phi i32 [ %20, %19 ], [ %15, %21 ]
%26 = call i64 @fgets(i8* %7, i32 8192, i32* %4)
%27 = icmp eq i64 %26, 0
br i1 %27, label %11, label %14
28: ; preds = %21
%29 = call i32 @atoi_next(i8** %3)
%30 = call i32 @fclose(i32* %4)
%31 = icmp sgt i32 %29, 0
%32 = select i1 %31, i32 %29, i32 %15
br label %33
33: ; preds = %28, %11
%34 = phi i32 [ %12, %11 ], [ %32, %28 ]
br label %35
35: ; preds = %1, %33
%36 = phi i32 [ %34, %33 ], [ 0, %1 ]
ret i32 %36
}