// File: /workspace/github_cpp_makefile_star2_O2/cloudinary[P]fuif/./import/read_pam.h
// Function: read_PAM_file(char const*)
Image read_PAM_file(const char *filename) {
    FILE *fp = NULL;
    if (!strcmp(filename,"-")) fp = stdin;
    else fp = fopen(filename,"rb");

    char buf[PPMREADBUFLEN], *t;
    if (!fp) {
        e_printf("Could not open file: %s\n", filename);
        return Image();
    }
    unsigned int width=0,height=0;
    unsigned int maxval=0,nbchans=0;
    do {
        /* # comments before the first line */
        t = fgets(buf, PPMREADBUFLEN, fp);
        if ( t == NULL ) { fclose(fp); return Image(); }
    } while ( strncmp(buf, "#", 1) == 0 || strncmp(buf, "\n", 1) == 0);
    int type=0;
    if ( (!strncmp(buf, "P4", 2)) ) type=4;
    if ( (!strncmp(buf, "P5", 2)) ) type=5;
    if ( (!strncmp(buf, "P6", 2)) ) type=6;
    if ( (!strncmp(buf, "P7", 2)) ) type=7;
    if (type==0) {
        if (buf[0] == 'P') e_printf("PNM file is not of type P4, P5, P6 or P7, cannot read other types.\n");
        else v_printf(8,"This does not look like a PNM file.\n");
        fclose(fp);
        return Image();
    }
    t += 2;

    v_printf(7,"Trying to load a PNM/PAM file of type P%i\n",type);
    if (type < 7) {
      if (!(width = read_pnm_int(fp,buf,&t))) return Image();
      if (!(height = read_pnm_int(fp,buf,&t))) return Image();
      if (type>4) {
        if (!(maxval = read_pnm_int(fp,buf,&t))) return Image();
        if ( maxval > 0xffff ) {
          e_printf("Invalid PNM file (more than 16-bit?)\n");
          fclose(fp);
          return Image();
        }
      } else maxval=1;
      nbchans=(type==6?3:1);
    } else {
      int maxlines = 100;
      do {
        t = fgets(buf, PPMREADBUFLEN, fp);
        if ( t == NULL ) {
            fclose(fp);
            return Image();
        }
        if (strncmp(buf, "#", 1) == 0 || strncmp(buf, "\n", 1) == 0)
            continue;

        sscanf(buf, "WIDTH %u\n", &width);
        sscanf(buf, "HEIGHT %u\n", &height);
        sscanf(buf, "DEPTH %u\n", &nbchans);
        sscanf(buf, "MAXVAL %u\n", &maxval);

        if (maxlines-- < 1) {
            e_printf("Problem while parsing PAM header.\n");
            fclose(fp);
            return Image();
        }
      } while ( strncmp(buf, "ENDHDR", 6) != 0 );
      if (nbchans>4 || nbchans <1 || width <1 || height < 1 || maxval<1 || maxval > 0xffff) {
        e_printf("Couldn't parse PAM header, or unsupported kind of PAM file.\n");
        fclose(fp);
        return Image();
      }
    }

    v_printf(7,"Loading %ix%i PNM/PAM file with %i channels and maxval=%i\n",width,height,nbchans,maxval);
    Image image(width,height, maxval, nbchans);
    if (type==4) {
      char byte=0;
      for (unsigned int y=0; y<height; y++) {
        for (unsigned int x=0; x<width; x++) {
                if (x%8 == 0) byte = fgetc(fp);
                image.channel[0].value(y,x) = (byte & (128>>(x%8)) ? 0 : 1);
        }
      }
    } else {
      if (maxval > 0xff) {
        for (unsigned int y=0; y<height; y++) {
          for (unsigned int x=0; x<width; x++) {
            for (unsigned int c=0; c<nbchans; c++) {
                pixel_type pixel= (fgetc(fp) << 8);
                pixel += fgetc(fp);
                if (pixel > (int)maxval) {
                    fclose(fp);
                    e_printf("Invalid PNM/PAM file: value %i is larger than declared maxval %u\n", pixel, maxval);
                    return Image();
                }
                image.channel[c].value(y,x) = pixel;
            }
          }
        }
      } else {
        for (unsigned int y=0; y<height; y++) {
          for (unsigned int x=0; x<width; x++) {
            for (unsigned int c=0; c<nbchans; c++) {
                image.channel[c].value(y,x) = fgetc(fp);
            }
          }
        }
      }
    }
    if (fp != stdin) fclose(fp);
    return image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rsi, %r15
movq %rdi, %rbx
cmpb $0x2d, (%rsi)
jne 0x18890
cmpb $0x0, 0x1(%r15)
je 0x18a88
leaq 0x6188(%rip), %rsi      # 0x1ea1f
movq %r15, %rdi
callq 0x33a0
movq %rax, %r14
testq %r14, %r14
je 0x18a72
andl $0x0, 0x10(%rsp)
andl $0x0, 0xc(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, 0x14(%rsp)
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3510
testq %rax, %rax
je 0x18b44
movzbl 0xe0(%rsp), %ecx
cmpl $0xa, %ecx
je 0x188c7
cmpl $0x23, %ecx
je 0x188c7
movzwl 0xe0(%rsp), %r15d
xorl %ecx, %ecx
cmpl $0x3450, %r15d          # imm = 0x3450
sete %cl
shll $0x2, %ecx
cmpl $0x3550, %r15d          # imm = 0x3550
pushq $0x5
popq %rdx
cmovnel %ecx, %edx
cmpl $0x3650, %r15d          # imm = 0x3650
pushq $0x6
popq %rcx
cmovnel %edx, %ecx
cmpl $0x3750, %r15d          # imm = 0x3750
pushq $0x7
popq %rbp
cmovnel %ecx, %ebp
testl %ebp, %ebp
je 0x18a97
addq $0x2, %rax
movq %rax, 0x20(%rsp)
leaq 0x68ec(%rip), %rsi      # 0x1f235
pushq $0x7
popq %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x16c30
cmpl $0x3750, %r15d          # imm = 0x3750
jne 0x18ab4
movl %ebp, 0x18(%rsp)
pushq $0x64
popq %rbp
leaq 0xe0(%rsp), %r15
leaq 0x695b(%rip), %r12      # 0x1f2d3
pushq $0x6
popq %r13
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x3510
testq %rax, %rax
je 0x18b44
movzbl 0xe0(%rsp), %eax
cmpl $0xa, %eax
je 0x18a09
cmpl $0x23, %eax
je 0x18a09
movq %r15, %rdi
leaq 0x68d5(%rip), %rsi      # 0x1f286
leaq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x32b0
movq %r15, %rdi
leaq 0x68c9(%rip), %rsi      # 0x1f290
leaq 0xc(%rsp), %rdx
xorl %eax, %eax
callq 0x32b0
movq %r15, %rdi
leaq 0x68be(%rip), %rsi      # 0x1f29b
leaq 0x14(%rsp), %rdx
xorl %eax, %eax
callq 0x32b0
movq %r15, %rdi
leaq 0x68b2(%rip), %rsi      # 0x1f2a5
leaq 0x8(%rsp), %rdx
xorl %eax, %eax
callq 0x32b0
testl %ebp, %ebp
jle 0x18b6f
decl %ebp
movq %r15, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3300
testl %eax, %eax
jne 0x1897c
movl 0x14(%rsp), %r8d
leal -0x5(%r8), %eax
cmpl $-0x4, %eax
setb %cl
movl 0x10(%rsp), %edx
testl %edx, %edx
sete %sil
movl 0xc(%rsp), %eax
testl %eax, %eax
sete %dil
orb %sil, %dil
orb %cl, %dil
movl 0x8(%rsp), %r9d
leal -0x10000(%r9), %ecx
cmpl $0xffff0001, %ecx       # imm = 0xFFFF0001
setb %cl
orb %dil, %cl
cmpb $0x1, %cl
jne 0x18b69
leaq 0x686a(%rip), %rdi      # 0x1f2da
jmp 0x18aa8
leaq 0x673a(%rip), %rdi      # 0x1f1b3
movq %r15, %rsi
xorl %eax, %eax
callq 0x16b80
jmp 0x18b4c
movq 0x13541(%rip), %rax     # 0x2bfd0
movq (%rax), %r14
jmp 0x188a2
cmpb $0x50, %r15b
jne 0x18b33
leaq 0x6724(%rip), %rdi      # 0x1f1cc
xorl %eax, %eax
callq 0x16b80
jmp 0x18b44
leaq 0xe0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
callq 0x187da
movl %eax, 0x10(%rsp)
testl %eax, %eax
je 0x18b4c
leaq 0xe0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
callq 0x187da
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x18b4c
cmpl $0x3650, %r15d          # imm = 0x3650
je 0x18b00
cmpl $0x3550, %r15d          # imm = 0x3550
jne 0x18b7b
leaq 0xe0(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r14, %rdi
callq 0x187da
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0x18b4c
movl %eax, %r9d
cmpl $0xffff, %eax           # imm = 0xFFFF
jbe 0x18b89
leaq 0x6732(%rip), %rdi      # 0x1f260
jmp 0x18aa8
leaq 0x66d6(%rip), %rsi      # 0x1f210
pushq $0x8
popq %rdi
xorl %eax, %eax
callq 0x16c30
movq %r14, %rdi
callq 0x32c0
movq %rbx, %rdi
callq 0xd18a
movq %rbx, %rax
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x18(%rsp), %ebp
jmp 0x18baa
leaq 0x673a(%rip), %rdi      # 0x1f2b0
jmp 0x18aa8
movl $0x1, 0x8(%rsp)
pushq $0x1
popq %r9
jmp 0x18b8d
movl 0xc(%rsp), %eax
xorl %ecx, %ecx
cmpl $0x3650, %r15d          # imm = 0x3650
sete %cl
leal 0x1(,%rcx,2), %r8d
movl %r8d, 0x14(%rsp)
movl 0x10(%rsp), %edx
leaq 0x6766(%rip), %rsi      # 0x1f317
xorl %r15d, %r15d
pushq $0x7
popq %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x16c30
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %ecx
movl 0x14(%rsp), %r8d
leaq 0x30(%rsp), %rdi
xorl %r9d, %r9d
callq 0xfe5a
cmpl $0x4, %ebp
movq %rbx, 0x28(%rsp)
jne 0x18c3f
xorl %ebp, %ebp
cmpl 0xc(%rsp), %r15d
jae 0x18d21
xorl %r12d, %r12d
cmpl 0x10(%rsp), %r12d
jae 0x18c3a
movl %r12d, %r13d
andl $0x7, %r13d
jne 0x18c12
movq %r14, %rdi
callq 0x34f0
movl %eax, %ebp
movzbl %bpl, %eax
movl %r13d, %ecx
shll %cl, %eax
xorl %ebx, %ebx
testb %al, %al
setns %bl
movq 0x30(%rsp), %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0xa44a
movw %bx, (%rax)
incl %r12d
jmp 0x18bf8
incl %r15d
jmp 0x18bea
cmpl $0xff, 0x8(%rsp)
jbe 0x18cce
xorl %eax, %eax
cmpl 0xc(%rsp), %eax
jae 0x18d21
movq %rax, 0x18(%rsp)
xorl %ebx, %ebx
cmpl 0x10(%rsp), %ebx
jae 0x18cc5
xorl %r15d, %r15d
xorl %ebp, %ebp
movl 0x14(%rsp), %eax
cmpq %rax, %rbp
jae 0x18cc1
movq %r14, %rdi
callq 0x34f0
movl %eax, %r13d
shll $0x8, %r13d
movq %r14, %rdi
callq 0x34f0
movl %eax, %r12d
addl %r13d, %r12d
movswl %r12w, %r13d
cmpl 0x8(%rsp), %r13d
jg 0x18d49
movq 0x30(%rsp), %rdi
addq %r15, %rdi
movq 0x18(%rsp), %rsi
movl %ebx, %edx
callq 0xa44a
movw %r12w, (%rax)
incq %rbp
addq $0x40, %r15
jmp 0x18c6b
incl %ebx
jmp 0x18c60
movq 0x18(%rsp), %rax
incl %eax
jmp 0x18c4f
xorl %r15d, %r15d
cmpl 0xc(%rsp), %r15d
jae 0x18d21
xorl %ebp, %ebp
cmpl 0x10(%rsp), %ebp
jae 0x18d1c
xorl %r13d, %r13d
xorl %ebx, %ebx
movl 0x14(%rsp), %eax
cmpq %rax, %rbx
jae 0x18d18
movq %r14, %rdi
callq 0x34f0
movl %eax, %r12d
movq 0x30(%rsp), %rdi
addq %r13, %rdi
movl %r15d, %esi
movl %ebp, %edx
callq 0xa44a
movw %r12w, (%rax)
incq %rbx
addq $0x40, %r13
jmp 0x18ce5
incl %ebp
jmp 0x18cda
incl %r15d
jmp 0x18cd1
movq 0x132a8(%rip), %rax     # 0x2bfd0
cmpq (%rax), %r14
je 0x18d35
movq %r14, %rdi
callq 0x32c0
leaq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x1c62e
jmp 0x18d73
movq %r14, %rdi
callq 0x32c0
movl 0x8(%rsp), %edx
leaq 0x65f6(%rip), %rdi      # 0x1f352
movl %r13d, %esi
xorl %eax, %eax
callq 0x16b80
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0xd18a
leaq 0x30(%rsp), %rdi
callq 0xa776
jmp 0x18b54
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa776
movq %rbx, %rdi
callq 0x35d0