#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*/ int8_t;

/* Variables and functions */
int /*<<< orphan*/ writeBit(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
void c64WriteBit(int8_t *buf, int x, int8_t val) {
  int m = x % 320;
  int xl = m % 8 + ((m / 8) * 64);
  int yl = x / 320;
  int r = yl / 8;
  int idx = ((yl % 8) * 8) + xl + (r * 320 * 8);
  writeBit(buf, idx, val);
}


x86 assembly (clang, -O0):
<c64WriteBit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x140,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%edx
shl    $0x6,%eax
add    %eax,%edx
mov    %edx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x140,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
shl    $0x3,%eax
add    -0x18(%rbp),%eax
imul   $0x140,-0x20(%rbp),%ecx
shl    $0x3,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <writeBit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<c64WriteBit>:
movslq %esi,%r9
imul   $0x66666667,%r9,%rax
mov    %rax,%r8
sar    $0x27,%r8
mov    %rax,%r10
shr    $0x3f,%r10
lea    (%r8,%r10,1),%r11d
mov    %r11d,%ecx
shl    $0x6,%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %r9d,%esi
sub    %ecx,%esi
neg    %ecx
add    %r9d,%ecx
add    $0x7,%ecx
test   %si,%si
cmovns %esi,%ecx
movswl %cx,%r9d
and    $0xfff8,%ecx
sub    %ecx,%esi
movswl %si,%ecx
and    $0xfffffff8,%r9d
lea    (%r8,%r10,1),%esi
add    $0x7,%esi
test   %r11d,%r11d
cmovns %r11d,%esi
and    $0x1ffffff8,%esi
sub    %esi,%r11d
sar    $0x2a,%rax
add    %r10d,%eax
lea    (%rax,%rax,4),%eax
shl    $0x9,%eax
lea    (%rax,%r11,8),%eax
add    %ecx,%eax
lea    (%rax,%r9,8),%esi
jmp    1030 <writeBit@plt>

LLVM IR (-O0):
define void @c64WriteBit(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
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* %5
%13 = srem i32 %12, 320
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = srem i32 %14, 8
%16 = load i32, i32* %7
%17 = sdiv i32 %16, 8
%18 = mul nsw i32 %17, 64
%19 = add nsw i32 %15, %18
store i32 %19, i32* %8
%20 = load i32, i32* %5
%21 = sdiv i32 %20, 320
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = sdiv i32 %22, 8
store i32 %23, i32* %10
%24 = load i32, i32* %9
%25 = srem i32 %24, 8
%26 = mul nsw i32 %25, 8
%27 = load i32, i32* %8
%28 = add nsw i32 %26, %27
%29 = load i32, i32* %10
%30 = mul nsw i32 %29, 320
%31 = mul nsw i32 %30, 8
%32 = add nsw i32 %28, %31
store i32 %32, i32* %11
%33 = load i32*, i32** %4
%34 = load i32, i32* %11
%35 = load i32, i32* %6
%36 = call i32 @writeBit(i32* %33, i32 %34, i32 %35)
ret void
}

LLVM IR (-O2):
define void @c64WriteBit(i32* %0, i32 %1, i32 %2) {
%4 = srem i32 %1, 320
%5 = trunc i32 %4 to i16
%6 = srem i16 %5, 8
%7 = sext i16 %6 to i32
%8 = sdiv i16 %5, 8
%9 = sext i16 %8 to i32
%10 = shl nsw i32 %9, 6
%11 = sdiv i32 %1, 320
%12 = srem i32 %11, 8
%13 = shl nsw i32 %12, 3
%14 = srem i32 %1, 2560
%15 = sub i32 %1, %14
%16 = add i32 %15, %13
%17 = add i32 %16, %7
%18 = add i32 %17, %10
%19 = tail call i32 @writeBit(i32* %0, i32 %18, i32 %2)
ret void
}