#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*/ memcpy(char *, char *, int);
int prepend_integer(char *, unsigned int, int);
int strlen(char *);
void finish_buffer(char *tag, char **bufp, unsigned int *sizep) {
  int taglen;
  int offset;
  char *buf = *bufp;
  unsigned int size = *sizep;
  offset = prepend_integer(buf, size - (40), (40));
  taglen = strlen(tag);
  offset -= taglen;
  buf += offset;
  size -= offset;
  memcpy(buf, tag, taglen);
  *bufp = buf;
  *sizep = size;
}


x86 assembly (clang, -O0):
<finish_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
sub    $0x28,%esi
mov    $0x28,%edx
call   1050 <prepend_integer@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x2c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<finish_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    (%rsi),%rbx
mov    (%rdx),%r13d
lea    -0x28(%r13),%esi
mov    %rbx,%rdi
mov    $0x28,%edx
call   1050 <prepend_integer@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1030 <strlen@plt>
sub    %eax,%ebp
movslq %ebp,%rcx
add    %rcx,%rbx
sub    %ecx,%r13d
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    %rbx,(%r15)
mov    %r13d,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @finish_buffer(i8* %0, i8** %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* %2, i32** %6
%11 = load i8**, i8*** %5
%12 = load i8*, i8** %11
store i8* %12, i8** %9
%13 = load i32*, i32** %6
%14 = load i32, i32* %13
store i32 %14, i32* %10
%15 = load i8*, i8** %9
%16 = load i32, i32* %10
%17 = sub i32 %16, 40
%18 = call i32 @prepend_integer(i8* %15, i32 %17, i32 40)
store i32 %18, i32* %8
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = sub nsw i32 %22, %21
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = load i8*, i8** %9
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8* %27, i8** %9
%28 = load i32, i32* %8
%29 = load i32, i32* %10
%30 = sub i32 %29, %28
store i32 %30, i32* %10
%31 = load i8*, i8** %9
%32 = load i8*, i8** %4
%33 = load i32, i32* %7
%34 = call i32 @memcpy(i8* %31, i8* %32, i32 %33)
%35 = load i8*, i8** %9
%36 = load i8**, i8*** %5
store i8* %35, i8** %36
%37 = load i32, i32* %10
%38 = load i32*, i32** %6
store i32 %37, i32* %38
ret void
}

LLVM IR (-O2):
define void @finish_buffer(i8* %0, i8** nocapture %1, i32* nocapture %2) {
%4 = load i8*, i8** %1
%5 = load i32, i32* %2
%6 = add i32 %5, -40
%7 = tail call i32 @prepend_integer(i8* %4, i32 %6, i32 40)
%8 = tail call i32 @strlen(i8* %0)
%9 = sub nsw i32 %7, %8
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %4, i64 %10
%12 = sub i32 %5, %9
%13 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %8)
store i8* %11, i8** %1
store i32 %12, i32* %2
ret void
}