#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*/ SEEK_SET;
int /*<<< orphan*/ fgetc(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int read_ppm_header(FILE *f, int *wd, int *ht) {
  int r = 0;
  char buf[2];
  r = fseek(f, 0, SEEK_SET);
  if (r != 0) {
    r = -1;
    goto exit;
  }
  r = fread(buf, 1, 2, f);
  if (r != 2 || buf[0] != 'P' || buf[1] != '6') {
    r = -1;
    goto exit;
  }
  r = fscanf(f, "%*[^0-9]%d %d\n%*[^\n]", wd, ht);
  fgetc(f);
  r = (r != 2) ? -1 : 0;
exit:
  return r;
}


x86 assembly (clang, -O0):
<read_ppm_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1188 <read_ppm_header+0x48>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1209 <read_ppm_header+0xc9>
lea    -0x1e(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1030 <fread@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jne    11c6 <read_ppm_header+0x86>
movsbl -0x1e(%rbp),%eax
cmp    $0x50,%eax
jne    11c6 <read_ppm_header+0x86>
movsbl -0x1d(%rbp),%eax
cmp    $0x36,%eax
je     11d2 <read_ppm_header+0x92>
movl   $0xffffffff,-0x1c(%rbp)
jmp    1209 <read_ppm_header+0xc9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe1b(%rip),%rsi
call   1040 <fscanf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <fgetc@plt>
mov    -0x1c(%rbp),%edx
xor    %eax,%eax
mov    $0xffffffff,%ecx
cmp    $0x2,%edx
cmovne %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ppm_header>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    11bf <read_ppm_header+0x7f>
lea    0x6(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x2,%eax
jne    11bf <read_ppm_header+0x7f>
cmpb   $0x50,0x6(%rsp)
jne    11bf <read_ppm_header+0x7f>
cmpb   $0x36,0x7(%rsp)
jne    11bf <read_ppm_header+0x7f>
lea    0xe66(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <fscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1050 <fgetc@plt>
xor    %ebp,%ebp
cmp    $0x2,%r14d
setne  %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [21 x i8] c"%*[^0-9]%d %d\0A%*[^\0A]\00"
define i32 @read_ppm_header(i32* %0, i32* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [2 x i8]
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i32 0, i32* %7
%9 = load i32*, i32** %4
%10 = load i32, i32* @SEEK_SET
%11 = call i32 @fseek(i32* %9, i32 0, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %7
br label %43
15: ; preds = %3
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%17 = load i32*, i32** %4
%18 = call i32 @fread(i8* %16, i32 1, i32 2, i32* %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp ne i32 %19, 2
br i1 %20, label %31, label %21
21: ; preds = %15
%22 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 80
br i1 %25, label %31, label %26
26: ; preds = %21
%27 = getelementptr inbounds [2 x i8], [2 x i8]* %8, i64 0, i64 1
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 54
br i1 %30, label %31, label %32
31: ; preds = %26, %21, %15
store i32 -1, i32* %7
br label %43
32: ; preds = %26
%33 = load i32*, i32** %4
%34 = load i32*, i32** %5
%35 = load i32*, i32** %6
%36 = call i32 @fscanf(i32* %33, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32* %34, i32* %35)
store i32 %36, i32* %7
%37 = load i32*, i32** %4
%38 = call i32 @fgetc(i32* %37)
%39 = load i32, i32* %7
%40 = icmp ne i32 %39, 2
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 -1, i32 0
store i32 %42, i32* %7
br label %43
43: ; preds = %32, %31, %14
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str = [21 x i8] c"%*[^0-9]%d %d\0A%*[^\0A]\00"
define i32 @read_ppm_header(i32* nocapture %0, i32* %1, i32* %2) {
%4 = alloca [2 x i8]
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @fseek(i32* %0, i32 0, i32 %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %3
%10 = call i32 @fread(i8* %5, i32 1, i32 2, i32* %0)
%11 = icmp ne i32 %10, 2
%12 = load i8, i8* %5
%13 = icmp ne i8 %12, 80
%14 = select i1 %11, i1 true, i1 %13
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%16 = load i8, i8* %15
%17 = icmp ne i8 %16, 54
%18 = select i1 %14, i1 true, i1 %17
br i1 %18, label %24, label %19
19: ; preds = %9
%20 = tail call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32* %1, i32* %2)
%21 = tail call i32 @fgetc(i32* %0)
%22 = icmp ne i32 %20, 2
%23 = sext i1 %22 to i32
br label %24
24: ; preds = %9, %3, %19
%25 = phi i32 [ %23, %19 ], [ -1, %3 ], [ -1, %9 ]
ret i32 %25
}