#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*/ printf(char *, int, int);
void render_greyscale(const unsigned char *c, const unsigned char *d) {
  int color, color1, color2, k;
  color1 = (c[0] + c[1] + c[2]) / 3;
  color2 = (d[0] + d[1] + d[2]) / 3;
  k = 231 + ((int)((float)color1 / 10.3));
  if (k < 232)
    k = 232;
  printf("\x1b[%d;5;%dm", 48, k);
  k = 231 + ((int)((float)color2 / 10.3));
  if (k < 232)
    k = 232;
  printf("\x1b[%d;5;%dm", 38, k);
}


x86 assembly (clang, -O0):
<render_greyscale>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movzbl (%rax),%eax
mov    -0x8(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
add    %ecx,%eax
mov    -0x8(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
add    %ecx,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    -0x10(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
add    %ecx,%eax
mov    -0x10(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
add    %ecx,%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
cvtsi2ssl -0x18(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe83(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
add    $0xe7,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xe8,-0x20(%rbp)
jge    11a1 <render_greyscale+0x91>
movl   $0xe8,-0x20(%rbp)
mov    -0x20(%rbp),%edx
lea    0xe5d(%rip),%rdi
mov    $0x30,%esi
call   1030 <printf@plt>
cvtsi2ssl -0x1c(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  0xe3a(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
add    $0xe7,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xe8,-0x20(%rbp)
jge    11ea <render_greyscale+0xda>
movl   $0xe8,-0x20(%rbp)
mov    -0x20(%rbp),%edx
lea    0xe14(%rip),%rdi
mov    $0x26,%esi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<render_greyscale>:
push   %rbp
push   %r14
push   %rbx
movzbl (%rdi),%eax
movzbl 0x1(%rdi),%ecx
add    %eax,%ecx
movzbl 0x2(%rdi),%eax
add    %ecx,%eax
imul   $0xaaab,%eax,%eax
shr    $0x11,%eax
movzbl (%rsi),%ecx
movzbl 0x1(%rsi),%edx
add    %ecx,%edx
movzbl 0x2(%rsi),%ecx
add    %edx,%ecx
imul   $0xaaab,%ecx,%ebp
shr    $0x11,%ebp
cvtsi2sd %eax,%xmm0
divsd  0xeb0(%rip),%xmm0
cvttsd2si %xmm0,%edx
test   %edx,%edx
mov    $0x1,%ebx
cmovle %ebx,%edx
add    $0xe7,%edx
lea    0xe9d(%rip),%r14
mov    %r14,%rdi
mov    $0x30,%esi
call   1030 <printf@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %ebp,%xmm0
divsd  0xe79(%rip),%xmm0
cvttsd2si %xmm0,%eax
test   %eax,%eax
cmovg  %eax,%ebx
add    $0xe7,%ebx
mov    %r14,%rdi
mov    $0x26,%esi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"\1B[%d;5;%dm\00"
define void @render_greyscale(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = add nsw i32 %12, %16
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 2
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = add nsw i32 %17, %21
%23 = sdiv i32 %22, 3
store i32 %23, i32* %6
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = add nsw i32 %27, %31
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 2
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = add nsw i32 %32, %36
%38 = sdiv i32 %37, 3
store i32 %38, i32* %7
%39 = load i32, i32* %6
%40 = sitofp i32 %39 to float
%41 = fpext float %40 to double
%42 = fdiv double %41, 1.030000e+01
%43 = fptosi double %42 to i32
%44 = add nsw i32 231, %43
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp slt i32 %45, 232
br i1 %46, label %47, label %48
47: ; preds = %2
store i32 232, i32* %8
br label %48
48: ; preds = %47, %2
%49 = load i32, i32* %8
%50 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 48, i32 %49)
%51 = load i32, i32* %7
%52 = sitofp i32 %51 to float
%53 = fpext float %52 to double
%54 = fdiv double %53, 1.030000e+01
%55 = fptosi double %54 to i32
%56 = add nsw i32 231, %55
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp slt i32 %57, 232
br i1 %58, label %59, label %60
59: ; preds = %48
store i32 232, i32* %8
br label %60
60: ; preds = %59, %48
%61 = load i32, i32* %8
%62 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 38, i32 %61)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"\1B[%d;5;%dm\00"
define void @render_greyscale(i8* nocapture %0, i8* nocapture %1) {
%3 = load i8, i8* %0
%4 = zext i8 %3 to i16
%5 = getelementptr inbounds i8, i8* %0, i64 1
%6 = load i8, i8* %5
%7 = zext i8 %6 to i16
%8 = add nuw nsw i16 %7, %4
%9 = getelementptr inbounds i8, i8* %0, i64 2
%10 = load i8, i8* %9
%11 = zext i8 %10 to i16
%12 = add nuw nsw i16 %8, %11
%13 = udiv i16 %12, 3
%14 = load i8, i8* %1
%15 = zext i8 %14 to i16
%16 = getelementptr inbounds i8, i8* %1, i64 1
%17 = load i8, i8* %16
%18 = zext i8 %17 to i16
%19 = add nuw nsw i16 %18, %15
%20 = getelementptr inbounds i8, i8* %1, i64 2
%21 = load i8, i8* %20
%22 = zext i8 %21 to i16
%23 = add nuw nsw i16 %19, %22
%24 = udiv i16 %23, 3
%25 = uitofp i16 %13 to double
%26 = fdiv double %25, 1.030000e+01
%27 = fptosi double %26 to i32
%28 = icmp sgt i32 %27, 1
%29 = select i1 %28, i32 %27, i32 1
%30 = add nuw nsw i32 %29, 231
%31 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 48, i32 %30)
%32 = uitofp i16 %24 to double
%33 = fdiv double %32, 1.030000e+01
%34 = fptosi double %33 to i32
%35 = icmp sgt i32 %34, 1
%36 = select i1 %35, i32 %34, i32 1
%37 = add nuw nsw i32 %36, 231
%38 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 38, i32 %37)
ret void
}