#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 char *malloc(int);
int /*<<< orphan*/ rgb16_to24(unsigned short *, unsigned char *);
unsigned char *buf16_to24(unsigned int *buf, int w, int h) {
  unsigned char *buf24, *tmp;
  unsigned short *buf16;
  int i;
  int size = w * h * 3;
  buf16 = (short *)buf;
  buf24 = malloc(w * h * 3);
  tmp = buf24;
  for (i = 0; i < w * h; i++) {
    rgb16_to24(buf16, buf24);
    buf16++;
    buf24 += 3;
  }
  return tmp;
}


x86 assembly (clang, -O0):
<buf16_to24>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
imul   $0x3,%eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0xc(%rbp),%ecx
imul   -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    11ae <buf16_to24+0x8e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <rgb16_to24@plt>
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x18(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1169 <buf16_to24+0x49>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf16_to24>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r12
imul   %edx,%r14d
lea    (%r14,%r14,2),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    1169 <buf16_to24+0x49>
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <rgb16_to24@plt>
add    $0x2,%r12
add    $0x3,%rbx
add    $0xffffffff,%r14d
jne    1150 <buf16_to24+0x30>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @buf16_to24(i32* %0, i32 %1, i32 %2) {
%4 = mul nsw i32 %2, %1
%5 = mul nsw i32 %4, 3
%6 = tail call i8* @malloc(i32 %5)
%7 = icmp sgt i32 %4, 0
br i1 %7, label %8, label %19
8: ; preds = %3
%9 = bitcast i32* %0 to i16*
br label %10
10: ; preds = %8, %10
%11 = phi i8* [ %16, %10 ], [ %6, %8 ]
%12 = phi i32 [ %17, %10 ], [ 0, %8 ]
%13 = phi i16* [ %15, %10 ], [ %9, %8 ]
%14 = tail call i32 @rgb16_to24(i16* %13, i8* %11)
%15 = getelementptr inbounds i16, i16* %13, i64 1
%16 = getelementptr inbounds i8, i8* %11, i64 3
%17 = add nuw nsw i32 %12, 1
%18 = icmp eq i32 %17, %4
br i1 %18, label %19, label %10
19: ; preds = %10, %3
ret i8* %6
}