#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 */
typedef int uint8_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ int16ToBuf(int, int *);
int /*<<< orphan*/ memcpy(int *, char *, int);
int /*<<< orphan*/ memmove(int *, int *, int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int insertStructEntry(uint8_t **pnt, uint8_t *end, int length, int type,
                      char *label) {
  int tagLen = strlen(label);
  if (*pnt + tagLen + 3 >= end) {
    printf("Error, running out of buffer space while adding int32 to buffer\n");
    exit(0);
  }
  printf("length=%d\n", length);
  printf("Shifting %d steps\n", tagLen + 3);
  memmove((*pnt) + tagLen + 3, *pnt, length);
  **pnt = type;
  *pnt += 1;
  int16ToBuf(tagLen, *pnt);
  *pnt += 2;
  memcpy(*pnt, label, tagLen);
  *pnt += tagLen;
  return tagLen + 3;
}


x86 assembly (clang, -O0):
<insertStructEntry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
add    $0xc,%rax
cmp    -0x10(%rbp),%rax
jb     11bb <insertStructEntry+0x5b>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%esi
add    $0x3,%esi
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
movslq -0x24(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
add    $0xc,%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
call   1060 <memmove@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x4,%rcx
mov    %rcx,(%rax)
mov    -0x24(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
call   1080 <int16ToBuf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
add    $0x8,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x24(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x3,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertStructEntry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %ecx,0x4(%rsp)
mov    %edx,%r13d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %r8,%rdi
call   1040 <strlen@plt>
mov    (%rbx),%rcx
movslq %eax,%r15
lea    (%rcx,%r15,4),%rax
add    $0xc,%rax
cmp    %rbp,%rax
jae    122a <insertStructEntry+0xba>
lea    0xe4f(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x3(%r15),%ebp
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%rsi
lea    (%rsi,%r15,4),%rdi
add    $0xc,%rdi
mov    %r15,%r12
shl    $0x2,%r12
mov    %r13d,%edx
call   1070 <memmove@plt>
mov    (%rbx),%rsi
mov    0x4(%rsp),%eax
mov    %eax,(%rsi)
add    $0x4,%rsi
mov    %rsi,(%rbx)
mov    %r15d,%edi
call   1090 <int16ToBuf@plt>
mov    (%rbx),%rdi
add    $0x8,%rdi
mov    %rdi,(%rbx)
mov    %r14,%rsi
mov    %r15d,%edx
call   1060 <memcpy@plt>
add    %r12,(%rbx)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [65 x i8] c"Error, running out of buffer space while adding int32 to buffer\0A\00"
@.str.1 = [11 x i8] c"length=%d\0A\00"
@.str.2 = [19 x i8] c"Shifting %d steps\0A\00"
define i32 @insertStructEntry(i32** %0, i32* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32** %0, i32*** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%12 = load i8*, i8** %10
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %11
%14 = load i32**, i32*** %6
%15 = load i32*, i32** %14
%16 = load i32, i32* %11
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %15, i64 %17
%19 = getelementptr inbounds i32, i32* %18, i64 3
%20 = load i32*, i32** %7
%21 = icmp uge i32* %19, %20
br i1 %21, label %22, label %25
22: ; preds = %5
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %5
%26 = load i32, i32* %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %11
%29 = add nsw i32 %28, 3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32**, i32*** %6
%32 = load i32*, i32** %31
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
%36 = getelementptr inbounds i32, i32* %35, i64 3
%37 = load i32**, i32*** %6
%38 = load i32*, i32** %37
%39 = load i32, i32* %8
%40 = call i32 @memmove(i32* %36, i32* %38, i32 %39)
%41 = load i32, i32* %9
%42 = load i32**, i32*** %6
%43 = load i32*, i32** %42
store i32 %41, i32* %43
%44 = load i32**, i32*** %6
%45 = load i32*, i32** %44
%46 = getelementptr inbounds i32, i32* %45, i64 1
store i32* %46, i32** %44
%47 = load i32, i32* %11
%48 = load i32**, i32*** %6
%49 = load i32*, i32** %48
%50 = call i32 @int16ToBuf(i32 %47, i32* %49)
%51 = load i32**, i32*** %6
%52 = load i32*, i32** %51
%53 = getelementptr inbounds i32, i32* %52, i64 2
store i32* %53, i32** %51
%54 = load i32**, i32*** %6
%55 = load i32*, i32** %54
%56 = load i8*, i8** %10
%57 = load i32, i32* %11
%58 = call i32 @memcpy(i32* %55, i8* %56, i32 %57)
%59 = load i32, i32* %11
%60 = load i32**, i32*** %6
%61 = load i32*, i32** %60
%62 = sext i32 %59 to i64
%63 = getelementptr inbounds i32, i32* %61, i64 %62
store i32* %63, i32** %60
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 3
ret i32 %65
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"length=%d\0A\00"
@.str.2 = [19 x i8] c"Shifting %d steps\0A\00"
@str = [64 x i8] c"Error, running out of buffer space while adding int32 to buffer\00"
define i32 @insertStructEntry(i32** nocapture %0, i32* readnone %1, i32 %2, i32 %3, i8* %4) {
%6 = tail call i32 @strlen(i8* %4)
%7 = load i32*, i32** %0
%8 = sext i32 %6 to i64
%9 = getelementptr inbounds i32, i32* %7, i64 %8
%10 = getelementptr inbounds i32, i32* %9, i64 3
%11 = icmp ult i32* %10, %1
br i1 %11, label %15, label %12
12: ; preds = %5
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 0)
unreachable
15: ; preds = %5
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%17 = add nsw i32 %6, 3
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i32*, i32** %0
%20 = getelementptr inbounds i32, i32* %19, i64 %8
%21 = getelementptr inbounds i32, i32* %20, i64 3
%22 = tail call i32 @memmove(i32* %21, i32* %19, i32 %2)
%23 = load i32*, i32** %0
store i32 %3, i32* %23
%24 = getelementptr inbounds i32, i32* %23, i64 1
store i32* %24, i32** %0
%25 = tail call i32 @int16ToBuf(i32 %6, i32* %24)
%26 = load i32*, i32** %0
%27 = getelementptr inbounds i32, i32* %26, i64 2
store i32* %27, i32** %0
%28 = tail call i32 @memcpy(i32* %27, i8* %4, i32 %6)
%29 = load i32*, i32** %0
%30 = getelementptr inbounds i32, i32* %29, i64 %8
store i32* %30, i32** %0
ret i32 %17
}