#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*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int readASCIIgrey(int /*<<< orphan*/ *, char **, int *, int *);
int readASCIIrgb(int /*<<< orphan*/ *, char **, int *, int *);
int readRGB(int /*<<< orphan*/ *, char **, int *, int *);
int readgrey(int /*<<< orphan*/ *, char **, int *, int *);
int read_image(char *file_name, char **image_data, int *width, int *height) {
  char identifier[4] = {0, 0, 0, 0};
  FILE *image = fopen(file_name, "rb");
  if (image == NULL) {
    printf("could not open file");
    return -1;
  }
  size_t numread = fread(identifier, sizeof(char), 3, image);
  printf("Read %u bytes\n", numread);
  if (numread != 3) {
    printf("could not read identifier file\n");
    fclose(image);
    return -1;
  }
  printf("%s\n", identifier);
  if (identifier[0] != 'P' || identifier[2] != ' ') {
    printf("identifier not correct");
    fclose(image);
    return -1;
  }
  int result = -1;
  if (identifier[1] == '5') {
    result = readgrey(image, image_data, width, height);
  } else if (identifier[1] == '6') {
    result = readRGB(image, image_data, width, height);
  } else if (identifier[1] == '2') {
    result = readASCIIgrey(image, image_data, width, height);
  } else if (identifier[1] == '3') {
    result = readASCIIrgb(image, image_data, width, height);
  } else {
    printf("Format not supported!!!");
  }
  fclose(image);
  return result;
}


x86 assembly (clang, -O0):
<read_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x2c(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1070 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f1 <read_image+0x61>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <read_image+0x1e6>
lea    -0x2c(%rbp),%rdi
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   1040 <fread@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpq   $0x3,-0x40(%rbp)
je     124c <read_image+0xbc>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <read_image+0x1e6>
lea    -0x2c(%rbp),%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movsbl -0x2c(%rbp),%eax
cmp    $0x50,%eax
jne    1278 <read_image+0xe8>
movsbl -0x2a(%rbp),%eax
cmp    $0x20,%eax
je     129b <read_image+0x10b>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1376 <read_image+0x1e6>
movl   $0xffffffff,-0x44(%rbp)
movsbl -0x2b(%rbp),%eax
cmp    $0x35,%eax
jne    12cc <read_image+0x13c>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1030 <readgrey@plt>
mov    %eax,-0x44(%rbp)
jmp    1367 <read_image+0x1d7>
movsbl -0x2b(%rbp),%eax
cmp    $0x36,%eax
jne    12f6 <read_image+0x166>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1080 <readRGB@plt>
mov    %eax,-0x44(%rbp)
jmp    1362 <read_image+0x1d2>
movsbl -0x2b(%rbp),%eax
cmp    $0x32,%eax
jne    1320 <read_image+0x190>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   10b0 <readASCIIgrey@plt>
mov    %eax,-0x44(%rbp)
jmp    135d <read_image+0x1cd>
movsbl -0x2b(%rbp),%eax
cmp    $0x33,%eax
jne    134a <read_image+0x1ba>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1090 <readASCIIrgb@plt>
mov    %eax,-0x44(%rbp)
jmp    1358 <read_image+0x1c8>
lea    0xd10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    135d <read_image+0x1cd>
jmp    1362 <read_image+0x1d2>
jmp    1367 <read_image+0x1d7>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_image>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
movl   $0x0,0xc(%rsp)
lea    0xe60(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     123d <read_image+0xad>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %rax,%rbp
lea    0xe49(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x3,%rbp
jne    1255 <read_image+0xc5>
lea    0xc(%rsp),%rdi
call   1040 <puts@plt>
cmpb   $0x50,0xc(%rsp)
jne    1263 <read_image+0xd3>
cmpb   $0x20,0xe(%rsp)
jne    1263 <read_image+0xd3>
mov    0xd(%rsp),%al
add    $0xce,%al
cmp    $0x4,%al
ja     1293 <read_image+0x103>
movzbl %al,%eax
lea    0xde2(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   10b0 <readASCIIgrey@plt>
jmp    12cc <read_image+0x13c>
lea    0xdd3(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%ebp
jmp    12d6 <read_image+0x146>
lea    0xe0d(%rip),%rdi
call   1040 <puts@plt>
jmp    1271 <read_image+0xe1>
lea    0xdd0(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    $0xffffffff,%ebp
jmp    12d6 <read_image+0x146>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1090 <readASCIIrgb@plt>
jmp    12cc <read_image+0x13c>
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%ebp
jmp    12ce <read_image+0x13e>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <readgrey@plt>
jmp    12cc <read_image+0x13c>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1080 <readRGB@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"could not open file\00"
@.str.2 = [15 x i8] c"Read %u bytes\0A\00"
@.str.3 = [32 x i8] c"could not read identifier file\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [23 x i8] c"identifier not correct\00"
@.str.6 = [24 x i8] c"Format not supported!!!\00"
define i32 @read_image(i8* %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [4 x i8]
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32
store i8* %0, i8** %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%14 = bitcast [4 x i8]* %10 to i8*
%15 = load i8*, i8** %6
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %11
%17 = load i32*, i32** %11
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %102
21: ; preds = %4
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%23 = load i32*, i32** %11
%24 = call i64 @fread(i8* %22, i32 1, i32 3, i32* %23)
store i64 %24, i64* %12
%25 = load i64, i64* %12
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %25)
%27 = load i64, i64* %12
%28 = icmp ne i64 %27, 3
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32*, i32** %11
%32 = call i32 @fclose(i32* %31)
store i32 -1, i32* %5
br label %102
33: ; preds = %21
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%36 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 80
br i1 %39, label %45, label %40
40: ; preds = %33
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 32
br i1 %44, label %45, label %49
45: ; preds = %40, %33
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32*, i32** %11
%48 = call i32 @fclose(i32* %47)
store i32 -1, i32* %5
br label %102
49: ; preds = %40
store i32 -1, i32* %13
%50 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 53
br i1 %53, label %54, label %60
54: ; preds = %49
%55 = load i32*, i32** %11
%56 = load i8**, i8*** %7
%57 = load i32*, i32** %8
%58 = load i32*, i32** %9
%59 = call i32 @readgrey(i32* %55, i8** %56, i32* %57, i32* %58)
store i32 %59, i32* %13
br label %98
60: ; preds = %49
%61 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 54
br i1 %64, label %65, label %71
65: ; preds = %60
%66 = load i32*, i32** %11
%67 = load i8**, i8*** %7
%68 = load i32*, i32** %8
%69 = load i32*, i32** %9
%70 = call i32 @readRGB(i32* %66, i8** %67, i32* %68, i32* %69)
store i32 %70, i32* %13
br label %97
71: ; preds = %60
%72 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp eq i32 %74, 50
br i1 %75, label %76, label %82
76: ; preds = %71
%77 = load i32*, i32** %11
%78 = load i8**, i8*** %7
%79 = load i32*, i32** %8
%80 = load i32*, i32** %9
%81 = call i32 @readASCIIgrey(i32* %77, i8** %78, i32* %79, i32* %80)
store i32 %81, i32* %13
br label %96
82: ; preds = %71
%83 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 1
%84 = load i8, i8* %83
%85 = sext i8 %84 to i32
%86 = icmp eq i32 %85, 51
br i1 %86, label %87, label %93
87: ; preds = %82
%88 = load i32*, i32** %11
%89 = load i8**, i8*** %7
%90 = load i32*, i32** %8
%91 = load i32*, i32** %9
%92 = call i32 @readASCIIrgb(i32* %88, i8** %89, i32* %90, i32* %91)
store i32 %92, i32* %13
br label %95
93: ; preds = %82
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %95
95: ; preds = %93, %87
br label %96
96: ; preds = %95, %76
br label %97
97: ; preds = %96, %65
br label %98
98: ; preds = %97, %54
%99 = load i32*, i32** %11
%100 = call i32 @fclose(i32* %99)
%101 = load i32, i32* %13
store i32 %101, i32* %5
br label %102
102: ; preds = %98, %45, %29, %19
%103 = load i32, i32* %5
ret i32 %103
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [20 x i8] c"could not open file\00"
@.str.2 = [15 x i8] c"Read %u bytes\0A\00"
@.str.5 = [23 x i8] c"identifier not correct\00"
@.str.6 = [24 x i8] c"Format not supported!!!\00"
@str = [31 x i8] c"could not read identifier file\00"
define i32 @read_image(i8* nocapture %0, i8** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to [4 x i8]*
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %46
12: ; preds = %4
%13 = call i64 @fread(i8* %7, i32 1, i32 3, i32* %8)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i64 %13)
%15 = icmp eq i64 %13, 3
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %8)
br label %46
19: ; preds = %12
%20 = call i32 @puts(i8* %7)
%21 = load i8, i8* %7
%22 = icmp ne i8 %21, 80
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%24 = load i8, i8* %23
%25 = icmp ne i8 %24, 32
%26 = select i1 %22, i1 true, i1 %25
br i1 %26, label %27, label %30
27: ; preds = %19
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0))
%29 = tail call i32 @fclose(i32* %8)
br label %46
30: ; preds = %19
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%32 = load i8, i8* %31
switch i8 %32, label %41 [
i8 53, label %33
i8 54, label %35
i8 50, label %37
i8 51, label %39
]
33: ; preds = %30
%34 = tail call i32 @readgrey(i32* %8, i8** %1, i32* %2, i32* %3)
br label %43
35: ; preds = %30
%36 = tail call i32 @readRGB(i32* %8, i8** %1, i32* %2, i32* %3)
br label %43
37: ; preds = %30
%38 = tail call i32 @readASCIIgrey(i32* %8, i8** %1, i32* %2, i32* %3)
br label %43
39: ; preds = %30
%40 = tail call i32 @readASCIIrgb(i32* %8, i8** %1, i32* %2, i32* %3)
br label %43
41: ; preds = %30
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %35, %39, %41, %37, %33
%44 = phi i32 [ %34, %33 ], [ %36, %35 ], [ %38, %37 ], [ %40, %39 ], [ -1, %41 ]
%45 = tail call i32 @fclose(i32* %8)
br label %46
46: ; preds = %16, %27, %43, %10
%47 = phi i32 [ -1, %10 ], [ -1, %16 ], [ -1, %27 ], [ %44, %43 ]
ret i32 %47
}