#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 */
unsigned short rgb24_to_rgb16(unsigned char, unsigned char, unsigned char);
unsigned char *buf24_to16(unsigned char *buf, int w, int h) {
  unsigned char *buf24;
  unsigned short *buf16;
  int i;
  int size = w * h * 3;
  buf16 = buf;
  for (i = 0; i < size; i += 3) {
    *buf16 = rgb24_to_rgb16(buf[i], buf[i + 1], buf[i + 2]);
    buf16++;
  }
}


x86 assembly (clang, -O0):
<buf24_to16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    11a7 <buf24_to16+0x97>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%sil
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movzbl %sil,%edi
movzbl %dl,%esi
movzbl (%rax,%rcx,1),%edx
call   1030 <rgb24_to_rgb16@plt>
mov    %ax,%cx
mov    -0x28(%rbp),%rax
mov    %cx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x3,%eax
mov    %eax,-0x2c(%rbp)
jmp    113e <buf24_to16+0x2e>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf24_to16>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
imul   %edx,%esi
lea    (%rsi,%rsi,2),%r14d
test   %r14d,%r14d
jle    1158 <buf24_to16+0x48>
mov    %rdi,%r15
xor    %ebp,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r15,%rbx,1),%edi
movzbl 0x1(%r15,%rbx,1),%esi
movzbl 0x2(%r15,%rbx,1),%edx
call   1030 <rgb24_to_rgb16@plt>
mov    %ax,(%r15,%rbp,1)
add    $0x3,%rbx
add    $0x2,%rbp
cmp    %ebx,%r14d
jg     1130 <buf24_to16+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @buf24_to16(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i16*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* %6
%13 = load i32, i32* %7
%14 = mul nsw i32 %12, %13
%15 = mul nsw i32 %14, 3
store i32 %15, i32* %11
%16 = load i8*, i8** %5
%17 = bitcast i8* %16 to i16*
store i16* %17, i16** %9
store i32 0, i32* %10
br label %18
18: ; preds = %44, %3
%19 = load i32, i32* %10
%20 = load i32, i32* %11
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %47
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = load i8*, i8** %5
%29 = load i32, i32* %10
%30 = add nsw i32 %29, 1
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = load i8, i8* %32
%34 = load i8*, i8** %5
%35 = load i32, i32* %10
%36 = add nsw i32 %35, 2
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
%39 = load i8, i8* %38
%40 = call zeroext i16 @rgb24_to_rgb16(i8 zeroext %27, i8 zeroext %33, i8 zeroext %39)
%41 = load i16*, i16** %9
store i16 %40, i16* %41
%42 = load i16*, i16** %9
%43 = getelementptr inbounds i16, i16* %42, i32 1
store i16* %43, i16** %9
br label %44
44: ; preds = %22
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 3
store i32 %46, i32* %10
br label %18
47: ; preds = %18
%48 = load i8*, i8** %4
ret i8* %48
}

LLVM IR (-O2):
define noalias i8* @buf24_to16(i8* nocapture %0, i32 %1, i32 %2) {
%4 = mul i32 %1, 3
%5 = mul i32 %4, %2
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %25
7: ; preds = %3
%8 = bitcast i8* %0 to i16*
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %22, %9 ]
%11 = phi i16* [ %8, %7 ], [ %21, %9 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %10
%13 = load i8, i8* %12
%14 = add nuw nsw i64 %10, 1
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = add nuw nsw i64 %10, 2
%18 = getelementptr inbounds i8, i8* %0, i64 %17
%19 = load i8, i8* %18
%20 = tail call zeroext i16 @rgb24_to_rgb16(i8 zeroext %13, i8 zeroext %16, i8 zeroext %19)
store i16 %20, i16* %11
%21 = getelementptr inbounds i16, i16* %11, i64 1
%22 = add nuw i64 %10, 3
%23 = trunc i64 %22 to i32
%24 = icmp sgt i32 %5, %23
br i1 %24, label %9, label %25
25: ; preds = %9, %3
ret i8* undef
}