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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int atoi(char *);
int /*<<< orphan*/ calc_gradients_whole_pic(int *, int *, int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(int *);
int *generate_random_image(int, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("Usage: ./gradient <width> <height>\n");
    exit(1);
  }
  int width = atoi(argv[1]);
  int height = atoi(argv[2]);
  assert(width > 0 && width <= 4096);
  assert(height > 0 && height <= 4096);
  printf("simulated image size: %dx%d\n", width, height);
  uint8_t *image = generate_random_image(width, height);
  if (!image) {
    printf("generate image failed\n");
    goto failed;
  }
  int *gradient = (int *)malloc(width * height * sizeof(int));
  if (!gradient) {
    printf("malloc gradient array failed\n");
    goto failed;
  }
  calc_gradients_whole_pic(gradient, image, width, height);
  free(image);
  image = NULL;
  free(gradient);
  gradient = NULL;
failed:
  if (image) {
    free(image);
  }
  if (gradient) {
    free(gradient);
  }
  return -1;
}


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)
cmpl   $0x3,-0x8(%rbp)
je     11b8 <main+0x38>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x29(%rbp)
jle    11f4 <main+0x74>
cmpl   $0x1000,-0x14(%rbp)
setle  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x2a(%rbp)
jle    121d <main+0x9d>
cmpl   $0x1000,-0x18(%rbp)
setle  %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <generate_random_image@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    126b <main+0xeb>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d6 <main+0x156>
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a1 <main+0x121>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12d6 <main+0x156>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1060 <calc_gradients_whole_pic@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12ea <main+0x16a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x28(%rbp)
je     12fe <main+0x17e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    126d <main+0xdd>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
lea    -0x1(%r14),%eax
xor    %edi,%edi
cmp    $0x1000,%eax
setb   %dil
call   1060 <assert@plt>
lea    -0x1(%rbx),%eax
xor    %edi,%edi
cmp    $0x1000,%eax
setb   %dil
call   1060 <assert@plt>
lea    0xe17(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
mov    %ebx,%esi
call   1090 <generate_random_image@plt>
test   %rax,%rax
je     123a <main+0xaa>
mov    %rax,%r15
mov    %r14d,%edi
imul   %ebx,%edi
shl    $0x2,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1248 <main+0xb8>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
call   1070 <calc_gradients_whole_pic@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
jmp    1257 <main+0xc7>
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
jmp    125c <main+0xcc>
lea    0xde4(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Usage: ./gradient <width> <height>\0A\00"
@.str.1 = [29 x i8] c"simulated image size: %dx%d\0A\00"
@.str.2 = [23 x i8] c"generate image failed\0A\00"
@.str.3 = [30 x i8] c"malloc gradient array failed\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %6
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %7
%24 = load i32, i32* %6
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = load i32, i32* %6
%28 = icmp sle i32 %27, 4096
br label %29
29: ; preds = %26, %15
%30 = phi i1 [ false, %15 ], [ %28, %26 ]
%31 = zext i1 %30 to i32
%32 = call i32 @assert(i32 %31)
%33 = load i32, i32* %7
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* %7
%37 = icmp sle i32 %36, 4096
br label %38
38: ; preds = %35, %29
%39 = phi i1 [ false, %29 ], [ %37, %35 ]
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %42, i32 %43)
%45 = load i32, i32* %6
%46 = load i32, i32* %7
%47 = call i32* @generate_random_image(i32 %45, i32 %46)
store i32* %47, i32** %8
%48 = load i32*, i32** %8
%49 = icmp ne i32* %48, null
br i1 %49, label %52, label %50
50: ; preds = %38
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %75
52: ; preds = %38
%53 = load i32, i32* %6
%54 = load i32, i32* %7
%55 = mul nsw i32 %53, %54
%56 = sext i32 %55 to i64
%57 = mul i64 %56, 4
%58 = trunc i64 %57 to i32
%59 = call i64 @malloc(i32 %58)
%60 = inttoptr i64 %59 to i32*
store i32* %60, i32** %9
%61 = load i32*, i32** %9
%62 = icmp ne i32* %61, null
br i1 %62, label %65, label %63
63: ; preds = %52
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
br label %75
65: ; preds = %52
%66 = load i32*, i32** %9
%67 = load i32*, i32** %8
%68 = load i32, i32* %6
%69 = load i32, i32* %7
%70 = call i32 @calc_gradients_whole_pic(i32* %66, i32* %67, i32 %68, i32 %69)
%71 = load i32*, i32** %8
%72 = call i32 @free(i32* %71)
store i32* null, i32** %8
%73 = load i32*, i32** %9
%74 = call i32 @free(i32* %73)
store i32* null, i32** %9
br label %75
75: ; preds = %65, %63, %50
%76 = load i32*, i32** %8
%77 = icmp ne i32* %76, null
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32*, i32** %8
%80 = call i32 @free(i32* %79)
br label %81
81: ; preds = %78, %75
%82 = load i32*, i32** %9
%83 = icmp ne i32* %82, null
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = load i32*, i32** %9
%86 = call i32 @free(i32* %85)
br label %87
87: ; preds = %84, %81
ret i32 -1
}

LLVM IR (-O2):
@.str.1 = [29 x i8] c"simulated image size: %dx%d\0A\00"
@str = [22 x i8] c"generate image failed\00"
@str.4 = [29 x i8] c"malloc gradient array failed\00"
@str.5 = [35 x i8] c"Usage: ./gradient <width> <height>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.5, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = add i32 %10, -1
%15 = icmp ult i32 %14, 4096
%16 = zext i1 %15 to i32
%17 = tail call i32 @assert(i32 %16)
%18 = add i32 %13, -1
%19 = icmp ult i32 %18, 4096
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %13)
%23 = tail call i32* @generate_random_image(i32 %10, i32 %13)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %27
25: ; preds = %7
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %40
27: ; preds = %7
%28 = shl i32 %10, 2
%29 = mul i32 %28, %13
%30 = tail call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i32*
%32 = icmp eq i64 %30, 0
br i1 %32, label %37, label %33
33: ; preds = %27
%34 = tail call i32 @calc_gradients_whole_pic(i32* %31, i32* %23, i32 %10, i32 %13)
%35 = tail call i32 @free(i32* %23)
%36 = tail call i32 @free(i32* %31)
br label %40
37: ; preds = %27
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.4, i64 0, i64 0))
%39 = tail call i32 @free(i32* %23)
br label %40
40: ; preds = %25, %33, %37
ret i32 -1
}