#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*/ add_grey(unsigned char *, int /*<<< orphan*/, int);
int extract_grey(unsigned char *, int /*<<< orphan*/);
int /*<<< orphan*/ index(int, int);
int /*<<< orphan*/ set_bw(unsigned char *, int /*<<< orphan*/, int);
void floyd_steinberg_dither(unsigned char *grey, unsigned char *bw,
                            int num_pixels) {
  int w = 144;
  int h = 144;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      int g = extract_grey(grey, index(x, y));
      int n = g > 127 ? 255 : 0;
      set_bw(bw, index(x, y), n);
      int error = g - n;
      add_grey(grey, index(x + 1, y), (7 * error) / 16);
      add_grey(grey, index(x - 1, y + 1), (3 * error) / 16);
      add_grey(grey, index(x, y + 1), (5 * error) / 16);
      add_grey(grey, index(x + 1, y + 1), (1 * error) / 16);
    }
  }
}


x86 assembly (clang, -O0):
<floyd_steinberg_dither>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x90,-0x18(%rbp)
movl   $0x90,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12ca <floyd_steinberg_dither+0x18a>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12b7 <floyd_steinberg_dither+0x177>
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <index@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   1050 <extract_grey@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    $0xff,%ecx
cmp    $0x7f,%edx
cmovg  %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <index@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
mov    -0x2c(%rbp),%edx
call   1060 <set_bw@plt>
mov    -0x28(%rbp),%eax
sub    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%esi
call   1040 <index@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%esi
imul   $0x7,-0x30(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1030 <add_grey@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%edi
sub    $0x1,%edi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
call   1040 <index@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
imul   $0x3,-0x30(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1030 <add_grey@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
call   1040 <index@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%esi
imul   $0x5,-0x30(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1030 <add_grey@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
call   1040 <index@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
mov    -0x30(%rbp),%eax
shl    $0x0,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1030 <add_grey@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117b <floyd_steinberg_dither+0x3b>
jmp    12bc <floyd_steinberg_dither+0x17c>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1168 <floyd_steinberg_dither+0x28>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<floyd_steinberg_dither>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,(%rsp)
mov    %rdi,%r13
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%r15),%r12d
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15d,%esi
call   1040 <index@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1050 <extract_grey@plt>
mov    %eax,%ebp
cmp    $0x7f,%eax
mov    $0x0,%ebx
mov    $0xff,%eax
cmovg  %eax,%ebx
mov    %r14d,%edi
mov    %r15d,%esi
call   1040 <index@plt>
mov    (%rsp),%rdi
mov    %eax,%esi
mov    %ebx,%edx
call   1060 <set_bw@plt>
sub    %ebx,%ebp
lea    0x1(%r14),%ebx
mov    %ebx,%edi
mov    %r15d,%esi
call   1040 <index@plt>
lea    0x0(,%rbp,8),%ecx
sub    %ebp,%ecx
lea    0xf(%rcx),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x4,%edx
mov    %r13,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
lea    -0x1(%r14),%edi
mov    %r12d,%esi
call   1040 <index@plt>
lea    0x0(%rbp,%rbp,2),%ecx
lea    0xf(%rbp,%rbp,2),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x4,%edx
mov    %r13,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
mov    %r14d,%edi
mov    %r12d,%esi
call   1040 <index@plt>
lea    0x0(%rbp,%rbp,4),%ecx
lea    0xf(%rbp,%rbp,4),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x4,%edx
mov    %r13,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
mov    %ebx,%edi
mov    %r12d,%esi
call   1040 <index@plt>
lea    0xf(%rbp),%edx
test   %ebp,%ebp
cmovns %ebp,%edx
sar    $0x4,%edx
mov    %r13,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
mov    %ebx,%r14d
cmp    $0x90,%ebx
jne    1170 <floyd_steinberg_dither+0x30>
mov    %r12d,%r15d
cmp    $0x90,%r12d
jne    1160 <floyd_steinberg_dither+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @floyd_steinberg_dither(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 144, i32* %7
store i32 144, i32* %8
store i32 0, i32* %9
br label %14
14: ; preds = %84, %3
%15 = load i32, i32* %9
%16 = load i32, i32* %8
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %87
18: ; preds = %14
store i32 0, i32* %10
br label %19
19: ; preds = %80, %18
%20 = load i32, i32* %10
%21 = load i32, i32* %7
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %83
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = load i32, i32* %10
%26 = load i32, i32* %9
%27 = call i32 @index(i32 %25, i32 %26)
%28 = call i32 @extract_grey(i8* %24, i32 %27)
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = icmp sgt i32 %29, 127
%31 = zext i1 %30 to i64
%32 = select i1 %30, i32 255, i32 0
store i32 %32, i32* %12
%33 = load i8*, i8** %5
%34 = load i32, i32* %10
%35 = load i32, i32* %9
%36 = call i32 @index(i32 %34, i32 %35)
%37 = load i32, i32* %12
%38 = call i32 @set_bw(i8* %33, i32 %36, i32 %37)
%39 = load i32, i32* %11
%40 = load i32, i32* %12
%41 = sub nsw i32 %39, %40
store i32 %41, i32* %13
%42 = load i8*, i8** %4
%43 = load i32, i32* %10
%44 = add nsw i32 %43, 1
%45 = load i32, i32* %9
%46 = call i32 @index(i32 %44, i32 %45)
%47 = load i32, i32* %13
%48 = mul nsw i32 7, %47
%49 = sdiv i32 %48, 16
%50 = call i32 @add_grey(i8* %42, i32 %46, i32 %49)
%51 = load i8*, i8** %4
%52 = load i32, i32* %10
%53 = sub nsw i32 %52, 1
%54 = load i32, i32* %9
%55 = add nsw i32 %54, 1
%56 = call i32 @index(i32 %53, i32 %55)
%57 = load i32, i32* %13
%58 = mul nsw i32 3, %57
%59 = sdiv i32 %58, 16
%60 = call i32 @add_grey(i8* %51, i32 %56, i32 %59)
%61 = load i8*, i8** %4
%62 = load i32, i32* %10
%63 = load i32, i32* %9
%64 = add nsw i32 %63, 1
%65 = call i32 @index(i32 %62, i32 %64)
%66 = load i32, i32* %13
%67 = mul nsw i32 5, %66
%68 = sdiv i32 %67, 16
%69 = call i32 @add_grey(i8* %61, i32 %65, i32 %68)
%70 = load i8*, i8** %4
%71 = load i32, i32* %10
%72 = add nsw i32 %71, 1
%73 = load i32, i32* %9
%74 = add nsw i32 %73, 1
%75 = call i32 @index(i32 %72, i32 %74)
%76 = load i32, i32* %13
%77 = mul nsw i32 1, %76
%78 = sdiv i32 %77, 16
%79 = call i32 @add_grey(i8* %70, i32 %75, i32 %78)
br label %80
80: ; preds = %23
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 1
store i32 %82, i32* %10
br label %19
83: ; preds = %19
br label %84
84: ; preds = %83
%85 = load i32, i32* %9
%86 = add nsw i32 %85, 1
store i32 %86, i32* %9
br label %14
87: ; preds = %14
ret void
}

LLVM IR (-O2):
define void @floyd_steinberg_dither(i8* %0, i8* %1, i32 %2) {
br label %4
4: ; preds = %3, %8
%5 = phi i32 [ 0, %3 ], [ %6, %8 ]
%6 = add nuw nsw i32 %5, 1
br label %10
7: ; preds = %8
ret void
8: ; preds = %10
%9 = icmp eq i32 %6, 144
br i1 %9, label %7, label %4
10: ; preds = %4, %10
%11 = phi i32 [ 0, %4 ], [ %19, %10 ]
%12 = tail call i32 @index(i32 %11, i32 %5)
%13 = tail call i32 @extract_grey(i8* %0, i32 %12)
%14 = icmp sgt i32 %13, 127
%15 = select i1 %14, i32 255, i32 0
%16 = tail call i32 @index(i32 %11, i32 %5)
%17 = tail call i32 @set_bw(i8* %1, i32 %16, i32 %15)
%18 = sub nsw i32 %13, %15
%19 = add nuw nsw i32 %11, 1
%20 = tail call i32 @index(i32 %19, i32 %5)
%21 = mul nsw i32 %18, 7
%22 = sdiv i32 %21, 16
%23 = tail call i32 @add_grey(i8* %0, i32 %20, i32 %22)
%24 = add nsw i32 %11, -1
%25 = tail call i32 @index(i32 %24, i32 %6)
%26 = mul nsw i32 %18, 3
%27 = sdiv i32 %26, 16
%28 = tail call i32 @add_grey(i8* %0, i32 %25, i32 %27)
%29 = tail call i32 @index(i32 %11, i32 %6)
%30 = mul nsw i32 %18, 5
%31 = sdiv i32 %30, 16
%32 = tail call i32 @add_grey(i8* %0, i32 %29, i32 %31)
%33 = tail call i32 @index(i32 %19, i32 %6)
%34 = sdiv i32 %18, 16
%35 = tail call i32 @add_grey(i8* %0, i32 %33, i32 %34)
%36 = icmp eq i32 %19, 144
br i1 %36, label %8, label %10
}