#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 *, char *);
scalar_t__ get_pgm_header(int /*<<< orphan*/ *, size_t *, size_t *, int *);
int /*<<< orphan*/ printf(char *);
int peek_pgm(size_t *pwidth, size_t *pheight, int *pmaxval, char *filename) {
  FILE *f;
  if (!(f = fopen(filename, "r"))) {
    printf("ERR:peek_pgm\n");
    return 1;
  }
  if (get_pgm_header(f, pwidth, pheight, pmaxval))
    return 2;
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<peek_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    118e <peek_pgm+0x4e>
lea    0xe85(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c9 <peek_pgm+0x89>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <get_pgm_header@plt>
cmp    $0x0,%rax
je     11b9 <peek_pgm+0x79>
movl   $0x2,-0x4(%rbp)
jmp    11c9 <peek_pgm+0x89>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek_pgm>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xea8(%rip),%rsi
mov    %rcx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1192 <peek_pgm+0x52>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <get_pgm_header@plt>
mov    %rax,%rcx
mov    $0x2,%eax
test   %rcx,%rcx
jne    11a3 <peek_pgm+0x63>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    11a3 <peek_pgm+0x63>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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