#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 abs(unsigned char);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
unsigned char update_neighbor_pixels(int, unsigned char *, unsigned char *);
void subtract_background_w_adjust_8(unsigned char *input, unsigned char *bg,
                                    int threshold) {
  int i;
  unsigned char *backup = (unsigned char *)malloc(3686400);
  memcpy(backup, input, 3686400);
  for (i = 0; i < 3686400; i += 4) {
    (abs(input[i] - bg[i]) <= threshold)
        ? input[i] = 0x00
        : update_neighbor_pixels(i, input, backup);
    (abs(input[i + 1] - bg[i + 1]) <= threshold)
        ? input[i + 1] = 0x00
        : update_neighbor_pixels(i + 1, input, backup);
    (abs(input[i + 2] - bg[i + 2]) <= threshold)
        ? input[i + 2] = 0x00
        : update_neighbor_pixels(i + 2, input, backup);
    (abs(input[i + 3] - bg[i + 3]) <= threshold)
        ? input[i + 3] = 0x00
        : update_neighbor_pixels(i + 3, input, backup);
  }
  free(backup);
  return;
}


x86 assembly (clang, -O0):
<subtract_background_w_adjust_8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x384000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x384000,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x384000,-0x18(%rbp)
jge    1317 <subtract_background_w_adjust_8+0x1c7>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
movslq -0x18(%rbp),%rdx
movzbl (%rcx,%rdx,1),%ecx
sub    %ecx,%eax
movzbl %al,%edi
call   1040 <abs@plt>
cmp    -0x14(%rbp),%eax
jg     11d3 <subtract_background_w_adjust_8+0x83>
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11e6 <subtract_background_w_adjust_8+0x96>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <update_neighbor_pixels@plt>
movzbl %al,%eax
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
sub    %ecx,%eax
movzbl %al,%edi
call   1040 <abs@plt>
cmp    -0x14(%rbp),%eax
jg     1231 <subtract_background_w_adjust_8+0xe1>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1247 <subtract_background_w_adjust_8+0xf7>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <update_neighbor_pixels@plt>
movzbl %al,%eax
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
add    $0x2,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
sub    %ecx,%eax
movzbl %al,%edi
call   1040 <abs@plt>
cmp    -0x14(%rbp),%eax
jg     1292 <subtract_background_w_adjust_8+0x142>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12a8 <subtract_background_w_adjust_8+0x158>
mov    -0x18(%rbp),%edi
add    $0x2,%edi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <update_neighbor_pixels@plt>
movzbl %al,%eax
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
movzbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%edx
add    $0x3,%edx
movslq %edx,%rdx
movzbl (%rcx,%rdx,1),%ecx
sub    %ecx,%eax
movzbl %al,%edi
call   1040 <abs@plt>
cmp    -0x14(%rbp),%eax
jg     12f3 <subtract_background_w_adjust_8+0x1a3>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1309 <subtract_background_w_adjust_8+0x1b9>
mov    -0x18(%rbp),%edi
add    $0x3,%edi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <update_neighbor_pixels@plt>
movzbl %al,%eax
mov    -0x18(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x18(%rbp)
jmp    118a <subtract_background_w_adjust_8+0x3a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subtract_background_w_adjust_8>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r13
mov    %rdi,%r14
mov    $0x384000,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x384000,%edx
call   1050 <memcpy@plt>
lea    0x3(%r14),%rbp
mov    $0xfffffffffffffffc,%rbx
jmp    11af <subtract_background_w_adjust_8+0x5f>
xchg   %ax,%ax
lea    0x7(%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <update_neighbor_pixels@plt>
add    $0x4,%rbx
cmp    $0x383ffc,%rbx
jae    126a <subtract_background_w_adjust_8+0x11a>
movzbl 0x1(%rbp,%rbx,1),%eax
sub    0x4(%r13,%rbx,1),%al
movzbl %al,%edi
call   1040 <abs@plt>
cmp    %r12d,%eax
jle    11e0 <subtract_background_w_adjust_8+0x90>
lea    0x4(%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <update_neighbor_pixels@plt>
jmp    11e5 <subtract_background_w_adjust_8+0x95>
cs nopw 0x0(%rax,%rax,1)
movb   $0x0,0x1(%rbp,%rbx,1)
movzbl 0x2(%rbp,%rbx,1),%eax
sub    0x5(%r13,%rbx,1),%al
movzbl %al,%edi
call   1040 <abs@plt>
cmp    %r12d,%eax
jle    1210 <subtract_background_w_adjust_8+0xc0>
lea    0x5(%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <update_neighbor_pixels@plt>
jmp    1215 <subtract_background_w_adjust_8+0xc5>
nopl   0x0(%rax)
movb   $0x0,0x2(%rbp,%rbx,1)
movzbl 0x3(%rbp,%rbx,1),%eax
sub    0x6(%r13,%rbx,1),%al
movzbl %al,%edi
call   1040 <abs@plt>
cmp    %r12d,%eax
jle    1240 <subtract_background_w_adjust_8+0xf0>
lea    0x6(%rbx),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <update_neighbor_pixels@plt>
jmp    1245 <subtract_background_w_adjust_8+0xf5>
nopl   0x0(%rax)
movb   $0x0,0x3(%rbp,%rbx,1)
movzbl 0x4(%rbp,%rbx,1),%eax
sub    0x7(%r13,%rbx,1),%al
movzbl %al,%edi
call   1040 <abs@plt>
cmp    %r12d,%eax
jg     1190 <subtract_background_w_adjust_8+0x40>
movb   $0x0,0x4(%rbp,%rbx,1)
jmp    119e <subtract_background_w_adjust_8+0x4e>
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @subtract_background_w_adjust_8(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = call i64 @malloc(i32 3686400)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = load i8*, i8** %4
%13 = call i32 @memcpy(i8* %11, i8* %12, i32 3686400)
store i32 0, i32* %7
br label %14
14: ; preds = %150, %3
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 3686400
br i1 %16, label %17, label %153
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = sub nsw i32 %23, %29
%31 = trunc i32 %30 to i8
%32 = call i32 @abs(i8 zeroext %31)
%33 = load i32, i32* %6
%34 = icmp sle i32 %32, %33
br i1 %34, label %35, label %40
35: ; preds = %17
%36 = load i8*, i8** %4
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
br label %46
40: ; preds = %17
%41 = load i32, i32* %7
%42 = load i8*, i8** %4
%43 = load i8*, i8** %8
%44 = call zeroext i8 @update_neighbor_pixels(i32 %41, i8* %42, i8* %43)
%45 = zext i8 %44 to i32
br label %46
46: ; preds = %40, %35
%47 = phi i32 [ 0, %35 ], [ %45, %40 ]
%48 = load i8*, i8** %4
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = load i8*, i8** %5
%56 = load i32, i32* %7
%57 = add nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = sub nsw i32 %54, %61
%63 = trunc i32 %62 to i8
%64 = call i32 @abs(i8 zeroext %63)
%65 = load i32, i32* %6
%66 = icmp sle i32 %64, %65
br i1 %66, label %67, label %73
67: ; preds = %46
%68 = load i8*, i8** %4
%69 = load i32, i32* %7
%70 = add nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
store i8 0, i8* %72
br label %80
73: ; preds = %46
%74 = load i32, i32* %7
%75 = add nsw i32 %74, 1
%76 = load i8*, i8** %4
%77 = load i8*, i8** %8
%78 = call zeroext i8 @update_neighbor_pixels(i32 %75, i8* %76, i8* %77)
%79 = zext i8 %78 to i32
br label %80
80: ; preds = %73, %67
%81 = phi i32 [ 0, %67 ], [ %79, %73 ]
%82 = load i8*, i8** %4
%83 = load i32, i32* %7
%84 = add nsw i32 %83, 2
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
%87 = load i8, i8* %86
%88 = zext i8 %87 to i32
%89 = load i8*, i8** %5
%90 = load i32, i32* %7
%91 = add nsw i32 %90, 2
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds i8, i8* %89, i64 %92
%94 = load i8, i8* %93
%95 = zext i8 %94 to i32
%96 = sub nsw i32 %88, %95
%97 = trunc i32 %96 to i8
%98 = call i32 @abs(i8 zeroext %97)
%99 = load i32, i32* %6
%100 = icmp sle i32 %98, %99
br i1 %100, label %101, label %107
101: ; preds = %80
%102 = load i8*, i8** %4
%103 = load i32, i32* %7
%104 = add nsw i32 %103, 2
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds i8, i8* %102, i64 %105
store i8 0, i8* %106
br label %114
107: ; preds = %80
%108 = load i32, i32* %7
%109 = add nsw i32 %108, 2
%110 = load i8*, i8** %4
%111 = load i8*, i8** %8
%112 = call zeroext i8 @update_neighbor_pixels(i32 %109, i8* %110, i8* %111)
%113 = zext i8 %112 to i32
br label %114
114: ; preds = %107, %101
%115 = phi i32 [ 0, %101 ], [ %113, %107 ]
%116 = load i8*, i8** %4
%117 = load i32, i32* %7
%118 = add nsw i32 %117, 3
%119 = sext i32 %118 to i64
%120 = getelementptr inbounds i8, i8* %116, i64 %119
%121 = load i8, i8* %120
%122 = zext i8 %121 to i32
%123 = load i8*, i8** %5
%124 = load i32, i32* %7
%125 = add nsw i32 %124, 3
%126 = sext i32 %125 to i64
%127 = getelementptr inbounds i8, i8* %123, i64 %126
%128 = load i8, i8* %127
%129 = zext i8 %128 to i32
%130 = sub nsw i32 %122, %129
%131 = trunc i32 %130 to i8
%132 = call i32 @abs(i8 zeroext %131)
%133 = load i32, i32* %6
%134 = icmp sle i32 %132, %133
br i1 %134, label %135, label %141
135: ; preds = %114
%136 = load i8*, i8** %4
%137 = load i32, i32* %7
%138 = add nsw i32 %137, 3
%139 = sext i32 %138 to i64
%140 = getelementptr inbounds i8, i8* %136, i64 %139
store i8 0, i8* %140
br label %148
141: ; preds = %114
%142 = load i32, i32* %7
%143 = add nsw i32 %142, 3
%144 = load i8*, i8** %4
%145 = load i8*, i8** %8
%146 = call zeroext i8 @update_neighbor_pixels(i32 %143, i8* %144, i8* %145)
%147 = zext i8 %146 to i32
br label %148
148: ; preds = %141, %135
%149 = phi i32 [ 0, %135 ], [ %147, %141 ]
br label %150
150: ; preds = %148
%151 = load i32, i32* %7
%152 = add nsw i32 %151, 4
store i32 %152, i32* %7
br label %14
153: ; preds = %14
%154 = load i8*, i8** %8
%155 = call i32 @free(i8* %154)
ret void
}

LLVM IR (-O2):
define void @subtract_background_w_adjust_8(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i64 @malloc(i32 3686400)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i32 @memcpy(i8* %5, i8* %0, i32 3686400)
br label %7
7: ; preds = %3, %59
%8 = phi i64 [ 0, %3 ], [ %60, %59 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = getelementptr inbounds i8, i8* %1, i64 %8
%12 = load i8, i8* %11
%13 = sub i8 %10, %12
%14 = tail call i32 @abs(i8 zeroext %13)
%15 = icmp sgt i32 %14, %2
br i1 %15, label %17, label %16
16: ; preds = %7
store i8 0, i8* %9
br label %20
17: ; preds = %7
%18 = trunc i64 %8 to i32
%19 = tail call zeroext i8 @update_neighbor_pixels(i32 %18, i8* %0, i8* %5)
br label %20
20: ; preds = %17, %16
%21 = or i64 %8, 1
%22 = getelementptr inbounds i8, i8* %0, i64 %21
%23 = load i8, i8* %22
%24 = getelementptr inbounds i8, i8* %1, i64 %21
%25 = load i8, i8* %24
%26 = sub i8 %23, %25
%27 = tail call i32 @abs(i8 zeroext %26)
%28 = icmp sgt i32 %27, %2
br i1 %28, label %30, label %29
29: ; preds = %20
store i8 0, i8* %22
br label %33
30: ; preds = %20
%31 = trunc i64 %21 to i32
%32 = tail call zeroext i8 @update_neighbor_pixels(i32 %31, i8* %0, i8* %5)
br label %33
33: ; preds = %30, %29
%34 = or i64 %8, 2
%35 = getelementptr inbounds i8, i8* %0, i64 %34
%36 = load i8, i8* %35
%37 = getelementptr inbounds i8, i8* %1, i64 %34
%38 = load i8, i8* %37
%39 = sub i8 %36, %38
%40 = tail call i32 @abs(i8 zeroext %39)
%41 = icmp sgt i32 %40, %2
br i1 %41, label %43, label %42
42: ; preds = %33
store i8 0, i8* %35
br label %46
43: ; preds = %33
%44 = trunc i64 %34 to i32
%45 = tail call zeroext i8 @update_neighbor_pixels(i32 %44, i8* %0, i8* %5)
br label %46
46: ; preds = %43, %42
%47 = or i64 %8, 3
%48 = getelementptr inbounds i8, i8* %0, i64 %47
%49 = load i8, i8* %48
%50 = getelementptr inbounds i8, i8* %1, i64 %47
%51 = load i8, i8* %50
%52 = sub i8 %49, %51
%53 = tail call i32 @abs(i8 zeroext %52)
%54 = icmp sgt i32 %53, %2
br i1 %54, label %56, label %55
55: ; preds = %46
store i8 0, i8* %48
br label %59
56: ; preds = %46
%57 = trunc i64 %47 to i32
%58 = tail call zeroext i8 @update_neighbor_pixels(i32 %57, i8* %0, i8* %5)
br label %59
59: ; preds = %55, %56
%60 = add nuw nsw i64 %8, 4
%61 = icmp ult i64 %8, 3686396
br i1 %61, label %7, label %62
62: ; preds = %59
%63 = tail call i32 @free(i8* %5)
ret void
}