#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 atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printdiaggrid(int, int);
int /*<<< orphan*/ printgrid(int, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  int i, m = -1, n = -1, x;
  int diagonal = 0;
  if (argc < 2 || argc > 4) {
    fprintf(stderr,
            "Usage: makegrid [-d] m [n]\n"
            "  Output an m x n (or m x m) grid. \n"
            "  If '-d' is given, the order of edges becomes diagonal.\n");
    exit(1);
  }
  for (i = 1; i < argc; ++i) {
    if (strcmp(argv[i], "-d") == 0) {
      diagonal = 1;
    } else {
      x = atoi(argv[i]);
      if (x <= 0) {
        fprintf(stderr, "Please input a positive integer.\n");
        exit(1);
      }
      if (m < 0) {
        m = x;
      } else if (n < 0) {
        n = x;
      }
    }
  }
  if (n < 0) {
    n = m;
  }
  if (diagonal) {
    printdiaggrid(m, n);
  } else {
    printgrid(m, n);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     119f <main+0x3f>
cmpl   $0x4,-0x8(%rbp)
jle    11be <main+0x5e>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    127e <main+0x11e>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe95(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%rax
jne    11ff <main+0x9f>
movl   $0x1,-0x24(%rbp)
jmp    126b <main+0x10b>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     123c <main+0xdc>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
jge    1251 <main+0xf1>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1266 <main+0x106>
cmpl   $0x0,-0x1c(%rbp)
jge    1261 <main+0x101>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1266 <main+0x106>
jmp    126b <main+0x10b>
jmp    1270 <main+0x110>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c5 <main+0x65>
cmpl   $0x0,-0x1c(%rbp)
jge    128e <main+0x12e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a8 <main+0x148>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1080 <printdiaggrid@plt>
jmp    12b3 <main+0x153>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1050 <printgrid@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
jb     1238 <main+0xd8>
mov    %rsi,%rbx
mov    %edi,%r12d
mov    $0xffffffff,%ebp
movl   $0x0,0x4(%rsp)
mov    $0x1,%r13d
lea    0xee2(%rip),%r15
mov    $0xffffffff,%r14d
jmp    11b1 <main+0x51>
nop
movl   $0x1,0x4(%rsp)
add    $0x1,%r13
cmp    %r13,%r12
je     11e5 <main+0x85>
mov    (%rbx,%r13,8),%rdi
mov    %r15,%rsi
call   1030 <strcmp@plt>
test   %rax,%rax
je     11a0 <main+0x40>
mov    (%rbx,%r13,8),%rdi
call   1060 <atoi@plt>
test   %eax,%eax
jle    1219 <main+0xb9>
test   %r14d,%r14d
js     11e0 <main+0x80>
test   %ebp,%ebp
cmovs  %eax,%ebp
jmp    11a8 <main+0x48>
nopl   0x0(%rax,%rax,1)
mov    %eax,%r14d
jmp    11a8 <main+0x48>
test   %ebp,%ebp
cmovs  %r14d,%ebp
cmpl   $0x0,0x4(%rsp)
je     11fe <main+0x9e>
mov    %r14d,%edi
mov    %ebp,%esi
call   1080 <printdiaggrid@plt>
jmp    1208 <main+0xa8>
mov    %r14d,%edi
mov    %ebp,%esi
call   1050 <printgrid@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
jmp    1229 <main+0xc9>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [121 x i8] c"Usage: makegrid [-d] m [n]\0A Output an m x n (or m x m) grid. \0A If '-d' is given, the order of edges becomes diagonal.\0A\00"
@.str.1 = [3 x i8] c"-d\00"
@.str.2 = [34 x i8] c"Please input a positive integer.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %7
store i32 -1, i32* %8
store i32 0, i32* %10
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 4
br i1 %15, label %16, label %20
16: ; preds = %13, %2
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([121 x i8], [121 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
store i32 1, i32* %6
br label %21
21: ; preds = %60, %20
%22 = load i32, i32* %6
%23 = load i32, i32* %4
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %63
25: ; preds = %21
%26 = load i8**, i8*** %5
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8*, i8** %26, i64 %28
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 1, i32* %10
br label %59
34: ; preds = %25
%35 = load i8**, i8*** %5
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8*, i8** %35, i64 %37
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %34
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %34
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = load i32, i32* %9
store i32 %51, i32* %7
br label %58
52: ; preds = %47
%53 = load i32, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = load i32, i32* %9
store i32 %56, i32* %8
br label %57
57: ; preds = %55, %52
br label %58
58: ; preds = %57, %50
br label %59
59: ; preds = %58, %33
br label %60
60: ; preds = %59
%61 = load i32, i32* %6
%62 = add nsw i32 %61, 1
store i32 %62, i32* %6
br label %21
63: ; preds = %21
%64 = load i32, i32* %8
%65 = icmp slt i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %63
%67 = load i32, i32* %7
store i32 %67, i32* %8
br label %68
68: ; preds = %66, %63
%69 = load i32, i32* %10
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %68
%72 = load i32, i32* %7
%73 = load i32, i32* %8
%74 = call i32 @printdiaggrid(i32 %72, i32 %73)
br label %79
75: ; preds = %68
%76 = load i32, i32* %7
%77 = load i32, i32* %8
%78 = call i32 @printgrid(i32 %76, i32 %77)
br label %79
79: ; preds = %75, %71
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [121 x i8] c"Usage: makegrid [-d] m [n]\0A Output an m x n (or m x m) grid. \0A If '-d' is given, the order of edges becomes diagonal.\0A\00"
@.str.1 = [3 x i8] c"-d\00"
@.str.2 = [34 x i8] c"Please input a positive integer.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -3
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = zext i32 %0 to i64
br label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([121 x i8], [121 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %5, %33
%12 = phi i64 [ 1, %5 ], [ %37, %33 ]
%13 = phi i32 [ 0, %5 ], [ %36, %33 ]
%14 = phi i32 [ -1, %5 ], [ %35, %33 ]
%15 = phi i32 [ -1, %5 ], [ %34, %33 ]
%16 = getelementptr inbounds i8*, i8** %1, i64 %12
%17 = load i8*, i8** %16
%18 = tail call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %11
%21 = load i8*, i8** %16
%22 = tail call i32 @atoi(i8* %21)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = icmp slt i32 %15, 0
br i1 %29, label %33, label %30
30: ; preds = %28
%31 = icmp slt i32 %14, 0
%32 = select i1 %31, i32 %22, i32 %14
br label %33
33: ; preds = %30, %28, %11
%34 = phi i32 [ %15, %11 ], [ %22, %28 ], [ %15, %30 ]
%35 = phi i32 [ %14, %11 ], [ %14, %28 ], [ %32, %30 ]
%36 = phi i32 [ 1, %11 ], [ %13, %28 ], [ %13, %30 ]
%37 = add nuw nsw i64 %12, 1
%38 = icmp eq i64 %37, %6
br i1 %38, label %39, label %11
39: ; preds = %33
%40 = icmp slt i32 %35, 0
%41 = select i1 %40, i32 %34, i32 %35
%42 = icmp eq i32 %36, 0
br i1 %42, label %45, label %43
43: ; preds = %39
%44 = tail call i32 @printdiaggrid(i32 %34, i32 %41)
br label %47
45: ; preds = %39
%46 = tail call i32 @printgrid(i32 %34, i32 %41)
br label %47
47: ; preds = %45, %43
ret i32 0
}