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

/* Variables and functions */
int /*<<< orphan*/ add_grey(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int extract_grey(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ index(int, int);
int /*<<< orphan*/ index_bw(int, int);
int /*<<< orphan*/ set_bw(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
void floyd_steinberg_dither(uint8_t *grey, uint8_t *bw, int num_pixels) {
  int w = 168;
  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_bw(x, y), n);
      int error = g - n;
      if (x < w - 1)
        add_grey(grey, index(x + 1, y), (7 * error) / 16);
      if (x > 1 && y < h - 1)
        add_grey(grey, index(x - 1, y + 1), (3 * error) / 16);
      if (y < h - 1)
        add_grey(grey, index(x, y + 1), (5 * error) / 16);
      if (x < w - 1 && y < h - 1)
        add_grey(grey, index(x + 1, y + 1), (1 * error) / 16);
      continue;
    }
  }
}


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   $0xa8,-0x18(%rbp)
movl   $0x90,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    133e <floyd_steinberg_dither+0x1ee>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    132b <floyd_steinberg_dither+0x1db>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1040 <index@plt>
mov    -0x40(%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,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1070 <index_bw@plt>
mov    -0x38(%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    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1235 <floyd_steinberg_dither+0xe5>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%esi
call   1040 <index@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
imul   $0x7,-0x30(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1030 <add_grey@plt>
cmpl   $0x1,-0x24(%rbp)
jle    1282 <floyd_steinberg_dither+0x132>
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1282 <floyd_steinberg_dither+0x132>
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x24(%rbp),%edi
sub    $0x1,%edi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
call   1040 <index@plt>
mov    -0x50(%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    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12c2 <floyd_steinberg_dither+0x172>
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
call   1040 <index@plt>
mov    -0x58(%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    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1318 <floyd_steinberg_dither+0x1c8>
mov    -0x20(%rbp),%eax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1318 <floyd_steinberg_dither+0x1c8>
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%esi
add    $0x1,%esi
call   1040 <index@plt>
mov    -0x60(%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>
jmp    131d <floyd_steinberg_dither+0x1cd>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118b <floyd_steinberg_dither+0x3b>
jmp    1330 <floyd_steinberg_dither+0x1e0>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1178 <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
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %rdi,%r15
xor    %r13d,%r13d
jmp    1180 <floyd_steinberg_dither+0x30>
nopl   0x0(%rax,%rax,1)
mov    %r12d,%r13d
cmp    $0x90,%r12d
je     12c0 <floyd_steinberg_dither+0x170>
cmp    $0x8f,%r13d
setae  0xf(%rsp)
lea    0x1(%r13),%r12d
xor    %ebp,%ebp
jmp    11ab <floyd_steinberg_dither+0x5b>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ebp
cmp    $0xa8,%ebp
je     1170 <floyd_steinberg_dither+0x20>
mov    %ebp,%edi
mov    %r13d,%esi
call   1040 <index@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <extract_grey@plt>
mov    %eax,%ebx
cmp    $0x7f,%eax
mov    $0x0,%r14d
mov    $0xff,%eax
cmovg  %eax,%r14d
mov    %ebp,%edi
mov    %r13d,%esi
call   1070 <index_bw@plt>
mov    0x10(%rsp),%rdi
mov    %eax,%esi
mov    %r14d,%edx
call   1060 <set_bw@plt>
sub    %r14d,%ebx
cmp    $0xa7,%ebp
je     1220 <floyd_steinberg_dither+0xd0>
lea    0x1(%rbp),%edi
mov    %r13d,%esi
call   1040 <index@plt>
lea    0x0(,%rbx,8),%ecx
sub    %ebx,%ecx
lea    0xf(%rcx),%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x4,%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
cmp    $0x2,%ebp
jb     1254 <floyd_steinberg_dither+0x104>
cmp    $0x8e,%r13d
ja     1254 <floyd_steinberg_dither+0x104>
lea    -0x1(%rbp),%edi
mov    %r12d,%esi
call   1040 <index@plt>
lea    (%rbx,%rbx,2),%ecx
lea    (%rbx,%rbx,2),%edx
add    $0xf,%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x4,%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
cmp    $0x8e,%r13d
ja     11a0 <floyd_steinberg_dither+0x50>
cmp    $0xa7,%ebp
sete   %r14b
mov    %ebp,%edi
mov    %r12d,%esi
call   1040 <index@plt>
lea    (%rbx,%rbx,4),%ecx
lea    (%rbx,%rbx,4),%edx
add    $0xf,%edx
test   %ecx,%ecx
cmovns %ecx,%edx
sar    $0x4,%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
or     0xf(%rsp),%r14b
jne    11a0 <floyd_steinberg_dither+0x50>
lea    0x1(%rbp),%edi
mov    %r12d,%esi
call   1040 <index@plt>
lea    0xf(%rbx),%edx
test   %ebx,%ebx
cmovns %ebx,%edx
sar    $0x4,%edx
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <add_grey@plt>
jmp    11a0 <floyd_steinberg_dither+0x50>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @floyd_steinberg_dither(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%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 i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 168, i32* %7
store i32 144, i32* %8
store i32 0, i32* %9
br label %14
14: ; preds = %116, %3
%15 = load i32, i32* %9
%16 = load i32, i32* %8
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %119
18: ; preds = %14
store i32 0, i32* %10
br label %19
19: ; preds = %112, %18
%20 = load i32, i32* %10
%21 = load i32, i32* %7
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %115
23: ; preds = %19
%24 = load i32*, i32** %4
%25 = load i32, i32* %10
%26 = load i32, i32* %9
%27 = call i32 @index(i32 %25, i32 %26)
%28 = call i32 @extract_grey(i32* %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 i32*, i32** %5
%34 = load i32, i32* %10
%35 = load i32, i32* %9
%36 = call i32 @index_bw(i32 %34, i32 %35)
%37 = load i32, i32* %12
%38 = call i32 @set_bw(i32* %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 i32, i32* %10
%43 = load i32, i32* %7
%44 = sub nsw i32 %43, 1
%45 = icmp slt i32 %42, %44
br i1 %45, label %46, label %56
46: ; preds = %23
%47 = load i32*, i32** %4
%48 = load i32, i32* %10
%49 = add nsw i32 %48, 1
%50 = load i32, i32* %9
%51 = call i32 @index(i32 %49, i32 %50)
%52 = load i32, i32* %13
%53 = mul nsw i32 7, %52
%54 = sdiv i32 %53, 16
%55 = call i32 @add_grey(i32* %47, i32 %51, i32 %54)
br label %56
56: ; preds = %46, %23
%57 = load i32, i32* %10
%58 = icmp sgt i32 %57, 1
br i1 %58, label %59, label %75
59: ; preds = %56
%60 = load i32, i32* %9
%61 = load i32, i32* %8
%62 = sub nsw i32 %61, 1
%63 = icmp slt i32 %60, %62
br i1 %63, label %64, label %75
64: ; preds = %59
%65 = load i32*, i32** %4
%66 = load i32, i32* %10
%67 = sub nsw i32 %66, 1
%68 = load i32, i32* %9
%69 = add nsw i32 %68, 1
%70 = call i32 @index(i32 %67, i32 %69)
%71 = load i32, i32* %13
%72 = mul nsw i32 3, %71
%73 = sdiv i32 %72, 16
%74 = call i32 @add_grey(i32* %65, i32 %70, i32 %73)
br label %75
75: ; preds = %64, %59, %56
%76 = load i32, i32* %9
%77 = load i32, i32* %8
%78 = sub nsw i32 %77, 1
%79 = icmp slt i32 %76, %78
br i1 %79, label %80, label %90
80: ; preds = %75
%81 = load i32*, i32** %4
%82 = load i32, i32* %10
%83 = load i32, i32* %9
%84 = add nsw i32 %83, 1
%85 = call i32 @index(i32 %82, i32 %84)
%86 = load i32, i32* %13
%87 = mul nsw i32 5, %86
%88 = sdiv i32 %87, 16
%89 = call i32 @add_grey(i32* %81, i32 %85, i32 %88)
br label %90
90: ; preds = %80, %75
%91 = load i32, i32* %10
%92 = load i32, i32* %7
%93 = sub nsw i32 %92, 1
%94 = icmp slt i32 %91, %93
br i1 %94, label %95, label %111
95: ; preds = %90
%96 = load i32, i32* %9
%97 = load i32, i32* %8
%98 = sub nsw i32 %97, 1
%99 = icmp slt i32 %96, %98
br i1 %99, label %100, label %111
100: ; preds = %95
%101 = load i32*, i32** %4
%102 = load i32, i32* %10
%103 = add nsw i32 %102, 1
%104 = load i32, i32* %9
%105 = add nsw i32 %104, 1
%106 = call i32 @index(i32 %103, i32 %105)
%107 = load i32, i32* %13
%108 = mul nsw i32 1, %107
%109 = sdiv i32 %108, 16
%110 = call i32 @add_grey(i32* %101, i32 %106, i32 %109)
br label %111
111: ; preds = %100, %95, %90
br label %112
112: ; preds = %111
%113 = load i32, i32* %10
%114 = add nsw i32 %113, 1
store i32 %114, i32* %10
br label %19
115: ; preds = %19
br label %116
116: ; preds = %115
%117 = load i32, i32* %9
%118 = add nsw i32 %117, 1
store i32 %118, i32* %9
br label %14
119: ; preds = %14
ret void
}

LLVM IR (-O2):
define void @floyd_steinberg_dither(i32* %0, i32* %1, i32 %2) {
br label %4
4: ; preds = %3, %10
%5 = phi i32 [ 0, %3 ], [ %7, %10 ]
%6 = icmp ult i32 %5, 143
%7 = add nuw nsw i32 %5, 1
%8 = xor i1 %6, true
br label %12
9: ; preds = %10
ret void
10: ; preds = %49
%11 = icmp eq i32 %7, 144
br i1 %11, label %9, label %4
12: ; preds = %4, %49
%13 = phi i32 [ 0, %4 ], [ %50, %49 ]
%14 = tail call i32 @index(i32 %13, i32 %5)
%15 = tail call i32 @extract_grey(i32* %0, i32 %14)
%16 = icmp sgt i32 %15, 127
%17 = select i1 %16, i32 255, i32 0
%18 = tail call i32 @index_bw(i32 %13, i32 %5)
%19 = tail call i32 @set_bw(i32* %1, i32 %18, i32 %17)
%20 = sub nsw i32 %15, %17
%21 = icmp eq i32 %13, 167
br i1 %21, label %28, label %22
22: ; preds = %12
%23 = add nuw nsw i32 %13, 1
%24 = tail call i32 @index(i32 %23, i32 %5)
%25 = mul nsw i32 %20, 7
%26 = sdiv i32 %25, 16
%27 = tail call i32 @add_grey(i32* %0, i32 %24, i32 %26)
br label %28
28: ; preds = %22, %12
%29 = icmp ugt i32 %13, 1
%30 = select i1 %29, i1 %6, i1 false
br i1 %30, label %31, label %37
31: ; preds = %28
%32 = add nsw i32 %13, -1
%33 = tail call i32 @index(i32 %32, i32 %7)
%34 = mul nsw i32 %20, 3
%35 = sdiv i32 %34, 16
%36 = tail call i32 @add_grey(i32* %0, i32 %33, i32 %35)
br label %37
37: ; preds = %31, %28
br i1 %6, label %38, label %49
38: ; preds = %37
%39 = tail call i32 @index(i32 %13, i32 %7)
%40 = mul nsw i32 %20, 5
%41 = sdiv i32 %40, 16
%42 = tail call i32 @add_grey(i32* %0, i32 %39, i32 %41)
%43 = select i1 %21, i1 true, i1 %8
br i1 %43, label %49, label %44
44: ; preds = %38
%45 = add nuw nsw i32 %13, 1
%46 = tail call i32 @index(i32 %45, i32 %7)
%47 = sdiv i32 %20, 16
%48 = tail call i32 @add_grey(i32* %0, i32 %46, i32 %47)
br label %49
49: ; preds = %37, %38, %44
%50 = add nuw nsw i32 %13, 1
%51 = icmp eq i32 %50, 168
br i1 %51, label %10, label %12
}