#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 idx1 = 0;
  size_t messagelen = 0;
  char currentChar = 0;
  if (image_data == NULL || encoded == NULL || message == NULL) {
    printf("NULL pointer passed.\n");
    return -1;
  }
  messagelen = strlen(message);
  if ((data_length / 8) < (messagelen + 1)) {
    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 (idx1 = 0; idx1 < messagelen; ++idx1) {
    currentChar = message[idx1];
    printf("%c\n", currentChar);
    dataArray = encArray + idx;
    if (pixelMessage(encArray, currentChar, data_length - idx) != 0) {
      printf("Failed to embed 1\n");
      if (encArray != NULL) {
        free(encArray);
        encArray = NULL;
      }
      return -1;
    }
    idx += 8;
  }
  if (pixelMessage(encArray + idx, 0, data_length - idx) != 0) {
    printf("Failed to embed 2\n");
    if (encArray != NULL) {
      free(encArray);
      encArray = NULL;
    }
    return -1;
  }
  *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)
movb   $0x0,-0x49(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c2 <embed+0x62>
cmpq   $0x0,-0x28(%rbp)
je     11c2 <embed+0x62>
cmpq   $0x0,-0x20(%rbp)
jne    11dc <embed+0x7c>
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <embed+0x25f>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cltq
mov    -0x48(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jae    1221 <embed+0xc1>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <embed+0x25f>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1259 <embed+0xf9>
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <embed+0x25f>
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    12a5 <embed+0x145>
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <embed+0x25f>
movl   $0x0,-0x40(%rbp)
movslq -0x40(%rbp),%rax
cmp    -0x48(%rbp),%rax
jae    1355 <embed+0x1f5>
mov    -0x20(%rbp),%rax
movslq -0x40(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x49(%rbp)
movsbl -0x49(%rbp),%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%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     133e <embed+0x1de>
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
je     1332 <embed+0x1d2>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <embed+0x25f>
mov    -0x3c(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12ac <embed+0x14c>
mov    -0x38(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%edx
sub    -0x3c(%rbp),%edx
xor    %esi,%esi
call   1080 <pixelMessage@plt>
cmp    $0x0,%rax
je     13ad <embed+0x24d>
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
je     13a1 <embed+0x241>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    13bf <embed+0x25f>
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
sub    $0x18,%rsp
test   %rdx,%rdx
je     11c2 <embed+0x52>
mov    %rdi,%rbp
test   %rdi,%rdi
je     11c2 <embed+0x52>
mov    %rcx,%r15
test   %rcx,%rcx
je     11c2 <embed+0x52>
mov    %rdx,%r12
mov    %esi,%ebx
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
lea    0x7(%rbx),%eax
test   %ebx,%ebx
cmovns %ebx,%eax
sar    $0x3,%eax
cltq
lea    0x1(%r13),%rcx
cmp    %rax,%rcx
jbe    11e4 <embed+0x74>
lea    0xeae(%rip),%rdi
jmp    11c9 <embed+0x59>
lea    0xeb6(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1261 <embed+0xf1>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1070 <memcpy@plt>
test   %rax,%rax
je     126d <embed+0xfd>
test   %r13,%r13
mov    %r14,0x8(%rsp)
je     127e <embed+0x10e>
mov    %r15,0x10(%rsp)
xor    %r14d,%r14d
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
movsbl (%r12,%r15,1),%ebp
lea    0xdd4(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    (%rbx,%r14,1),%edx
mov    0x8(%rsp),%rdi
mov    %ebp,%esi
call   1090 <pixelMessage@plt>
test   %rax,%rax
jne    12b3 <embed+0x143>
add    $0x1,%r15
add    $0xfffffff8,%r14d
cmp    %r15,%r13
jne    1220 <embed+0xb0>
neg    %r14d
mov    0x10(%rsp),%r15
jmp    1281 <embed+0x111>
lea    0xdd6(%rip),%rdi
jmp    11c9 <embed+0x59>
lea    0xdb4(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
jmp    12c4 <embed+0x154>
xor    %r14d,%r14d
mov    %r14d,%eax
mov    0x8(%rsp),%r12
mov    %r12,%rdi
add    %rax,%rdi
sub    %r14d,%ebx
xor    %ebp,%ebp
xor    %esi,%esi
mov    %ebx,%edx
call   1090 <pixelMessage@plt>
test   %rax,%rax
je     12ce <embed+0x15e>
lea    0xd5b(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
jmp    12c4 <embed+0x154>
lea    0xd5c(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
jmp    11ce <embed+0x5e>
mov    %r12,(%r15)
jmp    11d3 <embed+0x63>

LLVM IR (-O0):
@.str = [22 x i8] c"NULL pointer passed.\0A\00"
@.str.1 = [18 x i8] c"Message wont fit\0A\00"
@.str.2 = [49 x i8] c"Couldnt allocate memory, possibly out of memory\0A\00"
@.str.3 = [23 x i8] c"Could not copy memory\0A\00"
@.str.4 = [4 x i8] c"%c\0A\00"
@.str.5 = [19 x i8] c"Failed to embed 1\0A\00"
@.str.6 = [19 x i8] c"Failed to embed 2\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
store i8 0, i8* %15
%16 = load i8*, i8** %6
%17 = icmp eq i8* %16, null
br i1 %17, label %24, label %18
18: ; preds = %4
%19 = load i8**, i8*** %9
%20 = icmp eq i8** %19, null
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i8*, i8** %8
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %21, %18, %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %122
26: ; preds = %21
%27 = load i8*, i8** %8
%28 = call i64 @strlen(i8* %27)
store i64 %28, i64* %14
%29 = load i32, i32* %7
%30 = sdiv i32 %29, 8
%31 = sext i32 %30 to i64
%32 = load i64, i64* %14
%33 = add i64 %32, 1
%34 = icmp ult i64 %31, %33
br i1 %34, label %35, label %37
35: ; preds = %26
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %122
37: ; preds = %26
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = mul i64 1, %39
%41 = trunc i64 %40 to i32
%42 = call i64 @malloc(i32 %41)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %11
%44 = load i8*, i8** %11
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %48
46: ; preds = %37
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %122
48: ; preds = %37
%49 = load i8*, i8** %11
%50 = load i8*, i8** %6
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = mul i64 1, %52
%54 = trunc i64 %53 to i32
%55 = call i32* @memcpy(i8* %49, i8* %50, i32 %54)
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %61
57: ; preds = %48
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%59 = load i8*, i8** %11
%60 = call i32 @free(i8* %59)
store i8* null, i8** %11
store i32 -1, i32* %5
br label %122
61: ; preds = %48
store i32 0, i32* %13
br label %62
62: ; preds = %98, %61
%63 = load i32, i32* %13
%64 = sext i32 %63 to i64
%65 = load i64, i64* %14
%66 = icmp ult i64 %64, %65
br i1 %66, label %67, label %101
67: ; preds = %62
%68 = load i8*, i8** %8
%69 = load i32, i32* %13
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i8, i8* %71
store i8 %72, i8* %15
%73 = load i8, i8* %15
%74 = sext i8 %73 to i32
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %74)
%76 = load i8*, i8** %11
%77 = load i32, i32* %12
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %76, i64 %78
store i8* %79, i8** %10
%80 = load i8*, i8** %11
%81 = load i8, i8* %15
%82 = load i32, i32* %7
%83 = load i32, i32* %12
%84 = sub nsw i32 %82, %83
%85 = call i64 @pixelMessage(i8* %80, i8 signext %81, i32 %84)
%86 = icmp ne i64 %85, 0
br i1 %86, label %87, label %95
87: ; preds = %67
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%89 = load i8*, i8** %11
%90 = icmp ne i8* %89, null
br i1 %90, label %91, label %94
91: ; preds = %87
%92 = load i8*, i8** %11
%93 = call i32 @free(i8* %92)
store i8* null, i8** %11
br label %94
94: ; preds = %91, %87
store i32 -1, i32* %5
br label %122
95: ; preds = %67
%96 = load i32, i32* %12
%97 = add nsw i32 %96, 8
store i32 %97, i32* %12
br label %98
98: ; preds = %95
%99 = load i32, i32* %13
%100 = add nsw i32 %99, 1
store i32 %100, i32* %13
br label %62
101: ; preds = %62
%102 = load i8*, i8** %11
%103 = load i32, i32* %12
%104 = sext i32 %103 to i64
%105 = getelementptr inbounds i8, i8* %102, i64 %104
%106 = load i32, i32* %7
%107 = load i32, i32* %12
%108 = sub nsw i32 %106, %107
%109 = call i64 @pixelMessage(i8* %105, i8 signext 0, i32 %108)
%110 = icmp ne i64 %109, 0
br i1 %110, label %111, label %119
111: ; preds = %101
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%113 = load i8*, i8** %11
%114 = icmp ne i8* %113, null
br i1 %114, label %115, label %118
115: ; preds = %111
%116 = load i8*, i8** %11
%117 = call i32 @free(i8* %116)
store i8* null, i8** %11
br label %118
118: ; preds = %115, %111
store i32 -1, i32* %5
br label %122
119: ; preds = %101
%120 = load i8*, i8** %11
%121 = load i8**, i8*** %9
store i8* %120, i8** %121
store i32 0, i32* %5
br label %122
122: ; preds = %119, %118, %94, %57, %46, %35, %24
%123 = load i32, i32* %5
ret i32 %123
}

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