#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int random();
uint8_t *generate_random_image(int width, int height) {
  uint8_t *image = (uint8_t *)malloc(width * height);
  if (!image) {
    printf("malloc memory for image failed\n");
    return NULL;
  }
  int i, j;
  for (j = 0; j < height; j++) {
    for (i = 0; i < width; i++) {
      image[j * width + i] = random() % 256;
    }
  }
  return image;
}


x86 assembly (clang, -O0):
<generate_random_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%edi
imul   -0x10(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1172 <generate_random_image+0x42>
lea    0xea0(%rip),%rdi
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <generate_random_image+0xb4>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11dc <generate_random_image+0xac>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c9 <generate_random_image+0x99>
mov    $0x0,%al
call   1040 <random@plt>
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0xc(%rbp),%ecx
add    -0x1c(%rbp),%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118c <generate_random_image+0x5c>
jmp    11ce <generate_random_image+0x9e>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1179 <generate_random_image+0x49>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_random_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r15d
mov    %edi,%eax
mov    %esi,%edi
mov    %eax,0xc(%rsp)
imul   %eax,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11c9 <generate_random_image+0x99>
test   %r15d,%r15d
jle    11de <generate_random_image+0xae>
movslq 0xc(%rsp),%r13
mov    %r15d,%r15d
mov    %r13d,%ebp
shl    $0x2,%r13
mov    %rax,0x10(%rsp)
mov    %rax,%rbx
xor    %r14d,%r14d
jmp    118c <generate_random_image+0x5c>
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
add    %r13,%rbx
cmp    %r15,%r14
je     11d9 <generate_random_image+0xa9>
cmpl   $0x0,0xc(%rsp)
jle    1180 <generate_random_image+0x50>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <random@plt>
lea    0xff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xffffff00,%ecx
sub    %ecx,%eax
mov    %eax,(%rbx,%r12,4)
add    $0x1,%r12
cmp    %r12,%rbp
jne    11a0 <generate_random_image+0x70>
jmp    1180 <generate_random_image+0x50>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11de <generate_random_image+0xae>
mov    0x10(%rsp),%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"malloc memory for image failed\0A\00"
define i32* @generate_random_image(i32 %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = mul nsw i32 %9, %10
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i32*
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %3
br label %48
18: ; preds = %2
store i32 0, i32* %8
br label %19
19: ; preds = %43, %18
%20 = load i32, i32* %8
%21 = load i32, i32* %5
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %46
23: ; preds = %19
store i32 0, i32* %7
br label %24
24: ; preds = %39, %23
%25 = load i32, i32* %7
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %42
28: ; preds = %24
%29 = call i32 (...) @random()
%30 = srem i32 %29, 256
%31 = load i32*, i32** %6
%32 = load i32, i32* %8
%33 = load i32, i32* %4
%34 = mul nsw i32 %32, %33
%35 = load i32, i32* %7
%36 = add nsw i32 %34, %35
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32, i32* %31, i64 %37
store i32 %30, i32* %38
br label %39
39: ; preds = %28
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %24
42: ; preds = %24
br label %43
43: ; preds = %42
%44 = load i32, i32* %8
%45 = add nsw i32 %44, 1
store i32 %45, i32* %8
br label %19
46: ; preds = %19
%47 = load i32*, i32** %6
store i32* %47, i32** %3
br label %48
48: ; preds = %46, %16
%49 = load i32*, i32** %3
ret i32* %49
}

LLVM IR (-O2):
@str = [31 x i8] c"malloc memory for image failed\00"
define i32* @generate_random_image(i32 %0, i32 %1) {
%3 = mul nsw i32 %1, %0
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i32*
%6 = icmp eq i64 %4, 0
br i1 %6, label %14, label %7
7: ; preds = %2
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %31
9: ; preds = %7
%10 = icmp sgt i32 %0, 0
%11 = sext i32 %0 to i64
%12 = zext i32 %1 to i64
%13 = zext i32 %0 to i64
br label %16
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %31
16: ; preds = %9, %28
%17 = phi i64 [ 0, %9 ], [ %29, %28 ]
br i1 %10, label %18, label %28
18: ; preds = %16
%19 = mul nsw i64 %17, %11
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %26, %20 ]
%22 = tail call i32 (...) @random()
%23 = srem i32 %22, 256
%24 = add nsw i64 %21, %19
%25 = getelementptr inbounds i32, i32* %5, i64 %24
store i32 %23, i32* %25
%26 = add nuw nsw i64 %21, 1
%27 = icmp eq i64 %26, %13
br i1 %27, label %28, label %20
28: ; preds = %20, %16
%29 = add nuw nsw i64 %17, 1
%30 = icmp eq i64 %29, %12
br i1 %30, label %31, label %16
31: ; preds = %28, %7, %14
%32 = phi i32* [ null, %14 ], [ %5, %7 ], [ %5, %28 ]
ret i32* %32
}