#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
void printgrid(int m, int n) {
  int i;
  int need_space;
  for (i = 1; i <= m * n; ++i) {
    need_space = 0;
    if (i > n) {
      printf("%d", i - n);
      need_space = 1;
    }
    if (n >= 2 && i % n != 1) {
      if (need_space) {
        printf(" ");
      }
      printf("%d", i - 1);
      need_space = 1;
    }
    if (i % n != 0) {
      if (need_space) {
        printf(" ");
      }
      printf("%d", i + 1);
      need_space = 1;
    }
    if (i <= (m - 1) * n) {
      if (need_space) {
        printf(" ");
      }
      printf("%d", i + n);
      need_space = 1;
    }
    printf("\n");
  }
}


x86 assembly (clang, -O0):
<printgrid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x4(%rbp),%ecx
imul   -0x8(%rbp),%ecx
cmp    %ecx,%eax
jg     1259 <printgrid+0x149>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    1165 <printgrid+0x55>
mov    -0xc(%rbp),%esi
sub    -0x8(%rbp),%esi
lea    0xea9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11b2 <printgrid+0xa2>
mov    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
cmp    $0x1,%edx
je     11b2 <printgrid+0xa2>
cmpl   $0x0,-0x10(%rbp)
je     1197 <printgrid+0x87>
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x8(%rbp)
cmp    $0x0,%edx
je     11f5 <printgrid+0xe5>
cmpl   $0x0,-0x10(%rbp)
je     11da <printgrid+0xca>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
add    $0x1,%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
imul   -0x8(%rbp),%ecx
cmp    %ecx,%eax
jg     123d <printgrid+0x12d>
cmpl   $0x0,-0x10(%rbp)
je     1222 <printgrid+0x112>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%esi
add    -0x8(%rbp),%esi
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x10(%rbp)
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1125 <printgrid+0x15>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printgrid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r13d
imul   %edi,%r13d
test   %r13d,%r13d
jle    1245 <printgrid+0x125>
mov    %esi,%r12d
mov    %edi,%r15d
add    $0xffffffff,%r15d
imul   %esi,%r15d
neg    %r13d
mov    %esi,%eax
neg    %eax
mov    %rax,(%rsp)
mov    $0x1,%ebp
lea    0xea0(%rip),%r14
jmp    119d <printgrid+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r12,%rbp,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x1(%r13,%rbp,1),%eax
mov    %ebp,%ecx
add    $0x1,%ecx
mov    %ecx,%ebp
cmp    $0x1,%eax
je     1245 <printgrid+0x125>
mov    $0x0,%ecx
cmp    %r12d,%ebp
jle    11bd <printgrid+0x9d>
mov    (%rsp),%rax
lea    (%rax,%rbp,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ecx
mov    %ebp,%eax
cltd
idiv   %r12d
mov    %edx,%ebx
cmp    $0x2,%r12d
jl     1200 <printgrid+0xe0>
cmp    $0x1,%ebx
je     1204 <printgrid+0xe4>
cmp    %r12d,%ebp
jle    11df <printgrid+0xbf>
mov    $0x20,%edi
call   1030 <putchar@plt>
lea    -0x1(%rbp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jne    1209 <printgrid+0xe9>
jmp    1220 <printgrid+0x100>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
je     1225 <printgrid+0x105>
cmp    %r12d,%ebp
jle    1213 <printgrid+0xf3>
mov    $0x20,%edi
call   1030 <putchar@plt>
lea    0x1(%rbp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ecx
cmp    %r15d,%ebp
jg     117e <printgrid+0x5e>
test   %ecx,%ecx
je     1170 <printgrid+0x50>
mov    $0x20,%edi
call   1030 <putchar@plt>
jmp    1170 <printgrid+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
define void @printgrid(i32 %0, i32 %1) {
%3 = mul i32 %1, %0
%4 = icmp slt i32 %3, 1
br i1 %4, label %49, label %5
5: ; preds = %2
%6 = icmp sgt i32 %1, 1
%7 = add nsw i32 %0, -1
%8 = mul nsw i32 %7, %1
br label %9
9: ; preds = %5, %45
%10 = phi i32 [ 1, %5 ], [ %47, %45 ]
%11 = icmp sgt i32 %10, %1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = sub nsw i32 %10, %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %9
%16 = phi i32 [ 1, %12 ], [ 0, %9 ]
%17 = srem i32 %10, %1
br i1 %6, label %18, label %23
18: ; preds = %15
%19 = icmp eq i32 %17, 1
br i1 %19, label %29, label %20
20: ; preds = %18
br i1 %11, label %21, label %25
21: ; preds = %20
%22 = tail call i32 @putchar(i32 32)
br label %25
23: ; preds = %15
%24 = icmp eq i32 %17, 0
br i1 %24, label %35, label %29
25: ; preds = %20, %21
%26 = add nsw i32 %10, -1
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = icmp eq i32 %17, 0
br i1 %28, label %35, label %30
29: ; preds = %18, %23
br i1 %11, label %30, label %32
30: ; preds = %25, %29
%31 = tail call i32 @putchar(i32 32)
br label %32
32: ; preds = %30, %29
%33 = add nuw nsw i32 %10, 1
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %25, %32, %23
%36 = phi i32 [ 1, %32 ], [ %16, %23 ], [ 1, %25 ]
%37 = icmp sgt i32 %10, %8
br i1 %37, label %45, label %38
38: ; preds = %35
%39 = icmp eq i32 %36, 0
br i1 %39, label %42, label %40
40: ; preds = %38
%41 = tail call i32 @putchar(i32 32)
br label %42
42: ; preds = %40, %38
%43 = add nsw i32 %10, %1
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %42, %35
%46 = tail call i32 @putchar(i32 10)
%47 = add nuw i32 %10, 1
%48 = icmp eq i32 %10, %3
br i1 %48, label %49, label %9
49: ; preds = %45, %2
ret void
}