#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 u_int32_t;
typedef int u_char;

/* Variables and functions */
int /*<<< orphan*/ EX_IOERR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void print_next_image(int f, u_int32_t n, u_char *image, u_int32_t nr,
                      u_int32_t nc) {
  int c, ilen, i, j;
  ilen = nr * nc;
  c = read(f, image, ilen);
  if (c < 0) {
    fprintf(stderr, "read image %u: %s\n", n, strerror(errno));
    exit(EX_IOERR);
  }
  for (i = 0; i < nr; i++) {
    for (j = 0; j < nc; j++) {
      printf(" %u", *(image + j + (i * nc)));
    }
  }
  printf("\n");
  return;
}


x86 assembly (clang, -O0):
<print_next_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d2 <print_next_image+0x82>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xe41(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1243 <print_next_image+0xf3>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1230 <print_next_image+0xe0>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    -0x24(%rbp),%ecx
imul   -0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ec <print_next_image+0x9c>
jmp    1235 <print_next_image+0xe5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d9 <print_next_image+0x89>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_next_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %r8d,0xc(%rsp)
mov    %r8d,%edx
imul   %ecx,%edx
mov    %rbx,%rsi
call   1050 <read@plt>
test   %eax,%eax
js     1201 <print_next_image+0xa1>
test   %ebp,%ebp
jle    11e9 <print_next_image+0x89>
movslq 0xc(%rsp),%r15
mov    %ebp,%eax
mov    %rax,0x10(%rsp)
mov    %r15d,%ebp
shl    $0x2,%r15
lea    0xe68(%rip),%r13
xor    %r14d,%r14d
jmp    11be <print_next_image+0x5e>
add    $0x1,%r14
add    %r15,%rbx
cmp    0x10(%rsp),%r14
je     11e9 <print_next_image+0x89>
cmpl   $0x0,0xc(%rsp)
jle    11b0 <print_next_image+0x50>
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%r12,4),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    11d0 <print_next_image+0x70>
jmp    11b0 <print_next_image+0x50>
mov    $0xa,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%ebx
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde1(%rip),%rsi
mov    %ebx,%edi
mov    %r15d,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"read image %u: %s\0A\00"
@errno = global i32 0
@EX_IOERR = global i32 0
@.str.1 = [4 x i8] c" %u\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @print_next_image(i32 %0, i32 %1, i32* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%15 = load i32, i32* %9
%16 = load i32, i32* %10
%17 = mul nsw i32 %15, %16
store i32 %17, i32* %12
%18 = load i32, i32* %6
%19 = load i32*, i32** %8
%20 = load i32, i32* %12
%21 = call i32 @read(i32 %18, i32* %19, i32 %20)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %5
%25 = load i32, i32* @stderr
%26 = load i32, i32* %7
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %26, i8* %28)
%30 = load i32, i32* @EX_IOERR
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %5
store i32 0, i32* %13
br label %33
33: ; preds = %58, %32
%34 = load i32, i32* %13
%35 = load i32, i32* %9
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %61
37: ; preds = %33
store i32 0, i32* %14
br label %38
38: ; preds = %54, %37
%39 = load i32, i32* %14
%40 = load i32, i32* %10
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %57
42: ; preds = %38
%43 = load i32*, i32** %8
%44 = load i32, i32* %14
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %13
%48 = load i32, i32* %10
%49 = mul nsw i32 %47, %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i32, i32* %46, i64 %50
%52 = load i32, i32* %51
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %42
%55 = load i32, i32* %14
%56 = add nsw i32 %55, 1
store i32 %56, i32* %14
br label %38
57: ; preds = %38
br label %58
58: ; preds = %57
%59 = load i32, i32* %13
%60 = add nsw i32 %59, 1
store i32 %60, i32* %13
br label %33
61: ; preds = %33
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"read image %u: %s\0A\00"
@errno = global i32 0
@EX_IOERR = global i32 0
@.str.1 = [4 x i8] c" %u\00"
define void @print_next_image(i32 %0, i32 %1, i32* nocapture %2, i32 %3, i32 %4) {
%6 = mul nsw i32 %4, %3
%7 = tail call i32 @read(i32 %0, i32* %2, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %16, label %9
9: ; preds = %5
%10 = icmp sgt i32 %3, 0
br i1 %10, label %11, label %38
11: ; preds = %9
%12 = icmp sgt i32 %4, 0
%13 = sext i32 %4 to i64
%14 = zext i32 %3 to i64
%15 = zext i32 %4 to i64
br label %23
16: ; preds = %5
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %1, i8* %19)
%21 = load i32, i32* @EX_IOERR
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %11, %35
%24 = phi i64 [ 0, %11 ], [ %36, %35 ]
br i1 %12, label %25, label %35
25: ; preds = %23
%26 = mul nsw i64 %24, %13
%27 = getelementptr inbounds i32, i32* %2, i64 %26
br label %28
28: ; preds = %25, %28
%29 = phi i64 [ 0, %25 ], [ %33, %28 ]
%30 = getelementptr inbounds i32, i32* %27, i64 %29
%31 = load i32, i32* %30
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = add nuw nsw i64 %29, 1
%34 = icmp eq i64 %33, %15
br i1 %34, label %35, label %28
35: ; preds = %28, %23
%36 = add nuw nsw i64 %24, 1
%37 = icmp eq i64 %36, %14
br i1 %37, label %38, label %23
38: ; preds = %35, %9
%39 = tail call i32 @putchar(i32 10)
ret void
}