#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 */
void *coproc_read(int);
int /*<<< orphan*/ coproc_reset(int);
int /*<<< orphan*/ coproc_write(int, int);
void scale_opt3(unsigned char *data, int pixel_nb) {
  int i;
  unsigned char min, max;
  unsigned short beta;
  unsigned int beta_mini;
  unsigned int min_max;
  int *p;
  p = (int *)data;
  coproc_reset(0x40000000);
  for (i = 0; i < pixel_nb; i += 4) {
    coproc_write(0x40000004, *p);
    p += 1;
  }
  min_max = coproc_read(0x40000004);
  p = (int *)data;
  coproc_reset(0x40000030);
  coproc_write(0x40000034, min_max);
  for (i = 0; i < pixel_nb; i += 4) {
    coproc_write(0x40000034, *p);
    *p = coproc_read(0x40000034);
    p += 1;
  }
}


x86 assembly (clang, -O0):
<scale_opt3>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x40000000,%edi
call   1040 <coproc_reset@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118e <scale_opt3+0x5e>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    $0x40000004,%edi
call   1050 <coproc_write@plt>
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x10(%rbp)
jmp    1158 <scale_opt3+0x28>
mov    $0x40000004,%edi
call   1030 <coproc_read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x40000030,%edi
call   1040 <coproc_reset@plt>
mov    -0x1c(%rbp),%esi
mov    $0x40000034,%edi
call   1050 <coproc_write@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1209 <scale_opt3+0xd9>
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    $0x40000034,%edi
call   1050 <coproc_write@plt>
mov    $0x40000034,%edi
call   1030 <coproc_read@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x10(%rbp)
jmp    11c1 <scale_opt3+0x91>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scale_opt3>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
mov    $0x40000000,%edi
call   1040 <coproc_reset@plt>
test   %r15d,%r15d
jle    11de <scale_opt3+0xae>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rbx,%rbp,1),%esi
mov    $0x40000004,%edi
call   1050 <coproc_write@plt>
add    $0x4,%rbp
cmp    %r15d,%ebp
jl     1160 <scale_opt3+0x30>
mov    $0x40000004,%edi
call   1030 <coproc_read@plt>
mov    %rax,%r14
mov    $0x40000030,%edi
call   1040 <coproc_reset@plt>
mov    $0x40000034,%edi
mov    %r14d,%esi
call   1050 <coproc_write@plt>
test   %r15d,%r15d
jle    11d3 <scale_opt3+0xa3>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rbx,%rbp,1),%esi
mov    $0x40000034,%edi
call   1050 <coproc_write@plt>
mov    $0x40000034,%edi
call   1030 <coproc_read@plt>
mov    %eax,(%rbx,%rbp,1)
add    $0x4,%rbp
cmp    %r15d,%ebp
jl     11b0 <scale_opt3+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x40000004,%edi
call   1030 <coproc_read@plt>
mov    %rax,%rbx
mov    $0x40000030,%edi
call   1040 <coproc_reset@plt>
mov    $0x40000034,%edi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <coproc_write@plt>

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

LLVM IR (-O2):
define void @scale_opt3(i8* nocapture %0, i32 %1) {
%3 = bitcast i8* %0 to i32*
%4 = tail call i32 @coproc_reset(i32 1073741824)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %31
6: ; preds = %2, %6
%7 = phi i32* [ %11, %6 ], [ %3, %2 ]
%8 = phi i32 [ %12, %6 ], [ 0, %2 ]
%9 = load i32, i32* %7
%10 = tail call i32 @coproc_write(i32 1073741828, i32 %9)
%11 = getelementptr inbounds i32, i32* %7, i64 1
%12 = add nuw nsw i32 %8, 4
%13 = icmp slt i32 %12, %1
br i1 %13, label %6, label %14
14: ; preds = %6
%15 = tail call i8* @coproc_read(i32 1073741828)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = tail call i32 @coproc_reset(i32 1073741872)
%19 = tail call i32 @coproc_write(i32 1073741876, i32 %17)
br i1 %5, label %20, label %37
20: ; preds = %14, %20
%21 = phi i32* [ %28, %20 ], [ %3, %14 ]
%22 = phi i32 [ %29, %20 ], [ 0, %14 ]
%23 = load i32, i32* %21
%24 = tail call i32 @coproc_write(i32 1073741876, i32 %23)
%25 = tail call i8* @coproc_read(i32 1073741876)
%26 = ptrtoint i8* %25 to i64
%27 = trunc i64 %26 to i32
store i32 %27, i32* %21
%28 = getelementptr inbounds i32, i32* %21, i64 1
%29 = add nuw nsw i32 %22, 4
%30 = icmp slt i32 %29, %1
br i1 %30, label %20, label %37
31: ; preds = %2
%32 = tail call i8* @coproc_read(i32 1073741828)
%33 = ptrtoint i8* %32 to i64
%34 = trunc i64 %33 to i32
%35 = tail call i32 @coproc_reset(i32 1073741872)
%36 = tail call i32 @coproc_write(i32 1073741876, i32 %34)
br label %37
37: ; preds = %20, %31, %14
ret void
}