#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(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, ...);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
void loadImage(char *filename, unsigned char **dest, int *width, int *height) {
  char buffer[200];
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    printf("Error opening file %s.", filename);
    exit(1);
  }
  fgets(buffer, 200, file);
  fscanf(file, "%d %d\n", width, height);
  fgets(buffer, 200, file);
  *dest =
      (unsigned char *)malloc((*width) * (*height) * sizeof(unsigned char) * 3);
  int pixels_read = 0;
  int pixel = 0;
  int curr_component = 0;
  int i;
  for (i = 0; i < ((*width) * (*height) * 3); i++) {
    int v;
    int got = fscanf(file, "%d", &v);
    if (got == 0) {
      printf("Unexpected end of file after reading %d color values", i);
      exit(1);
    }
    (*dest)[i] = (unsigned char)v;
  }
  fclose(file);
}


x86 assembly (clang, -O0):
<loadImage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0xf8(%rbp)
cmpq   $0x0,-0xf8(%rbp)
jne    11cc <loadImage+0x5c>
mov    -0x8(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    -0xf0(%rbp),%rdi
mov    -0xf8(%rbp),%rdx
mov    $0xc8,%esi
call   1060 <fgets@plt>
mov    -0xf8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
lea    -0xf0(%rbp),%rdi
mov    -0xf8(%rbp),%rdx
mov    $0xc8,%esi
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
imul   (%rcx),%eax
cltq
shl    $0x0,%rax
imul   $0x3,%rax,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0xfc(%rbp)
movl   $0x0,-0x100(%rbp)
movl   $0x0,-0x104(%rbp)
movl   $0x0,-0x108(%rbp)
mov    -0x108(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
mov    -0x20(%rbp),%rdx
imul   (%rdx),%ecx
imul   $0x3,%ecx,%ecx
cmp    %ecx,%eax
jge    1301 <loadImage+0x191>
mov    -0xf8(%rbp),%rdi
lea    0xd8b(%rip),%rsi
lea    -0x10c(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    %eax,-0x110(%rbp)
cmpl   $0x0,-0x110(%rbp)
jne    12d4 <loadImage+0x164>
mov    -0x108(%rbp),%esi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movslq -0x108(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x108(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x108(%rbp)
jmp    1269 <loadImage+0xf9>
mov    -0xf8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadImage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xd8,%rsp
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     127d <loadImage+0x10d>
mov    %rax,%r13
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
lea    0xe58(%rip),%rsi
mov    %r13,%rdi
mov    %r12,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %r13,%rdx
call   1060 <fgets@plt>
mov    (%r12),%eax
imul   (%r14),%eax
lea    (%rax,%rax,2),%edi
call   1070 <malloc@plt>
mov    %rax,(%r15)
mov    (%r12),%eax
imul   (%r14),%eax
lea    (%rax,%rax,2),%eax
test   %eax,%eax
jle    1249 <loadImage+0xd9>
lea    0xc(%rsp),%rbp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
lea    0xe06(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1050 <fscanf@plt>
test   %eax,%eax
je     1263 <loadImage+0xf3>
movzbl 0xc(%rsp),%eax
mov    (%r15),%rcx
mov    %al,(%rcx,%rbx,1)
mov    (%r12),%eax
imul   (%r14),%eax
add    $0x1,%rbx
lea    (%rax,%rax,2),%eax
cltq
cmp    %rax,%rbx
jl     1210 <loadImage+0xa0>
mov    %r13,%rdi
call   1030 <fclose@plt>
add    $0xd8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd7e(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [23 x i8] c"Error opening file %s.\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [53 x i8] c"Unexpected end of file after reading %d color values\00"
define void @loadImage(i8* %0, i8** nocapture %1, i32* %2, i32* %3) {
%5 = alloca [200 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %4
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %4
%14 = call i32 @fgets(i8* %7, i32 200, i32* %8)
%15 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %2, i32* %3)
%16 = call i32 @fgets(i8* %7, i32 200, i32* %8)
%17 = load i32, i32* %2
%18 = load i32, i32* %3
%19 = mul i32 %17, 3
%20 = mul i32 %19, %18
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %1
%23 = load i32, i32* %2
%24 = load i32, i32* %3
%25 = mul i32 %23, 3
%26 = mul i32 %25, %24
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %50
28: ; preds = %13
%29 = bitcast i32* %6 to i8*
br label %30
30: ; preds = %28, %38
%31 = phi i64 [ 0, %28 ], [ %43, %38 ]
%32 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = trunc i64 %31 to i32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %30
%39 = load i32, i32* %6
%40 = trunc i32 %39 to i8
%41 = load i8*, i8** %1
%42 = getelementptr inbounds i8, i8* %41, i64 %31
store i8 %40, i8* %42
%43 = add nuw nsw i64 %31, 1
%44 = load i32, i32* %2
%45 = load i32, i32* %3
%46 = mul i32 %44, 3
%47 = mul i32 %46, %45
%48 = sext i32 %47 to i64
%49 = icmp slt i64 %43, %48
br i1 %49, label %30, label %50
50: ; preds = %38, %13
%51 = call i32 @fclose(i32* %8)
ret void
}