#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 */
void *atoi(char *);
scalar_t__ feof(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
scalar_t__ get_next_token(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ strcmp(char *, char *);
int get_pgm_header(FILE *f, size_t *pwidth, size_t *pheight, int *pmaxval) {
  char w;
  char buf[1024];
  if (get_next_token(f, buf))
    return 1;
  if (strcmp("P5", buf) != 0) {
    printf("ERR:Image not binary 8-bit grayscale.\n");
    return 7;
  }
  if (get_next_token(f, buf))
    return 2;
  *pwidth = atoi(buf);
  if (get_next_token(f, buf))
    return 3;
  *pheight = atoi(buf);
  if (get_next_token(f, buf))
    return 4;
  *pmaxval = atoi(buf);
  w = fgetc(f);
  if (feof(f))
    return 5;
  if ((w != ' ') && (w != '\n') && (w != '\r') && (w != '\t')) {
    return 6;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_pgm_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <get_next_token@plt>
cmp    $0x0,%rax
je     11a1 <get_pgm_header+0x41>
movl   $0x1,-0x4(%rbp)
jmp    12fa <get_pgm_header+0x19a>
lea    -0x430(%rbp),%rsi
lea    0xe51(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11d6 <get_pgm_header+0x76>
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x7,-0x4(%rbp)
jmp    12fa <get_pgm_header+0x19a>
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <get_next_token@plt>
cmp    $0x0,%rax
je     11fc <get_pgm_header+0x9c>
movl   $0x2,-0x4(%rbp)
jmp    12fa <get_pgm_header+0x19a>
lea    -0x430(%rbp),%rdi
call   1080 <atoi@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <get_next_token@plt>
cmp    $0x0,%rax
je     1238 <get_pgm_header+0xd8>
movl   $0x3,-0x4(%rbp)
jmp    12fa <get_pgm_header+0x19a>
lea    -0x430(%rbp),%rdi
call   1080 <atoi@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1070 <get_next_token@plt>
cmp    $0x0,%rax
je     1274 <get_pgm_header+0x114>
movl   $0x4,-0x4(%rbp)
jmp    12fa <get_pgm_header+0x19a>
lea    -0x430(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     12b3 <get_pgm_header+0x153>
movl   $0x5,-0x4(%rbp)
jmp    12fa <get_pgm_header+0x19a>
movsbl -0x29(%rbp),%eax
cmp    $0x20,%eax
je     12f3 <get_pgm_header+0x193>
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
je     12f3 <get_pgm_header+0x193>
movsbl -0x29(%rbp),%eax
cmp    $0xd,%eax
je     12f3 <get_pgm_header+0x193>
movsbl -0x29(%rbp),%eax
cmp    $0x9,%eax
je     12f3 <get_pgm_header+0x193>
movl   $0x6,-0x4(%rbp)
jmp    12fa <get_pgm_header+0x19a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pgm_header>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1070 <get_next_token@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    1272 <get_pgm_header+0x112>
lea    0xe65(%rip),%rdi
mov    %rsp,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11be <get_pgm_header+0x5e>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
mov    $0x7,%eax
jmp    1272 <get_pgm_header+0x112>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <get_next_token@plt>
mov    %rax,%rcx
mov    $0x2,%eax
test   %rcx,%rcx
jne    1272 <get_pgm_header+0x112>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1080 <atoi@plt>
mov    %rax,(%r12)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <get_next_token@plt>
mov    %rax,%rcx
mov    $0x3,%eax
test   %rcx,%rcx
jne    1272 <get_pgm_header+0x112>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1080 <atoi@plt>
mov    %rax,(%r15)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <get_next_token@plt>
mov    %rax,%rcx
mov    $0x4,%eax
test   %rcx,%rcx
jne    1272 <get_pgm_header+0x112>
mov    %rsp,%rdi
call   1080 <atoi@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1040 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <feof@plt>
mov    %rax,%rcx
mov    $0x5,%eax
test   %rcx,%rcx
jne    1272 <get_pgm_header+0x112>
mov    $0x6,%eax
cmp    $0x20,%bpl
ja     1272 <get_pgm_header+0x112>
movzbl %bpl,%ecx
movabs $0x100002600,%rdx
bt     %rcx,%rdx
jae    1272 <get_pgm_header+0x112>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"P5\00"
@.str.1 = [39 x i8] c"ERR:Image not binary 8-bit grayscale.\0A\00"
define i32 @get_pgm_header(i32* %0, i64* %1, i64* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i32*
%10 = alloca i8
%11 = alloca [1024 x i8]
store i32* %0, i32** %6
store i64* %1, i64** %7
store i64* %2, i64** %8
store i32* %3, i32** %9
%12 = load i32*, i32** %6
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%14 = call i64 @get_next_token(i32* %12, i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 1, i32* %5
br label %78
17: ; preds = %4
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%19 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 7, i32* %5
br label %78
23: ; preds = %17
%24 = load i32*, i32** %6
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%26 = call i64 @get_next_token(i32* %24, i8* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 2, i32* %5
br label %78
29: ; preds = %23
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%31 = call i8* @atoi(i8* %30)
%32 = ptrtoint i8* %31 to i64
%33 = load i64*, i64** %7
store i64 %32, i64* %33
%34 = load i32*, i32** %6
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%36 = call i64 @get_next_token(i32* %34, i8* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %29
store i32 3, i32* %5
br label %78
39: ; preds = %29
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%41 = call i8* @atoi(i8* %40)
%42 = ptrtoint i8* %41 to i64
%43 = load i64*, i64** %8
store i64 %42, i64* %43
%44 = load i32*, i32** %6
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%46 = call i64 @get_next_token(i32* %44, i8* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %39
store i32 4, i32* %5
br label %78
49: ; preds = %39
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%51 = call i8* @atoi(i8* %50)
%52 = ptrtoint i8* %51 to i32
%53 = load i32*, i32** %9
store i32 %52, i32* %53
%54 = load i32*, i32** %6
%55 = call signext i8 @fgetc(i32* %54)
store i8 %55, i8* %10
%56 = load i32*, i32** %6
%57 = call i64 @feof(i32* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %49
store i32 5, i32* %5
br label %78
60: ; preds = %49
%61 = load i8, i8* %10
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 32
br i1 %63, label %64, label %77
64: ; preds = %60
%65 = load i8, i8* %10
%66 = sext i8 %65 to i32
%67 = icmp ne i32 %66, 10
br i1 %67, label %68, label %77
68: ; preds = %64
%69 = load i8, i8* %10
%70 = sext i8 %69 to i32
%71 = icmp ne i32 %70, 13
br i1 %71, label %72, label %77
72: ; preds = %68
%73 = load i8, i8* %10
%74 = sext i8 %73 to i32
%75 = icmp ne i32 %74, 9
br i1 %75, label %76, label %77
76: ; preds = %72
store i32 6, i32* %5
br label %78
77: ; preds = %72, %68, %64, %60
store i32 0, i32* %5
br label %78
78: ; preds = %77, %76, %59, %48, %38, %28, %21, %16
%79 = load i32, i32* %5
ret i32 %79
}

LLVM IR (-O2):
@.str = [3 x i8] c"P5\00"
@str = [38 x i8] c"ERR:Image not binary 8-bit grayscale.\00"
define i32 @get_pgm_header(i32* %0, i64* nocapture writeonly %1, i64* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i64 @get_next_token(i32* %0, i8* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %36
9: ; preds = %4
%10 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %36
14: ; preds = %9
%15 = call i64 @get_next_token(i32* %0, i8* %6)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %36
17: ; preds = %14
%18 = call i8* @atoi(i8* %6)
%19 = ptrtoint i8* %18 to i64
store i64 %19, i64* %1
%20 = call i64 @get_next_token(i32* %0, i8* %6)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %17
%23 = call i8* @atoi(i8* %6)
%24 = ptrtoint i8* %23 to i64
store i64 %24, i64* %2
%25 = call i64 @get_next_token(i32* %0, i8* %6)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %22
%28 = call i8* @atoi(i8* %6)
%29 = ptrtoint i8* %28 to i64
%30 = trunc i64 %29 to i32
store i32 %30, i32* %3
%31 = call signext i8 @fgetc(i32* %0)
%32 = call i64 @feof(i32* %0)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
switch i8 %31, label %36 [
i8 32, label %35
i8 13, label %35
i8 10, label %35
i8 9, label %35
]
35: ; preds = %34, %34, %34, %34
br label %36
36: ; preds = %34, %27, %22, %17, %14, %4, %35, %12
%37 = phi i32 [ 7, %12 ], [ 0, %35 ], [ 1, %4 ], [ 2, %14 ], [ 3, %17 ], [ 4, %22 ], [ 5, %27 ], [ 6, %34 ]
ret i32 %37
}