#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(int);
int /*<<< orphan*/ *memcpy(char *, char *, int);
scalar_t__ pixelMessage(char *, char, int);
int /*<<< orphan*/ printf(char *);
size_t strlen(char *);
int embed(char *image_data, int data_length, char *message, char **encoded) {
  char *dataArray = NULL;
  char *encArray = NULL;
  int idx = 0;
  int messageidx = 0;
  size_t messagelen = 0;
  messagelen = strlen(message);
  if (data_length / 8 < (int)messagelen) {
    printf("Message wont fit\n");
    return -1;
  }
  encArray = (char *)malloc(sizeof(char) * data_length);
  if (encArray == NULL) {
    printf("Couldnt allocate memory, possibly out of memory\n");
    return -1;
  }
  if (memcpy(encArray, image_data, sizeof(char) * data_length) == NULL) {
    printf("Could not copy memory\n");
    free(encArray);
    encArray = NULL;
    return -1;
  }
  for (idx; idx < data_length; idx += 8, ++messageidx) {
    char c = message[messageidx];
    dataArray = image_data + idx;
    if (pixelMessage(encArray, c, data_length - idx) != 0) {
      printf("Failed to embed\n");
      free(encArray);
      return -1;
    }
    if (messageidx >= (int)messagelen) {
      printf("Message has been coded\n");
      break;
    }
  }
  *encoded = encArray;
  return 0;
}


x86 assembly (clang, -O0):
<embed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x48(%rbp),%rcx
cmp    %ecx,%eax
jge    11d9 <embed+0x79>
lea    0xe38(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <embed+0x1b0>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    120f <embed+0xaf>
lea    0xe14(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <embed+0x1b0>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
cmp    $0x0,%rax
jne    1259 <embed+0xf9>
lea    0xe0c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <embed+0x1b0>
mov    -0x3c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12fe <embed+0x19e>
mov    -0x20(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x49(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x49(%rbp),%al
mov    -0x14(%rbp),%edx
sub    -0x3c(%rbp),%edx
movsbl %al,%esi
call   1080 <pixelMessage@plt>
cmp    $0x0,%rax
je     12c2 <embed+0x162>
lea    0xdb2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1310 <embed+0x1b0>
mov    -0x40(%rbp),%eax
mov    -0x48(%rbp),%rcx
cmp    %ecx,%eax
jl     12e2 <embed+0x182>
lea    0xd93(%rip),%rdi
call   1050 <printf@plt>
jmp    12fe <embed+0x19e>
jmp    12e7 <embed+0x187>
mov    -0x3c(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1259 <embed+0xf9>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<embed>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
lea    0x7(%r15),%eax
test   %r15d,%r15d
cmovns %r15d,%eax
sar    $0x3,%eax
cmp    %r13d,%eax
jge    11a1 <embed+0x41>
lea    0xed1(%rip),%rdi
jmp    1220 <embed+0xc0>
mov    %r15d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1219 <embed+0xb9>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
test   %rax,%rax
je     1239 <embed+0xd9>
mov    %r14,(%rsp)
test   %r15d,%r15d
jle    1264 <embed+0x104>
xor    %r14d,%r14d
test   %r13d,%r13d
cmovle %r14d,%r13d
lea    0x8(,%r13,8),%r13
mov    %r15d,%ebp
cs nopw 0x0(%rax,%rax,1)
movsbl (%rbx),%esi
mov    %r12,%rdi
mov    %ebp,%edx
call   1080 <pixelMessage@plt>
test   %rax,%rax
jne    1242 <embed+0xe2>
add    $0x8,%r14
cmp    %r14,%r13
je     1258 <embed+0xf8>
add    $0x1,%rbx
add    $0xfffffff8,%ebp
cmp    %r15d,%r14d
jl     11f0 <embed+0x90>
jmp    1264 <embed+0x104>
lea    0xe1d(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde7(%rip),%rdi
jmp    1249 <embed+0xe9>
lea    0xdce(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1225 <embed+0xc5>
lea    0xda1(%rip),%rdi
call   1040 <puts@plt>
mov    (%rsp),%rax
mov    %r12,(%rax)
xor    %eax,%eax
jmp    122a <embed+0xca>

LLVM IR (-O0):
@.str = [18 x i8] c"Message wont fit\0A\00"
@.str.1 = [49 x i8] c"Couldnt allocate memory, possibly out of memory\0A\00"
@.str.2 = [23 x i8] c"Could not copy memory\0A\00"
@.str.3 = [17 x i8] c"Failed to embed\0A\00"
@.str.4 = [24 x i8] c"Message has been coded\0A\00"
define i32 @embed(i8* %0, i32 %1, i8* %2, i8** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i8
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8** %3, i8*** %9
store i8* null, i8** %10
store i8* null, i8** %11
store i32 0, i32* %12
store i32 0, i32* %13
store i64 0, i64* %14
%16 = load i8*, i8** %8
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %14
%18 = load i32, i32* %7
%19 = sdiv i32 %18, 8
%20 = load i64, i64* %14
%21 = trunc i64 %20 to i32
%22 = icmp slt i32 %19, %21
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %92
25: ; preds = %4
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = mul i64 1, %27
%29 = trunc i64 %28 to i32
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %11
%32 = load i8*, i8** %11
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %92
36: ; preds = %25
%37 = load i8*, i8** %11
%38 = load i8*, i8** %6
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = mul i64 1, %40
%42 = trunc i64 %41 to i32
%43 = call i32* @memcpy(i8* %37, i8* %38, i32 %42)
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %49
45: ; preds = %36
%46 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%47 = load i8*, i8** %11
%48 = call i32 @free(i8* %47)
store i8* null, i8** %11
store i32 -1, i32* %5
br label %92
49: ; preds = %36
%50 = load i32, i32* %12
br label %51
51: ; preds = %84, %49
%52 = load i32, i32* %12
%53 = load i32, i32* %7
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %89
55: ; preds = %51
%56 = load i8*, i8** %8
%57 = load i32, i32* %13
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
%60 = load i8, i8* %59
store i8 %60, i8* %15
%61 = load i8*, i8** %6
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
store i8* %64, i8** %10
%65 = load i8*, i8** %11
%66 = load i8, i8* %15
%67 = load i32, i32* %7
%68 = load i32, i32* %12
%69 = sub nsw i32 %67, %68
%70 = call i64 @pixelMessage(i8* %65, i8 signext %66, i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %55
%73 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%74 = load i8*, i8** %11
%75 = call i32 @free(i8* %74)
store i32 -1, i32* %5
br label %92
76: ; preds = %55
%77 = load i32, i32* %13
%78 = load i64, i64* %14
%79 = trunc i64 %78 to i32
%80 = icmp sge i32 %77, %79
br i1 %80, label %81, label %83
81: ; preds = %76
%82 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %89
83: ; preds = %76
br label %84
84: ; preds = %83
%85 = load i32, i32* %12
%86 = add nsw i32 %85, 8
store i32 %86, i32* %12
%87 = load i32, i32* %13
%88 = add nsw i32 %87, 1
store i32 %88, i32* %13
br label %51
89: ; preds = %81, %51
%90 = load i8*, i8** %11
%91 = load i8**, i8*** %9
store i8* %90, i8** %91
store i32 0, i32* %5
br label %92
92: ; preds = %89, %72, %45, %34, %23
%93 = load i32, i32* %5
ret i32 %93
}

LLVM IR (-O2):
@str = [23 x i8] c"Message has been coded\00"
@str.5 = [16 x i8] c"Failed to embed\00"
@str.6 = [22 x i8] c"Could not copy memory\00"
@str.7 = [48 x i8] c"Couldnt allocate memory, possibly out of memory\00"
@str.8 = [17 x i8] c"Message wont fit\00"
define i32 @embed(i8* %0, i32 %1, i8* nocapture %2, i8** nocapture writeonly %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%6 = sdiv i32 %1, 8
%7 = trunc i64 %5 to i32
%8 = icmp slt i32 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.8, i64 0, i64 0))
br label %48
11: ; preds = %4
%12 = tail call i64 @malloc(i32 %1)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.7, i64 0, i64 0))
br label %48
17: ; preds = %11
%18 = tail call i32* @memcpy(i8* %13, i8* %0, i32 %1)
%19 = icmp eq i32* %18, null
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = icmp sgt i32 %1, 0
br i1 %21, label %22, label %47
22: ; preds = %20
%23 = call i32 @llvm.smax.i32(i32 %7, i32 0)
%24 = zext i32 %23 to i64
br label %28
25: ; preds = %17
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
%27 = tail call i32 @free(i8* %13)
br label %48
28: ; preds = %22, %43
%29 = phi i64 [ 0, %22 ], [ %45, %43 ]
%30 = phi i32 [ 0, %22 ], [ %44, %43 ]
%31 = getelementptr inbounds i8, i8* %2, i64 %29
%32 = load i8, i8* %31
%33 = sub nsw i32 %1, %30
%34 = tail call i64 @pixelMessage(i8* %13, i8 signext %32, i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %28
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
%38 = tail call i32 @free(i8* %13)
br label %48
39: ; preds = %28
%40 = icmp eq i64 %29, %24
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %47
43: ; preds = %39
%44 = add nuw nsw i32 %30, 8
%45 = add nuw nsw i64 %29, 1
%46 = icmp slt i32 %44, %1
br i1 %46, label %28, label %47
47: ; preds = %43, %20, %41
store i8* %13, i8** %3
br label %48
48: ; preds = %36, %47, %25, %15, %9
%49 = phi i32 [ -1, %9 ], [ -1, %15 ], [ -1, %25 ], [ 0, %47 ], [ -1, %36 ]
ret i32 %49
}