#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 */
void set_grey(uint8_t *grey, int i, uint8_t val) {
  if (i % 2 == 1) {
    grey[i / 2] = (grey[i / 2] & 0xF0) | (val >> 4);
  } else {
    grey[i / 2] = (grey[i / 2] & 0x0F) | (val & 0xF0);
  }
}


x86 assembly (clang, -O0):
<set_grey>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x1,%edx
jne    1178 <set_grey+0x78>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
and    $0xf0,%eax
mov    -0x10(%rbp),%ecx
sar    $0x4,%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11ca <set_grey+0xca>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
and    $0xf,%eax
mov    -0x10(%rbp),%ecx
and    $0xf0,%ecx
or     %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x34(%rbp),%edx
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_grey>:
mov    %esi,%eax
shr    $0x1f,%eax
add    %esi,%eax
mov    %eax,%ecx
sar    %ecx
and    $0xfffffffe,%eax
sub    %eax,%esi
movslq %ecx,%rax
mov    (%rdi,%rax,4),%ecx
cmp    $0x1,%esi
jne    112a <set_grey+0x2a>
and    $0xf0,%ecx
sar    $0x4,%edx
or     %ecx,%edx
mov    %edx,(%rdi,%rax,4)
ret
and    $0xf,%ecx
and    $0xf0,%edx
or     %ecx,%edx
mov    %edx,(%rdi,%rax,4)
ret

LLVM IR (-O0):
define void @set_grey(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%7 = load i32, i32* %5
%8 = srem i32 %7, 2
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %26
10: ; preds = %3
%11 = load i32*, i32** %4
%12 = load i32, i32* %5
%13 = sdiv i32 %12, 2
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %11, i64 %14
%16 = load i32, i32* %15
%17 = and i32 %16, 240
%18 = load i32, i32* %6
%19 = ashr i32 %18, 4
%20 = or i32 %17, %19
%21 = load i32*, i32** %4
%22 = load i32, i32* %5
%23 = sdiv i32 %22, 2
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %21, i64 %24
store i32 %20, i32* %25
br label %42
26: ; preds = %3
%27 = load i32*, i32** %4
%28 = load i32, i32* %5
%29 = sdiv i32 %28, 2
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32, i32* %27, i64 %30
%32 = load i32, i32* %31
%33 = and i32 %32, 15
%34 = load i32, i32* %6
%35 = and i32 %34, 240
%36 = or i32 %33, %35
%37 = load i32*, i32** %4
%38 = load i32, i32* %5
%39 = sdiv i32 %38, 2
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32, i32* %37, i64 %40
store i32 %36, i32* %41
br label %42
42: ; preds = %26, %10
ret void
}

LLVM IR (-O2):
define void @set_grey(i32* nocapture %0, i32 %1, i32 %2) {
%4 = srem i32 %1, 2
%5 = icmp eq i32 %4, 1
%6 = sdiv i32 %1, 2
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i32, i32* %0, i64 %7
%9 = load i32, i32* %8
br i1 %5, label %10, label %14
10: ; preds = %3
%11 = and i32 %9, 240
%12 = ashr i32 %2, 4
%13 = or i32 %11, %12
br label %18
14: ; preds = %3
%15 = and i32 %9, 15
%16 = and i32 %2, 240
%17 = or i32 %15, %16
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ %17, %14 ], [ %13, %10 ]
store i32 %19, i32* %8
ret void
}