#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 /*<<< orphan*/ uint8_t;
struct timespec {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_MONOTONIC;
int atoi(char *);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int rand();
int /*<<< orphan*/ setpx(int /*<<< orphan*/ *, int, int, int, int);
int size(int, int);
int /*<<< orphan*/ srand(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  struct timespec now;
  uint8_t *img;
  int w, h;
  int sz;
  int x, y;
  if (argc != 3) {
    fprintf(stderr, "Usage: %s width height\n", argv[0]);
    return 1;
  }
  clock_gettime(CLOCK_MONOTONIC, &now);
  srand((int)now.tv_sec ^ (int)now.tv_nsec);
  w = atoi(argv[1]);
  h = atoi(argv[2]);
  sz = size(w, h);
  fprintf(stderr, "Image size: %dx%d  Array size: %d\n", w, h, sz);
  img = malloc(sz);
  if (img == NULL) {
    perror("Failed to allocate image");
    return 2;
  }
  memset(img, 0, sz);
  fprintf(stderr, "Allocated image at %p\n", img);
  fprintf(stderr, "Setting some pixels\n");
  for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++) {
      setpx(img, w, x, y, rand() % 255);
    }
  }
  fprintf(stderr, "Writing image to stdout\n");
  fwrite(img, sz, 1, stdout);
  free(img);
  fprintf(stderr, "Goodbye.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     120a <main+0x4a>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ca <main+0x20a>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0x20(%rbp),%rax
xor    %eax,%edi
call   1070 <srand@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
call   1040 <size@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x34(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12ab <main+0xeb>
lea    0xda1(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    13ca <main+0x20a>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1372 <main+0x1b2>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    135f <main+0x19f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   10e0 <rand@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%esi
mov    -0x44(%rbp),%ecx
mov    $0xff,%r8d
cltd
idiv   %r8d
mov    %edx,%r8d
mov    -0x40(%rbp),%edx
call   10a0 <setpx@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1305 <main+0x145>
jmp    1364 <main+0x1a4>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    12f2 <main+0x132>
mov    0x2c5f(%rip),%rax
mov    (%rax),%edi
lea    0xcfe(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    0x2c39(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    12ea <main+0x12a>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <clock_gettime@plt>
mov    0x8(%rsp),%edi
xor    0x10(%rsp),%edi
call   1070 <srand@plt>
mov    0x8(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r12d
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <size@plt>
mov    %eax,%r15d
mov    0x2db4(%rip),%rbx
mov    (%rbx),%edi
lea    0xdeb(%rip),%rsi
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1364 <main+0x1a4>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1060 <memset@plt>
mov    (%rbx),%edi
lea    0xdee(%rip),%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
lea    0xdf2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
test   %r12d,%r12d
jle    130b <main+0x14b>
xor    %ebx,%ebx
jmp    1298 <main+0xd8>
nopl   (%rax)
add    $0x1,%ebx
cmp    %r12d,%ebx
je     130b <main+0x14b>
test   %ebp,%ebp
jle    1290 <main+0xd0>
xor    %r14d,%r14d
nop
xor    %eax,%eax
call   10e0 <rand@plt>
cltq
imul   $0xffffffff80808081,%rax,%r8
shr    $0x20,%r8
add    %eax,%r8d
mov    %r8d,%ecx
shr    $0x1f,%ecx
sar    $0x7,%r8d
add    %ecx,%r8d
mov    %r8d,%ecx
shl    $0x8,%ecx
sub    %ecx,%r8d
add    %eax,%r8d
mov    %r13,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %ebx,%ecx
call   10a0 <setpx@plt>
add    $0x1,%r14d
cmp    %r14d,%ebp
jne    12a0 <main+0xe0>
jmp    1290 <main+0xd0>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd03(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
jmp    1355 <main+0x195>
mov    0x2cc6(%rip),%rbx
mov    (%rbx),%edi
lea    0xd65(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2ca7(%rip),%rax
mov    (%rax),%ecx
mov    %r13,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    (%rbx),%edi
lea    0xd4d(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd0(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%eax
jmp    1355 <main+0x195>

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s width height\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [35 x i8] c"Image size: %dx%d Array size: %d\0A\00"
@.str.2 = [25 x i8] c"Failed to allocate image\00"
@.str.3 = [23 x i8] c"Allocated image at %p\0A\00"
@.str.4 = [21 x i8] c"Setting some pixels\0A\00"
@.str.5 = [25 x i8] c"Writing image to stdout\0A\00"
@stdout = global i32 0
@.str.6 = [10 x i8] c"Goodbye.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %98
21: ; preds = %2
%22 = load i32, i32* @CLOCK_MONOTONIC
%23 = call i32 @clock_gettime(i32 %22, %struct.timespec* %6)
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
%25 = load i64, i64* %24
%26 = trunc i64 %25 to i32
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
%28 = load i64, i64* %27
%29 = trunc i64 %28 to i32
%30 = xor i32 %26, %29
%31 = call i32 @srand(i32 %30)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 2
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %9
%40 = load i32, i32* %8
%41 = load i32, i32* %9
%42 = call i32 @size(i32 %40, i32 %41)
store i32 %42, i32* %10
%43 = load i32, i32* @stderr
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = load i32, i32* %10
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* %10
%49 = call i32* @malloc(i32 %48)
store i32* %49, i32** %7
%50 = load i32*, i32** %7
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %54
52: ; preds = %21
%53 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %3
br label %98
54: ; preds = %21
%55 = load i32*, i32** %7
%56 = load i32, i32* %10
%57 = call i32 @memset(i32* %55, i32 0, i32 %56)
%58 = load i32, i32* @stderr
%59 = load i32*, i32** %7
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32* %59)
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %12
br label %63
63: ; preds = %84, %54
%64 = load i32, i32* %12
%65 = load i32, i32* %9
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %87
67: ; preds = %63
store i32 0, i32* %11
br label %68
68: ; preds = %80, %67
%69 = load i32, i32* %11
%70 = load i32, i32* %8
%71 = icmp slt i32 %69, %70
br i1 %71, label %72, label %83
72: ; preds = %68
%73 = load i32*, i32** %7
%74 = load i32, i32* %8
%75 = load i32, i32* %11
%76 = load i32, i32* %12
%77 = call i32 (...) @rand()
%78 = srem i32 %77, 255
%79 = call i32 @setpx(i32* %73, i32 %74, i32 %75, i32 %76, i32 %78)
br label %80
80: ; preds = %72
%81 = load i32, i32* %11
%82 = add nsw i32 %81, 1
store i32 %82, i32* %11
br label %68
83: ; preds = %68
br label %84
84: ; preds = %83
%85 = load i32, i32* %12
%86 = add nsw i32 %85, 1
store i32 %86, i32* %12
br label %63
87: ; preds = %63
%88 = load i32, i32* @stderr
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %88, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%90 = load i32*, i32** %7
%91 = load i32, i32* %10
%92 = load i32, i32* @stdout
%93 = call i32 @fwrite(i32* %90, i32 %91, i32 1, i32 %92)
%94 = load i32*, i32** %7
%95 = call i32 @free(i32* %94)
%96 = load i32, i32* @stderr
%97 = call i32 (i32, i8*, ...) @fprintf(i32 %96, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %98
98: ; preds = %87, %52, %15
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s width height\0A\00"
@CLOCK_MONOTONIC = global i32 0
@.str.1 = [35 x i8] c"Image size: %dx%d Array size: %d\0A\00"
@.str.2 = [25 x i8] c"Failed to allocate image\00"
@.str.3 = [23 x i8] c"Allocated image at %p\0A\00"
@.str.4 = [21 x i8] c"Setting some pixels\0A\00"
@.str.5 = [25 x i8] c"Writing image to stdout\0A\00"
@stdout = global i32 0
@.str.6 = [10 x i8] c"Goodbye.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timespec
%4 = bitcast %struct.timespec* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %62
10: ; preds = %2
%11 = load i32, i32* @CLOCK_MONOTONIC
%12 = call i32 @clock_gettime(i32 %11, %struct.timespec* %3)
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
%16 = load i64, i64* %15
%17 = xor i64 %16, %14
%18 = trunc i64 %17 to i32
%19 = call i32 @srand(i32 %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = call i32 @size(i32 %22, i32 %25)
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %25, i32 %26)
%29 = call i32* @malloc(i32 %26)
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %33
31: ; preds = %10
%32 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %62
33: ; preds = %10
%34 = call i32 @memset(i32* %29, i32 0, i32 %26)
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32* %29)
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%39 = icmp sgt i32 %25, 0
br i1 %39, label %40, label %54
40: ; preds = %33
%41 = icmp sgt i32 %22, 0
br label %42
42: ; preds = %40, %51
%43 = phi i32 [ 0, %40 ], [ %52, %51 ]
br i1 %41, label %44, label %51
44: ; preds = %42, %44
%45 = phi i32 [ %49, %44 ], [ 0, %42 ]
%46 = call i32 (...) @rand()
%47 = srem i32 %46, 255
%48 = call i32 @setpx(i32* %29, i32 %22, i32 %45, i32 %43, i32 %47)
%49 = add nuw nsw i32 %45, 1
%50 = icmp eq i32 %49, %22
br i1 %50, label %51, label %44
51: ; preds = %44, %42
%52 = add nuw nsw i32 %43, 1
%53 = icmp eq i32 %52, %25
br i1 %53, label %54, label %42
54: ; preds = %51, %33
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32, i32* @stdout
%58 = call i32 @fwrite(i32* %29, i32 %26, i32 1, i32 %57)
%59 = call i32 @free(i32* %29)
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %54, %31, %6
%63 = phi i32 [ 1, %6 ], [ 2, %31 ], [ 0, %54 ]
ret i32 %63
}