#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*/ free(char *);
scalar_t__ malloc(long);
int /*<<< orphan*/ memcpy(char *, char *, long);
int /*<<< orphan*/ textureInvertRBAndRemoveAlphaChannel(char *, long, char *,
                                                        long);
int /*<<< orphan*/ textureInvertRBInPlace(char *, long);
int /*<<< orphan*/ textureRemoveAlphaChannel(char *, long, char *, long);
char *textureResample(char *srcBuf, long width, long height,
                      long inBitsPerPixel, long outBitsPerPixel,
                      int bgrOutput) {
  char *resampledData = NULL;
  long inBytesPerPixel, outBytesPerPixel;
  if (outBitsPerPixel == 0 || srcBuf == NULL)
    return NULL;
  if (inBitsPerPixel != 32)
    return NULL;
  if (outBitsPerPixel != 32 && outBitsPerPixel != 24)
    return NULL;
  inBytesPerPixel = inBitsPerPixel / 8;
  outBytesPerPixel = outBitsPerPixel / 8;
  resampledData = (char *)malloc(width * height * outBytesPerPixel);
  if (inBitsPerPixel == 32 && outBitsPerPixel == 24 && !bgrOutput) {
    textureRemoveAlphaChannel(resampledData, width * height * outBytesPerPixel,
                              srcBuf, width * height * inBytesPerPixel);
  } else if (inBitsPerPixel == 32 && outBitsPerPixel == 24 && bgrOutput) {
    textureInvertRBAndRemoveAlphaChannel(
        resampledData, width * height * outBytesPerPixel, srcBuf,
        width * height * inBytesPerPixel);
  } else if (inBitsPerPixel == 32 && outBitsPerPixel == 32 && bgrOutput) {
    memcpy(resampledData, srcBuf, width * height * outBytesPerPixel);
    textureInvertRBInPlace(resampledData, width * height * outBytesPerPixel);
  } else {
    free(resampledData);
    resampledData = NULL;
  }
  return resampledData;
}


x86 assembly (clang, -O0):
<textureResample>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     119e <textureResample+0x3e>
cmpq   $0x0,-0x10(%rbp)
jne    11ab <textureResample+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    1337 <textureResample+0x1d7>
cmpq   $0x20,-0x28(%rbp)
je     11c3 <textureResample+0x63>
movq   $0x0,-0x8(%rbp)
jmp    1337 <textureResample+0x1d7>
cmpq   $0x20,-0x30(%rbp)
je     11e6 <textureResample+0x86>
cmpq   $0x18,-0x30(%rbp)
je     11e6 <textureResample+0x86>
movq   $0x0,-0x8(%rbp)
jmp    1337 <textureResample+0x1d7>
mov    -0x28(%rbp),%rax
mov    $0x8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    $0x8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
imul   -0x20(%rbp),%rdi
imul   -0x50(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x20,-0x28(%rbp)
jne    126f <textureResample+0x10f>
cmpq   $0x18,-0x30(%rbp)
jne    126f <textureResample+0x10f>
cmpl   $0x0,-0x34(%rbp)
jne    126f <textureResample+0x10f>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
imul   -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
imul   -0x20(%rbp),%rcx
imul   -0x48(%rbp),%rcx
call   1050 <textureRemoveAlphaChannel@plt>
jmp    132f <textureResample+0x1cf>
cmpq   $0x20,-0x28(%rbp)
jne    12bd <textureResample+0x15d>
cmpq   $0x18,-0x30(%rbp)
jne    12bd <textureResample+0x15d>
cmpl   $0x0,-0x34(%rbp)
je     12bd <textureResample+0x15d>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
imul   -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
imul   -0x20(%rbp),%rcx
imul   -0x48(%rbp),%rcx
call   1080 <textureInvertRBAndRemoveAlphaChannel@plt>
jmp    132a <textureResample+0x1ca>
cmpq   $0x20,-0x28(%rbp)
jne    1314 <textureResample+0x1b4>
cmpq   $0x20,-0x30(%rbp)
jne    1314 <textureResample+0x1b4>
cmpl   $0x0,-0x34(%rbp)
je     1314 <textureResample+0x1b4>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
imul   -0x20(%rbp),%rdx
imul   -0x50(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
imul   -0x20(%rbp),%rsi
imul   -0x50(%rbp),%rsi
call   1040 <textureInvertRBInPlace@plt>
jmp    1325 <textureResample+0x1c5>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x40(%rbp)
jmp    132a <textureResample+0x1ca>
jmp    132f <textureResample+0x1cf>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<textureResample>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r15d,%r15d
cmp    $0x20,%rcx
jne    1235 <textureResample+0xd5>
mov    %rdi,%r14
test   %rdi,%rdi
je     1235 <textureResample+0xd5>
mov    %r8,%rbp
test   %r8,%r8
je     1235 <textureResample+0xd5>
mov    %r9d,%r12d
mov    %rdx,%rbx
cmp    $0x20,%rbp
je     11a6 <textureResample+0x46>
cmp    $0x18,%rbp
jne    1232 <textureResample+0xd2>
mov    %rbp,%r13
shr    $0x3,%r13
imul   %rsi,%rbx
imul   %rbx,%r13
mov    %r13,%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %r12d,%r12d
jne    11e2 <textureResample+0x82>
cmp    $0x18,%rbp
jne    11e2 <textureResample+0x82>
shl    $0x2,%rbx
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <textureRemoveAlphaChannel@plt>
jmp    1235 <textureResample+0xd5>
cmp    $0x18,%rbp
jne    1204 <textureResample+0xa4>
test   %r12d,%r12d
je     1204 <textureResample+0xa4>
shl    $0x2,%rbx
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1080 <textureInvertRBAndRemoveAlphaChannel@plt>
jmp    1235 <textureResample+0xd5>
cmp    $0x20,%rbp
jne    122a <textureResample+0xca>
test   %r12d,%r12d
je     122a <textureResample+0xca>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1060 <memcpy@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1040 <textureInvertRBInPlace@plt>
jmp    1235 <textureResample+0xd5>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @textureResample(i8* %0, i64 %1, i64 %2, i64 %3, i64 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i64
store i8* %0, i8** %8
store i64 %1, i64* %9
store i64 %2, i64* %10
store i64 %3, i64* %11
store i64 %4, i64* %12
store i32 %5, i32* %13
store i8* null, i8** %14
%17 = load i64, i64* %12
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %6
%20 = load i8*, i8** %8
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19, %6
store i8* null, i8** %7
br label %123
23: ; preds = %19
%24 = load i64, i64* %11
%25 = icmp ne i64 %24, 32
br i1 %25, label %26, label %27
26: ; preds = %23
store i8* null, i8** %7
br label %123
27: ; preds = %23
%28 = load i64, i64* %12
%29 = icmp ne i64 %28, 32
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i64, i64* %12
%32 = icmp ne i64 %31, 24
br i1 %32, label %33, label %34
33: ; preds = %30
store i8* null, i8** %7
br label %123
34: ; preds = %30, %27
%35 = load i64, i64* %11
%36 = sdiv i64 %35, 8
store i64 %36, i64* %15
%37 = load i64, i64* %12
%38 = sdiv i64 %37, 8
store i64 %38, i64* %16
%39 = load i64, i64* %9
%40 = load i64, i64* %10
%41 = mul nsw i64 %39, %40
%42 = load i64, i64* %16
%43 = mul nsw i64 %41, %42
%44 = call i64 @malloc(i64 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %14
%46 = load i64, i64* %11
%47 = icmp eq i64 %46, 32
br i1 %47, label %48, label %68
48: ; preds = %34
%49 = load i64, i64* %12
%50 = icmp eq i64 %49, 24
br i1 %50, label %51, label %68
51: ; preds = %48
%52 = load i32, i32* %13
%53 = icmp ne i32 %52, 0
br i1 %53, label %68, label %54
54: ; preds = %51
%55 = load i8*, i8** %14
%56 = load i64, i64* %9
%57 = load i64, i64* %10
%58 = mul nsw i64 %56, %57
%59 = load i64, i64* %16
%60 = mul nsw i64 %58, %59
%61 = load i8*, i8** %8
%62 = load i64, i64* %9
%63 = load i64, i64* %10
%64 = mul nsw i64 %62, %63
%65 = load i64, i64* %15
%66 = mul nsw i64 %64, %65
%67 = call i32 @textureRemoveAlphaChannel(i8* %55, i64 %60, i8* %61, i64 %66)
br label %121
68: ; preds = %51, %48, %34
%69 = load i64, i64* %11
%70 = icmp eq i64 %69, 32
br i1 %70, label %71, label %91
71: ; preds = %68
%72 = load i64, i64* %12
%73 = icmp eq i64 %72, 24
br i1 %73, label %74, label %91
74: ; preds = %71
%75 = load i32, i32* %13
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %91
77: ; preds = %74
%78 = load i8*, i8** %14
%79 = load i64, i64* %9
%80 = load i64, i64* %10
%81 = mul nsw i64 %79, %80
%82 = load i64, i64* %16
%83 = mul nsw i64 %81, %82
%84 = load i8*, i8** %8
%85 = load i64, i64* %9
%86 = load i64, i64* %10
%87 = mul nsw i64 %85, %86
%88 = load i64, i64* %15
%89 = mul nsw i64 %87, %88
%90 = call i32 @textureInvertRBAndRemoveAlphaChannel(i8* %78, i64 %83, i8* %84, i64 %89)
br label %120
91: ; preds = %74, %71, %68
%92 = load i64, i64* %11
%93 = icmp eq i64 %92, 32
br i1 %93, label %94, label %116
94: ; preds = %91
%95 = load i64, i64* %12
%96 = icmp eq i64 %95, 32
br i1 %96, label %97, label %116
97: ; preds = %94
%98 = load i32, i32* %13
%99 = icmp ne i32 %98, 0
br i1 %99, label %100, label %116
100: ; preds = %97
%101 = load i8*, i8** %14
%102 = load i8*, i8** %8
%103 = load i64, i64* %9
%104 = load i64, i64* %10
%105 = mul nsw i64 %103, %104
%106 = load i64, i64* %16
%107 = mul nsw i64 %105, %106
%108 = call i32 @memcpy(i8* %101, i8* %102, i64 %107)
%109 = load i8*, i8** %14
%110 = load i64, i64* %9
%111 = load i64, i64* %10
%112 = mul nsw i64 %110, %111
%113 = load i64, i64* %16
%114 = mul nsw i64 %112, %113
%115 = call i32 @textureInvertRBInPlace(i8* %109, i64 %114)
br label %119
116: ; preds = %97, %94, %91
%117 = load i8*, i8** %14
%118 = call i32 @free(i8* %117)
store i8* null, i8** %14
br label %119
119: ; preds = %116, %100
br label %120
120: ; preds = %119, %77
br label %121
121: ; preds = %120, %54
%122 = load i8*, i8** %14
store i8* %122, i8** %7
br label %123
123: ; preds = %121, %33, %26, %22
%124 = load i8*, i8** %7
ret i8* %124
}

LLVM IR (-O2):
define i8* @textureResample(i8* %0, i64 %1, i64 %2, i64 %3, i64 %4, i32 %5) {
%7 = icmp ne i64 %4, 0
%8 = icmp ne i8* %0, null
%9 = and i1 %8, %7
%10 = icmp eq i64 %3, 32
%11 = and i1 %10, %9
br i1 %11, label %12, label %39
12: ; preds = %6
switch i64 %4, label %39 [
i64 32, label %13
i64 24, label %13
]
13: ; preds = %12, %12
%14 = lshr i64 %4, 3
%15 = mul nsw i64 %2, %1
%16 = mul nsw i64 %14, %15
%17 = tail call i64 @malloc(i64 %16)
%18 = inttoptr i64 %17 to i8*
%19 = icmp eq i64 %4, 24
%20 = xor i1 %19, true
%21 = icmp ne i32 %5, 0
%22 = or i1 %21, %20
br i1 %22, label %26, label %23
23: ; preds = %13
%24 = shl nsw i64 %15, 2
%25 = tail call i32 @textureRemoveAlphaChannel(i8* %18, i64 %16, i8* %0, i64 %24)
br label %39
26: ; preds = %13
%27 = and i1 %19, %21
br i1 %27, label %28, label %31
28: ; preds = %26
%29 = shl nsw i64 %15, 2
%30 = tail call i32 @textureInvertRBAndRemoveAlphaChannel(i8* %18, i64 %16, i8* %0, i64 %29)
br label %39
31: ; preds = %26
%32 = icmp eq i64 %4, 32
%33 = and i1 %32, %21
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = tail call i32 @memcpy(i8* %18, i8* %0, i64 %16)
%36 = tail call i32 @textureInvertRBInPlace(i8* %18, i64 %16)
br label %39
37: ; preds = %31
%38 = tail call i32 @free(i8* %18)
br label %39
39: ; preds = %23, %34, %37, %28, %12, %6
%40 = phi i8* [ null, %6 ], [ null, %12 ], [ %18, %28 ], [ %18, %34 ], [ null, %37 ], [ %18, %23 ]
ret i8* %40
}