#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 /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sscanf(char *, char *, int *, ...);
scalar_t__ strcmp(char *, char *);
int read_pnm_header(FILE *f, int *w, int *h, int *max) {
  char buf[256];
  memset(buf, 0, sizeof(buf));
  int ret;
  fgets(buf, 256, f);
  if (strcmp(buf, "P6\n") == 0) {
    ret = 6;
  } else if (strcmp(buf, "P5\n") == 0) {
    ret = 5;
  } else {
    return 0;
  }
  do {
    fgets(buf, 256, f);
  } while (buf[0] == '#');
  sscanf(buf, "%d %d", w, h);
  if (*w < 0 || *h < 0)
    return 0;
  fgets(buf, 256, f);
  sscanf(buf, "%d", max);
  if (*max > 255)
    return 0;
  return ret;
}


x86 assembly (clang, -O0):
<read_pnm_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
lea    -0x130(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11af <read_pnm_header+0x6f>
movl   $0x6,-0x134(%rbp)
jmp    11ec <read_pnm_header+0xac>
lea    -0x130(%rbp),%rdi
lea    0xe47(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11db <read_pnm_header+0x9b>
movl   $0x5,-0x134(%rbp)
jmp    11e7 <read_pnm_header+0xa7>
movl   $0x0,-0x4(%rbp)
jmp    12ac <read_pnm_header+0x16c>
jmp    11ec <read_pnm_header+0xac>
jmp    11f1 <read_pnm_header+0xb1>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
movsbl -0x130(%rbp),%eax
cmp    $0x23,%eax
je     11f1 <read_pnm_header+0xb1>
lea    -0x130(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jl     124d <read_pnm_header+0x10d>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1259 <read_pnm_header+0x119>
movl   $0x0,-0x4(%rbp)
jmp    12ac <read_pnm_header+0x16c>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    -0x28(%rbp),%rax
cmpl   $0xff,(%rax)
jle    12a3 <read_pnm_header+0x163>
movl   $0x0,-0x4(%rbp)
jmp    12ac <read_pnm_header+0x16c>
mov    -0x134(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_pnm_header>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0x10(%rsp),%rbp
xor    %r13d,%r13d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1030 <memset@plt>
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
lea    0xe75(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
mov    %r14,0x8(%rsp)
je     11bf <read_pnm_header+0x7f>
lea    0xe60(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    124e <read_pnm_header+0x10e>
mov    $0x5,%r14d
jmp    11c5 <read_pnm_header+0x85>
mov    $0x6,%r14d
lea    0x10(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
cmpb   $0x23,0x10(%rsp)
je     11d0 <read_pnm_header+0x90>
lea    0xe1a(%rip),%rsi
xor    %r13d,%r13d
lea    0x10(%rsp),%rdi
mov    %r12,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <sscanf@plt>
cmpl   $0x0,(%r12)
js     124e <read_pnm_header+0x10e>
cmpl   $0x0,(%r15)
js     124e <read_pnm_header+0x10e>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
lea    0xddf(%rip),%rsi
xor    %ebx,%ebx
mov    %rbp,%rdi
mov    0x8(%rsp),%rbp
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <sscanf@plt>
cmpl   $0x100,0x0(%rbp)
cmovge %ebx,%r14d
mov    %r14d,%r13d
mov    %r13d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"P6\0A\00"
@.str.1 = [4 x i8] c"P5\0A\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @read_pnm_header(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [256 x i8]
%11 = alloca i32
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 256)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%15 = load i32*, i32** %6
%16 = call i32 @fgets(i8* %14, i32 256, i32* %15)
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 6, i32* %11
br label %28
21: ; preds = %4
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 5, i32* %11
br label %27
26: ; preds = %21
store i32 0, i32* %5
br label %64
27: ; preds = %25
br label %28
28: ; preds = %27, %20
br label %29
29: ; preds = %33, %28
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%31 = load i32*, i32** %6
%32 = call i32 @fgets(i8* %30, i32 256, i32* %31)
br label %33
33: ; preds = %29
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 35
br i1 %37, label %29, label %38
38: ; preds = %33
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%40 = load i32*, i32** %7
%41 = load i32*, i32** %8
%42 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %40, i32* %41)
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = icmp slt i32 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %38
%47 = load i32*, i32** %8
%48 = load i32, i32* %47
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46, %38
store i32 0, i32* %5
br label %64
51: ; preds = %46
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%53 = load i32*, i32** %6
%54 = call i32 @fgets(i8* %52, i32 256, i32* %53)
%55 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%56 = load i32*, i32** %9
%57 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %56)
%58 = load i32*, i32** %9
%59 = load i32, i32* %58
%60 = icmp sgt i32 %59, 255
br i1 %60, label %61, label %62
61: ; preds = %51
store i32 0, i32* %5
br label %64
62: ; preds = %51
%63 = load i32, i32* %11
store i32 %63, i32* %5
br label %64
64: ; preds = %62, %61, %50, %26
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@.str = [4 x i8] c"P6\0A\00"
@.str.1 = [4 x i8] c"P5\0A\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [3 x i8] c"%d\00"
define i32 @read_pnm_header(i32* nocapture %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i32 0, i32 256)
%8 = call i32 @fgets(i8* %6, i32 256, i32* %0)
%9 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %4
%12 = call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %33
14: ; preds = %11, %4
%15 = phi i32 [ 6, %4 ], [ 5, %11 ]
br label %16
16: ; preds = %16, %14
%17 = call i32 @fgets(i8* %6, i32 256, i32* %0)
%18 = load i8, i8* %6
%19 = icmp eq i8 %18, 35
br i1 %19, label %16, label %20
20: ; preds = %16
%21 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %1, i32* %2)
%22 = load i32, i32* %1
%23 = icmp slt i32 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %20
%25 = load i32, i32* %2
%26 = icmp slt i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = call i32 @fgets(i8* %6, i32 256, i32* %0)
%29 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %3)
%30 = load i32, i32* %3
%31 = icmp sgt i32 %30, 255
%32 = select i1 %31, i32 0, i32 %15
br label %33
33: ; preds = %27, %20, %24, %11
%34 = phi i32 [ 0, %11 ], [ 0, %24 ], [ 0, %20 ], [ %32, %27 ]
ret i32 %34
}