#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(short *);
int /*<<< orphan*/ mag_img(short *, short *, short *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ scale_img(short *, short *, int, int, int, int);
int /*<<< orphan*/ sobelx(short *, short *, int, int);
int /*<<< orphan*/ sobely(short *, short *, int, int);
void grad_img(short int *src, short int *dst, int scale, int ncol, int npix) {
  short int *imgx, *imgy, *imgm;
  imgx = (short int *)malloc(npix * sizeof(short int));
  imgy = (short int *)malloc(npix * sizeof(short int));
  imgm = (short int *)malloc(npix * sizeof(short int));
  sobelx(src, imgx, ncol, npix);
  sobely(src, imgy, ncol, npix);
  mag_img(imgx, imgy, imgm, npix);
  scale_img(imgm, dst, scale, 1, 1, npix);
  free(imgx);
  free(imgy);
  free(imgm);
}


x86 assembly (clang, -O0):
<grad_img>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1060 <sobelx@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1080 <sobely@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1050 <mag_img@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%r9d
mov    $0x1,%r8d
mov    %r8d,%ecx
call   1040 <scale_img@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grad_img>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%ebx
mov    %ecx,%r14d
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    %rdi,%r15
lea    (%rbx,%rbx,1),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
call   1060 <sobelx@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
call   1080 <sobely@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
mov    %ebx,%ecx
call   1050 <mag_img@plt>
mov    %rbp,%rdi
mov    0x10(%rsp),%rsi
mov    0xc(%rsp),%edx
mov    $0x1,%ecx
mov    $0x1,%r8d
mov    %ebx,%r9d
call   1040 <scale_img@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @grad_img(i16* %0, i16* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i16*
%7 = alloca i16*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i16*
%12 = alloca i16*
%13 = alloca i16*
store i16* %0, i16** %6
store i16* %1, i16** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%14 = load i32, i32* %10
%15 = sext i32 %14 to i64
%16 = mul i64 %15, 2
%17 = trunc i64 %16 to i32
%18 = call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i16*
store i16* %19, i16** %11
%20 = load i32, i32* %10
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 2
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i16*
store i16* %25, i16** %12
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 2
%29 = trunc i64 %28 to i32
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i16*
store i16* %31, i16** %13
%32 = load i16*, i16** %6
%33 = load i16*, i16** %11
%34 = load i32, i32* %9
%35 = load i32, i32* %10
%36 = call i32 @sobelx(i16* %32, i16* %33, i32 %34, i32 %35)
%37 = load i16*, i16** %6
%38 = load i16*, i16** %12
%39 = load i32, i32* %9
%40 = load i32, i32* %10
%41 = call i32 @sobely(i16* %37, i16* %38, i32 %39, i32 %40)
%42 = load i16*, i16** %11
%43 = load i16*, i16** %12
%44 = load i16*, i16** %13
%45 = load i32, i32* %10
%46 = call i32 @mag_img(i16* %42, i16* %43, i16* %44, i32 %45)
%47 = load i16*, i16** %13
%48 = load i16*, i16** %7
%49 = load i32, i32* %8
%50 = load i32, i32* %10
%51 = call i32 @scale_img(i16* %47, i16* %48, i32 %49, i32 1, i32 1, i32 %50)
%52 = load i16*, i16** %11
%53 = call i32 @free(i16* %52)
%54 = load i16*, i16** %12
%55 = call i32 @free(i16* %54)
%56 = load i16*, i16** %13
%57 = call i32 @free(i16* %56)
ret void
}

LLVM IR (-O2):
define void @grad_img(i16* %0, i16* %1, i32 %2, i32 %3, i32 %4) {
%6 = shl i32 %4, 1
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i16*
%9 = tail call i64 @malloc(i32 %6)
%10 = inttoptr i64 %9 to i16*
%11 = tail call i64 @malloc(i32 %6)
%12 = inttoptr i64 %11 to i16*
%13 = tail call i32 @sobelx(i16* %0, i16* %8, i32 %3, i32 %4)
%14 = tail call i32 @sobely(i16* %0, i16* %10, i32 %3, i32 %4)
%15 = tail call i32 @mag_img(i16* %8, i16* %10, i16* %12, i32 %4)
%16 = tail call i32 @scale_img(i16* %12, i16* %1, i32 %2, i32 1, i32 1, i32 %4)
%17 = tail call i32 @free(i16* %8)
%18 = tail call i32 @free(i16* %10)
%19 = tail call i32 @free(i16* %12)
ret void
}