#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ check_dimension(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *, int *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void read_ppm_header(FILE *fp, int *width, int *height, int *bpp) {
  char ch, line[80];
  fseek(fp, 0, SEEK_SET);
  fgets(line, 3, fp);
  if (line[1] != '6')
    do {
      fprintf(stderr,
              "El archivo no esta en formato ppm.\nNo se puede leer.\n");
      exit(EXIT_FAILURE);
    } while (0);
  fseek(fp, 3, SEEK_SET);
  ch = getc(fp);
  if (ch == '#') {
    do {
      fgets(line, sizeof line, fp);
    } while (line[0] == '#');
  } else {
    fseek(fp, 2, SEEK_SET);
  }
  fscanf(fp, "%d%d%d\n", width, height, bpp);
  if (*width == 0 || *height == 0 || *bpp == 0)
    do {
      fprintf(stderr, "No se pudo leer la cabecera ppm del archivo.\n");
      exit(EXIT_FAILURE);
    } while (0);
  if (*bpp != 255)
    do {
      fprintf(stderr, "La imagen no es true-color (24 bits).\n");
      exit(EXIT_FAILURE);
    } while (0);
  check_dimension(*width);
  check_dimension(*height);
}


x86 assembly (clang, -O0):
<read_ppm_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x3,%esi
call   1040 <fgets@plt>
movsbl -0x7f(%rbp),%eax
cmp    $0x36,%eax
je     11e6 <read_ppm_header+0x76>
jmp    11c3 <read_ppm_header+0x53>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    $0x3,%esi
call   1060 <fseek@plt>
mov    -0x8(%rbp),%rdi
call   1090 <getc@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0x23,%eax
jne    123f <read_ppm_header+0xcf>
jmp    121b <read_ppm_header+0xab>
lea    -0x80(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x50,%esi
call   1040 <fgets@plt>
movsbl -0x80(%rbp),%eax
cmp    $0x23,%eax
je     121b <read_ppm_header+0xab>
jmp    1256 <read_ppm_header+0xe6>
mov    -0x8(%rbp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
mov    $0x2,%esi
call   1060 <fseek@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xdc9(%rip),%rsi
call   1030 <fscanf@plt>
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
je     1299 <read_ppm_header+0x129>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
je     1299 <read_ppm_header+0x129>
mov    -0x20(%rbp),%rax
cmpl   $0x0,(%rax)
jne    12c1 <read_ppm_header+0x151>
jmp    129e <read_ppm_header+0x12e>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rax
cmpl   $0xff,(%rax)
je     12f9 <read_ppm_header+0x189>
jmp    12d6 <read_ppm_header+0x166>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd86(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <check_dimension@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <check_dimension@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ppm_header>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e48(%rip),%r13
mov    0x0(%r13),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %rsp,%rdi
mov    $0x3,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
cmpb   $0x36,0x1(%rsp)
jne    126b <read_ppm_header+0xfb>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x3,%esi
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1090 <getc@plt>
cmp    $0x23,%al
jne    11f8 <read_ppm_header+0x88>
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
cmpb   $0x23,(%rsp)
je     11e0 <read_ppm_header+0x70>
jmp    1209 <read_ppm_header+0x99>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <fseek@plt>
lea    0xe26(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %r14,%r8
call   1030 <fscanf@plt>
mov    (%r15),%edi
test   %edi,%edi
je     1259 <read_ppm_header+0xe9>
cmpl   $0x0,(%r12)
je     1259 <read_ppm_header+0xe9>
mov    (%r14),%eax
cmp    $0xff,%eax
jne    1255 <read_ppm_header+0xe5>
call   1070 <check_dimension@plt>
mov    (%r12),%edi
call   1070 <check_dimension@plt>
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
test   %eax,%eax
jne    127d <read_ppm_header+0x10d>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
jmp    128d <read_ppm_header+0x11d>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
jmp    128d <read_ppm_header+0x11d>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"El archivo no esta en formato ppm.\0ANo se puede leer.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"%d%d%d\0A\00"
@.str.2 = [46 x i8] c"No se pudo leer la cabecera ppm del archivo.\0A\00"
@.str.3 = [39 x i8] c"La imagen no es true-color (24 bits).\0A\00"
define void @read_ppm_header(i32* %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8
%10 = alloca [80 x i8]
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%11 = load i32*, i32** %5
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @fseek(i32* %11, i32 0, i32 %12)
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%15 = load i32*, i32** %5
%16 = call i32 @fgets(i8* %14, i32 3, i32* %15)
%17 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 1
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 54
br i1 %20, label %21, label %28
21: ; preds = %4
br label %22
22: ; preds = %21
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; No predecessors!
br label %28
28: ; preds = %27, %4
%29 = load i32*, i32** %5
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @fseek(i32* %29, i32 3, i32 %30)
%32 = load i32*, i32** %5
%33 = call signext i8 @getc(i32* %32)
store i8 %33, i8* %9
%34 = load i8, i8* %9
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 35
br i1 %36, label %37, label %48
37: ; preds = %28
br label %38
38: ; preds = %42, %37
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%40 = load i32*, i32** %5
%41 = call i32 @fgets(i8* %39, i32 80, i32* %40)
br label %42
42: ; preds = %38
%43 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 35
br i1 %46, label %38, label %47
47: ; preds = %42
br label %52
48: ; preds = %28
%49 = load i32*, i32** %5
%50 = load i32, i32* @SEEK_SET
%51 = call i32 @fseek(i32* %49, i32 2, i32 %50)
br label %52
52: ; preds = %48, %47
%53 = load i32*, i32** %5
%54 = load i32*, i32** %6
%55 = load i32*, i32** %7
%56 = load i32*, i32** %8
%57 = call i32 @fscanf(i32* %53, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* %54, i32* %55, i32* %56)
%58 = load i32*, i32** %6
%59 = load i32, i32* %58
%60 = icmp eq i32 %59, 0
br i1 %60, label %69, label %61
61: ; preds = %52
%62 = load i32*, i32** %7
%63 = load i32, i32* %62
%64 = icmp eq i32 %63, 0
br i1 %64, label %69, label %65
65: ; preds = %61
%66 = load i32*, i32** %8
%67 = load i32, i32* %66
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %76
69: ; preds = %65, %61, %52
br label %70
70: ; preds = %69
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; No predecessors!
br label %76
76: ; preds = %75, %65
%77 = load i32*, i32** %8
%78 = load i32, i32* %77
%79 = icmp ne i32 %78, 255
br i1 %79, label %80, label %87
80: ; preds = %76
br label %81
81: ; preds = %80
%82 = load i32, i32* @stderr
%83 = call i32 @fprintf(i32 %82, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%84 = load i32, i32* @EXIT_FAILURE
%85 = call i32 @exit(i32 %84)
unreachable
86: ; No predecessors!
br label %87
87: ; preds = %86, %76
%88 = load i32*, i32** %6
%89 = load i32, i32* %88
%90 = call i32 @check_dimension(i32 %89)
%91 = load i32*, i32** %7
%92 = load i32, i32* %91
%93 = call i32 @check_dimension(i32 %92)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [54 x i8] c"El archivo no esta en formato ppm.\0ANo se puede leer.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [8 x i8] c"%d%d%d\0A\00"
@.str.2 = [46 x i8] c"No se pudo leer la cabecera ppm del archivo.\0A\00"
@.str.3 = [39 x i8] c"La imagen no es true-color (24 bits).\0A\00"
define void @read_ppm_header(i32* nocapture %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca [80 x i8]
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @fseek(i32* %0, i32 0, i32 %7)
%9 = call i32 @fgets(i8* %6, i32 3, i32* %0)
%10 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 54
br i1 %12, label %18, label %13
13: ; preds = %4
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %4
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %0, i32 3, i32 %19)
%21 = call signext i8 @getc(i32* %0)
%22 = icmp eq i8 %21, 35
br i1 %22, label %23, label %27
23: ; preds = %18, %23
%24 = call i32 @fgets(i8* %6, i32 80, i32* %0)
%25 = load i8, i8* %6
%26 = icmp eq i8 %25, 35
br i1 %26, label %23, label %30
27: ; preds = %18
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @fseek(i32* %0, i32 2, i32 %28)
br label %30
30: ; preds = %23, %27
%31 = call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32* %1, i32* %2, i32* %3)
%32 = load i32, i32* %1
%33 = icmp eq i32 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %30
%35 = load i32, i32* %2
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %34
%38 = load i32, i32* %3
switch i32 %38, label %44 [
i32 0, label %39
i32 255, label %49
]
39: ; preds = %37, %30, %34
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %37
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %37
%50 = call i32 @check_dimension(i32 %32)
%51 = load i32, i32* %2
%52 = call i32 @check_dimension(i32 %51)
ret void
}