#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
unsigned char *fetch_image(const char *fname, int w, int h) {
  char *img = NULL;
  int img_fd = open(fname, O_RDONLY);
  ssize_t n;
  extern int verbose;
  if (img_fd == -1)
    return NULL;
  img = (unsigned char *)malloc(sizeof(unsigned char) * w * h);
  if (!img)
    return NULL;
  n = read(img_fd, img, w * h);
  close(img_fd);
  if (n < (w * h)) {
    return NULL;
  }
  if (verbose)
    fprintf(stderr, "Read %d by %d bitmap; %d bytes.\n", w, h, n);
  return img;
}


x86 assembly (clang, -O0):
<fetch_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1196 <fetch_image+0x46>
movq   $0x0,-0x8(%rbp)
jmp    123d <fetch_image+0xed>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
movslq -0x18(%rbp),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c9 <fetch_image+0x79>
movq   $0x0,-0x8(%rbp)
jmp    123d <fetch_image+0xed>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
imul   -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x18(%rbp),%ecx
cmp    %ecx,%eax
jge    1206 <fetch_image+0xb6>
movq   $0x0,-0x8(%rbp)
jmp    123d <fetch_image+0xed>
mov    0x2ddb(%rip),%rax
cmpl   $0x0,(%rax)
je     1235 <fetch_image+0xe5>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%r8d
lea    0xdd0(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetch_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11a7 <fetch_image+0x57>
mov    %eax,%ebp
mov    %r14d,%ebx
imul   %r15d,%ebx
mov    %ebx,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11a7 <fetch_image+0x57>
mov    %rax,%r12
mov    %ebp,%edi
mov    %rax,%rsi
mov    %ebx,%edx
call   1040 <read@plt>
mov    %eax,%r13d
mov    %ebp,%edi
call   1030 <close@plt>
cmp    %ebx,%r13d
jge    11bc <fetch_image+0x6c>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e25(%rip),%rax
cmpl   $0x0,(%rax)
je     11aa <fetch_image+0x5a>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
mov    %r13d,%r8d
call   1050 <fprintf@plt>
jmp    11aa <fetch_image+0x5a>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@verbose = external global i32
@stderr = global i32 0
@.str = [33 x i8] c"Read %d by %d bitmap; %d bytes.\0A\00"
define i8* @fetch_image(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* null, i8** %8
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %3
store i8* null, i8** %4
br label %56
17: ; preds = %3
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = mul i64 1, %19
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = mul i64 %20, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %17
store i8* null, i8** %4
br label %56
30: ; preds = %17
%31 = load i32, i32* %9
%32 = load i8*, i8** %8
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = mul nsw i32 %33, %34
%36 = call i32 @read(i32 %31, i8* %32, i32 %35)
store i32 %36, i32* %10
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %10
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = mul nsw i32 %40, %41
%43 = icmp slt i32 %39, %42
br i1 %43, label %44, label %45
44: ; preds = %30
store i8* null, i8** %4
br label %56
45: ; preds = %30
%46 = load i32, i32* @verbose
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = load i32, i32* %6
%51 = load i32, i32* %7
%52 = load i32, i32* %10
%53 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %50, i32 %51, i32 %52)
br label %54
54: ; preds = %48, %45
%55 = load i8*, i8** %8
store i8* %55, i8** %4
br label %56
56: ; preds = %54, %44, %29, %16
%57 = load i8*, i8** %4
ret i8* %57
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@verbose = external global i32
@stderr = global i32 0
@.str = [33 x i8] c"Read %d by %d bitmap; %d bytes.\0A\00"
define i8* @fetch_image(i8* nocapture %0, i32 %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %22, label %7
7: ; preds = %3
%8 = mul i32 %2, %1
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = icmp eq i64 %9, 0
br i1 %11, label %22, label %12
12: ; preds = %7
%13 = tail call i32 @read(i32 %5, i8* %10, i32 %8)
%14 = tail call i32 @close(i32 %5)
%15 = icmp slt i32 %13, %8
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = load i32, i32* @verbose
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2, i32 %13)
br label %22
22: ; preds = %16, %19, %12, %7, %3
%23 = phi i8* [ null, %3 ], [ null, %7 ], [ null, %12 ], [ %10, %19 ], [ %10, %16 ]
ret i8* %23
}