#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 EmbedChar(int, int, char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ strlen(char *);
int embed(char *image_data, int data_length, char *message, char **encoded) {
  int idx;
  int mesidx;
  char *localdata;
  int msglen = 0;
  msglen = (int)strlen(message);
  if ((data_length / 8) < (msglen + 1)) {
    printf("Not enough room!!\n");
    return -1;
  }
  if (image_data == NULL) {
    printf("No data available.\n");
    return -1;
  }
  localdata = malloc(data_length * sizeof(char));
  if (localdata == NULL) {
    printf("Out of memory! \n");
    return -1;
  }
  memcpy(localdata, image_data, data_length * sizeof(char));
  for (idx = 0, mesidx = 0; mesidx < msglen; idx += 8, mesidx++) {
    if (EmbedChar(data_length - idx, message[mesidx], image_data + idx,
                  localdata + idx) == -1) {
      printf("Failed to embed a character... :(\n");
      free(localdata);
      localdata = NULL;
      return -1;
    }
  }
  if (EmbedChar(data_length - idx, 0x00, image_data + idx, localdata + idx) ==
      -1) {
    printf("Failed to embed a character... :(\n");
    free(localdata);
    localdata = NULL;
    return -1;
  }
  *encoded = localdata;
  localdata = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<embed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x3c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    11bb <embed+0x5b>
lea    0xe56(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <embed+0x1cf>
cmpq   $0x0,-0x10(%rbp)
jne    11de <embed+0x7e>
lea    0xe46(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <embed+0x1cf>
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1214 <embed+0xb4>
lea    0xe24(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <embed+0x1cf>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    12c0 <embed+0x160>
mov    -0x14(%rbp),%edi
sub    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
mov    -0x10(%rbp),%rdx
movslq -0x2c(%rbp),%rax
add    %rax,%rdx
mov    -0x38(%rbp),%rcx
movslq -0x2c(%rbp),%rax
add    %rax,%rcx
call   1080 <EmbedChar@plt>
cmp    $0xffffffff,%eax
jne    12a4 <embed+0x144>
lea    0xdb6(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <embed+0x1cf>
jmp    12a9 <embed+0x149>
mov    -0x2c(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1239 <embed+0xd9>
mov    -0x14(%rbp),%edi
sub    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rdx
movslq -0x2c(%rbp),%rax
add    %rax,%rdx
mov    -0x38(%rbp),%rcx
movslq -0x2c(%rbp),%rax
add    %rax,%rcx
xor    %esi,%esi
call   1080 <EmbedChar@plt>
cmp    $0xffffffff,%eax
jne    1315 <embed+0x1b5>
lea    0xd45(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    132f <embed+0x1cf>
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<embed>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %rdx,%rbx
mov    %esi,%r13d
mov    %rdi,%r15
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %rax,%rbp
lea    0x7(%r13),%eax
test   %r13d,%r13d
cmovns %r13d,%eax
sar    $0x3,%eax
cmp    %ebp,%eax
jle    1213 <embed+0xb3>
test   %r15,%r15
je     121c <embed+0xbc>
mov    %r13d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1225 <embed+0xc5>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
call   1060 <memcpy@plt>
test   %ebp,%ebp
mov    %r14,0x8(%rsp)
jle    1247 <embed+0xe7>
mov    %r12,0x10(%rsp)
mov    %ebp,%r12d
shl    $0x3,%r12
xor    %r14d,%r14d
mov    %r13d,%ebp
nopl   (%rax)
movsbl (%rbx),%esi
lea    (%r15,%r14,1),%rdx
mov    0x8(%rsp),%rax
lea    (%rax,%r14,1),%rcx
mov    %ebp,%edi
call   1080 <EmbedChar@plt>
cmp    $0xffffffff,%eax
je     1278 <embed+0x118>
add    $0x8,%r14
add    $0xfffffff8,%ebp
add    $0x1,%rbx
cmp    %r14,%r12
jne    11e0 <embed+0x80>
mov    0x10(%rsp),%r12
jmp    124a <embed+0xea>
lea    0xe2b(%rip),%rdi
jmp    122c <embed+0xcc>
lea    0xe0f(%rip),%rdi
jmp    122c <embed+0xcc>
lea    0xdf6(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
sub    %r14d,%r13d
mov    %r14d,%eax
add    %rax,%r15
mov    0x8(%rsp),%rbp
mov    %rbp,%rcx
add    %rax,%rcx
xor    %ebx,%ebx
mov    %r13d,%edi
xor    %esi,%esi
mov    %r15,%rdx
call   1080 <EmbedChar@plt>
cmp    $0xffffffff,%eax
je     128b <embed+0x12b>
mov    %rbp,(%r12)
jmp    1236 <embed+0xd6>
lea    0xd81(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%rdi
jmp    129a <embed+0x13a>
lea    0xd6e(%rip),%rdi
call   1040 <puts@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1231 <embed+0xd1>

LLVM IR (-O0):
@.str = [19 x i8] c"Not enough room!!\0A\00"
@.str.1 = [20 x i8] c"No data available.\0A\00"
@.str.2 = [17 x i8] c"Out of memory! \0A\00"
@.str.3 = [35 x i8] c"Failed to embed a character... :(\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 i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8** %3, i8*** %9
store i32 0, i32* %13
%14 = load i8*, i8** %8
%15 = call i64 @strlen(i8* %14)
%16 = trunc i64 %15 to i32
store i32 %16, i32* %13
%17 = load i32, i32* %7
%18 = sdiv i32 %17, 8
%19 = load i32, i32* %13
%20 = add nsw i32 %19, 1
%21 = icmp slt i32 %18, %20
br i1 %21, label %22, label %24
22: ; preds = %4
%23 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %102
24: ; preds = %4
%25 = load i8*, i8** %6
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %102
29: ; preds = %24
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = mul i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = call i8* @malloc(i32 %33)
store i8* %34, i8** %12
%35 = load i8*, i8** %12
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %102
39: ; preds = %29
%40 = load i8*, i8** %12
%41 = load i8*, i8** %6
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = mul i64 %43, 1
%45 = trunc i64 %44 to i32
%46 = call i32 @memcpy(i8* %40, i8* %41, i32 %45)
store i32 0, i32* %10
store i32 0, i32* %11
br label %47
47: ; preds = %76, %39
%48 = load i32, i32* %11
%49 = load i32, i32* %13
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %81
51: ; preds = %47
%52 = load i32, i32* %7
%53 = load i32, i32* %10
%54 = sub nsw i32 %52, %53
%55 = load i8*, i8** %8
%56 = load i32, i32* %11
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = load i8*, i8** %6
%62 = load i32, i32* %10
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i8*, i8** %12
%66 = load i32, i32* %10
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
%69 = call i32 @EmbedChar(i32 %54, i32 %60, i8* %64, i8* %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %75
71: ; preds = %51
%72 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%73 = load i8*, i8** %12
%74 = call i32 @free(i8* %73)
store i8* null, i8** %12
store i32 -1, i32* %5
br label %102
75: ; preds = %51
br label %76
76: ; preds = %75
%77 = load i32, i32* %10
%78 = add nsw i32 %77, 8
store i32 %78, i32* %10
%79 = load i32, i32* %11
%80 = add nsw i32 %79, 1
store i32 %80, i32* %11
br label %47
81: ; preds = %47
%82 = load i32, i32* %7
%83 = load i32, i32* %10
%84 = sub nsw i32 %82, %83
%85 = load i8*, i8** %6
%86 = load i32, i32* %10
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %85, i64 %87
%89 = load i8*, i8** %12
%90 = load i32, i32* %10
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds i8, i8* %89, i64 %91
%93 = call i32 @EmbedChar(i32 %84, i32 0, i8* %88, i8* %92)
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %99
95: ; preds = %81
%96 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%97 = load i8*, i8** %12
%98 = call i32 @free(i8* %97)
store i8* null, i8** %12
store i32 -1, i32* %5
br label %102
99: ; preds = %81
%100 = load i8*, i8** %12
%101 = load i8**, i8*** %9
store i8* %100, i8** %101
store i8* null, i8** %12
store i32 0, i32* %5
br label %102
102: ; preds = %99, %95, %71, %37, %27, %22
%103 = load i32, i32* %5
ret i32 %103
}

LLVM IR (-O2):
@str.4 = [34 x i8] c"Failed to embed a character... :(\00"
@str.5 = [16 x i8] c"Out of memory! \00"
@str.6 = [19 x i8] c"No data available.\00"
@str.7 = [18 x i8] c"Not enough room!!\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 = trunc i64 %5 to i32
%7 = sdiv i32 %1, 8
%8 = icmp sgt i32 %7, %6
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
br label %58
11: ; preds = %4
%12 = icmp eq i8* %0, null
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %58
15: ; preds = %11
%16 = tail call i8* @malloc(i32 %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
br label %58
20: ; preds = %15
%21 = tail call i32 @memcpy(i8* %16, i8* %0, i32 %1)
%22 = icmp sgt i32 %6, 0
br i1 %22, label %23, label %46
23: ; preds = %20
%24 = and i64 %5, 4294967295
br label %25
25: ; preds = %23, %40
%26 = phi i64 [ 0, %23 ], [ %41, %40 ]
%27 = phi i64 [ 0, %23 ], [ %42, %40 ]
%28 = trunc i64 %26 to i32
%29 = sub nsw i32 %1, %28
%30 = getelementptr inbounds i8, i8* %2, i64 %27
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = getelementptr inbounds i8, i8* %0, i64 %26
%34 = getelementptr inbounds i8, i8* %16, i64 %26
%35 = tail call i32 @EmbedChar(i32 %29, i32 %32, i8* %33, i8* %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %25
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.4, i64 0, i64 0))
%39 = tail call i32 @free(i8* %16)
br label %58
40: ; preds = %25
%41 = add nuw nsw i64 %26, 8
%42 = add nuw nsw i64 %27, 1
%43 = icmp eq i64 %42, %24
br i1 %43, label %44, label %25
44: ; preds = %40
%45 = trunc i64 %41 to i32
br label %46
46: ; preds = %44, %20
%47 = phi i32 [ 0, %20 ], [ %45, %44 ]
%48 = sub nsw i32 %1, %47
%49 = zext i32 %47 to i64
%50 = getelementptr inbounds i8, i8* %0, i64 %49
%51 = getelementptr inbounds i8, i8* %16, i64 %49
%52 = tail call i32 @EmbedChar(i32 %48, i32 0, i8* %50, i8* %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.4, i64 0, i64 0))
%56 = tail call i32 @free(i8* %16)
br label %58
57: ; preds = %46
store i8* %16, i8** %3
br label %58
58: ; preds = %57, %54, %37, %18, %13, %9
%59 = phi i32 [ -1, %9 ], [ -1, %13 ], [ -1, %18 ], [ -1, %37 ], [ -1, %54 ], [ 0, %57 ]
ret i32 %59
}