#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sscanf(char *, char *, int *, ...);
void readHeader(char *fileName, char *mode, char **magicNumber, int *idim,
                int *jdim, int *dynamic, int *headerSize) {
  char fileLine[2048];
  int numberLine = 0;
  FILE *file;
  file = fopen(fileName, mode);
  if (file != NULL) {
    fgets(fileLine, 2048, file);
    *magicNumber = fileLine;
    if (fileLine[0] == 'P' && fileLine[1] == '5') {
      fgets(fileLine, 2048, file);
      numberLine++;
      while (fileLine[0] == '#') {
        fgets(fileLine, 2048, file);
        numberLine++;
      }
      sscanf(fileLine, "%d %d", idim, jdim);
      fgets(fileLine, 2048, file);
      numberLine++;
      sscanf(fileLine, "%d", dynamic);
      numberLine++;
      *headerSize = numberLine;
    }
    fclose(file);
  } else {
    printf("Error opening file readHeader");
  }
}


x86 assembly (clang, -O0):
<readHeader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x834(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x840(%rbp)
cmpq   $0x0,-0x840(%rbp)
je     12d5 <readHeader+0x185>
lea    -0x830(%rbp),%rdi
mov    -0x840(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
lea    -0x830(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movsbl -0x830(%rbp),%eax
cmp    $0x50,%eax
jne    12c4 <readHeader+0x174>
movsbl -0x82f(%rbp),%eax
cmp    $0x35,%eax
jne    12c4 <readHeader+0x174>
lea    -0x830(%rbp),%rdi
mov    -0x840(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
mov    -0x834(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x834(%rbp)
movsbl -0x830(%rbp),%eax
cmp    $0x23,%eax
jne    124c <readHeader+0xfc>
lea    -0x830(%rbp),%rdi
mov    -0x840(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
mov    -0x834(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x834(%rbp)
jmp    1210 <readHeader+0xc0>
lea    -0x830(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1060 <sscanf@plt>
lea    -0x830(%rbp),%rdi
mov    -0x840(%rbp),%rdx
mov    $0x800,%esi
call   1050 <fgets@plt>
mov    -0x834(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x834(%rbp)
lea    -0x830(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xd61(%rip),%rsi
mov    $0x0,%al
call   1060 <sscanf@plt>
mov    -0x834(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x834(%rbp)
mov    -0x834(%rbp),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x840(%rbp),%rdi
call   1030 <fclose@plt>
jmp    12e1 <readHeader+0x191>
lea    0xd2a(%rip),%rdi
call   1040 <printf@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readHeader>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%r12
mov    %rdx,%r13
mov    0x850(%rsp),%rbp
call   1070 <fopen@plt>
test   %rax,%rax
je     126e <readHeader+0x11e>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x800,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rbp,0x0(%r13)
cmpb   $0x50,0x10(%rsp)
jne    1254 <readHeader+0x104>
cmpb   $0x35,0x11(%rsp)
jne    1254 <readHeader+0x104>
mov    %r14,(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    $0x3,%r14d
cmpb   $0x23,0x10(%rsp)
jne    120b <readHeader+0xbb>
mov    $0x3,%r14d
lea    0x10(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
add    $0x1,%r14d
cmpb   $0x23,0x10(%rsp)
je     11f0 <readHeader+0xa0>
lea    0xdee(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <sscanf@plt>
mov    %rbp,%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
lea    0xdc5(%rip),%rsi
mov    %rbp,%rdi
mov    (%rsp),%rdx
xor    %eax,%eax
call   1060 <sscanf@plt>
mov    0x8(%rsp),%rax
mov    %r14d,(%rax)
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd91(%rip),%rdi
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [30 x i8] c"Error opening file readHeader\00"
define void @readHeader(i8* %0, i8* %1, i8** %2, i32* %3, i32* %4, i32* %5, i32* %6) {
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca [2048 x i8]
%16 = alloca i32
%17 = alloca i32*
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8** %2, i8*** %10
store i32* %3, i32** %11
store i32* %4, i32** %12
store i32* %5, i32** %13
store i32* %6, i32** %14
store i32 0, i32* %16
%18 = load i8*, i8** %8
%19 = load i8*, i8** %9
%20 = call i32* @fopen(i8* %18, i8* %19)
store i32* %20, i32** %17
%21 = load i32*, i32** %17
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %75
23: ; preds = %7
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%25 = load i32*, i32** %17
%26 = call i32 @fgets(i8* %24, i32 2048, i32* %25)
%27 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%28 = load i8**, i8*** %10
store i8* %27, i8** %28
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 80
br i1 %32, label %33, label %72
33: ; preds = %23
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 53
br i1 %37, label %38, label %72
38: ; preds = %33
%39 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%40 = load i32*, i32** %17
%41 = call i32 @fgets(i8* %39, i32 2048, i32* %40)
%42 = load i32, i32* %16
%43 = add nsw i32 %42, 1
store i32 %43, i32* %16
br label %44
44: ; preds = %49, %38
%45 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 35
br i1 %48, label %49, label %55
49: ; preds = %44
%50 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%51 = load i32*, i32** %17
%52 = call i32 @fgets(i8* %50, i32 2048, i32* %51)
%53 = load i32, i32* %16
%54 = add nsw i32 %53, 1
store i32 %54, i32* %16
br label %44
55: ; preds = %44
%56 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%57 = load i32*, i32** %11
%58 = load i32*, i32** %12
%59 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %57, i32* %58)
%60 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%61 = load i32*, i32** %17
%62 = call i32 @fgets(i8* %60, i32 2048, i32* %61)
%63 = load i32, i32* %16
%64 = add nsw i32 %63, 1
store i32 %64, i32* %16
%65 = getelementptr inbounds [2048 x i8], [2048 x i8]* %15, i64 0, i64 0
%66 = load i32*, i32** %13
%67 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %66)
%68 = load i32, i32* %16
%69 = add nsw i32 %68, 1
store i32 %69, i32* %16
%70 = load i32, i32* %16
%71 = load i32*, i32** %14
store i32 %70, i32* %71
br label %72
72: ; preds = %55, %33, %23
%73 = load i32*, i32** %17
%74 = call i32 @fclose(i32* %73)
br label %77
75: ; preds = %7
%76 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %77
77: ; preds = %75, %72
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [30 x i8] c"Error opening file readHeader\00"
define void @readHeader(i8* nocapture %0, i8* nocapture %1, i8** nocapture writeonly %2, i32* %3, i32* %4, i32* %5, i32* nocapture writeonly %6) {
%8 = alloca [2048 x i8]
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%10 = tail call i32* @fopen(i8* %0, i8* %1)
%11 = icmp eq i32* %10, null
br i1 %11, label %39, label %12
12: ; preds = %7
%13 = call i32 @fgets(i8* %9, i32 2048, i32* %10)
store i8* %9, i8** %2
%14 = load i8, i8* %9
%15 = icmp eq i8 %14, 80
%16 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 53
%19 = select i1 %15, i1 %18, i1 false
br i1 %19, label %20, label %37
20: ; preds = %12
%21 = call i32 @fgets(i8* %9, i32 2048, i32* %10)
%22 = load i8, i8* %9
%23 = icmp eq i8 %22, 35
br i1 %23, label %24, label %32
24: ; preds = %20, %24
%25 = phi i32 [ %27, %24 ], [ 2, %20 ]
%26 = call i32 @fgets(i8* %9, i32 2048, i32* %10)
%27 = add i32 %25, 1
%28 = load i8, i8* %9
%29 = icmp eq i8 %28, 35
br i1 %29, label %24, label %30
30: ; preds = %24
%31 = add i32 %25, 2
br label %32
32: ; preds = %30, %20
%33 = phi i32 [ 3, %20 ], [ %31, %30 ]
%34 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %3, i32* %4)
%35 = call i32 @fgets(i8* %9, i32 2048, i32* %10)
%36 = call i32 (i8*, i8*, i32*, ...) @sscanf(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %5)
store i32 %33, i32* %6
br label %37
37: ; preds = %32, %12
%38 = call i32 @fclose(i32* %10)
br label %41
39: ; preds = %7
%40 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %37
ret void
}