#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*/ exit(int);
int /*<<< orphan*/ memcpy(unsigned char *, char *, unsigned int);
int /*<<< orphan*/ printf(char *);
unsigned int strlen(char *);
unsigned int writeAppData(unsigned char *appdata, char *valstr,
                          unsigned int loc) {
  if (loc + strlen(valstr) > 511) {
    printf("Attempted to write too much appdata, exiting...\n");
    exit(-1);
  }
  memcpy(appdata + loc, valstr, strlen(valstr));
  return loc + strlen(valstr);
}


x86 assembly (clang, -O0):
<writeAppData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
cmp    $0x1ff,%eax
jbe    118a <writeAppData+0x4a>
lea    0xe85(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x1c(%rbp),%eax
add    %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeAppData>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1040 <strlen@plt>
add    %r14d,%eax
cmp    $0x200,%eax
jae    118a <writeAppData+0x4a>
mov    %r14d,%eax
add    %rax,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [49 x i8] c"Attempted to write too much appdata, exiting...\0A\00"
define i32 @writeAppData(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%7 = load i32, i32* %6
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
%10 = add i32 %7, %9
%11 = icmp ugt i32 %10, 511
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = load i32, i32* %6
%18 = zext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8*, i8** %5
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = call i32 @memcpy(i8* %19, i8* %20, i32 %22)
%24 = load i32, i32* %6
%25 = load i8*, i8** %5
%26 = call i32 @strlen(i8* %25)
%27 = add i32 %24, %26
ret i32 %27
}

LLVM IR (-O2):
@str = [48 x i8] c"Attempted to write too much appdata, exiting...\00"
define i32 @writeAppData(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %4, %2
%6 = icmp ugt i32 %5, 511
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %3
%11 = zext i32 %2 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = tail call i32 @strlen(i8* %1)
%14 = tail call i32 @memcpy(i8* %12, i8* %1, i32 %13)
%15 = tail call i32 @strlen(i8* %1)
%16 = add i32 %15, %2
ret i32 %16
}