#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 */
struct buffer {
  int dummy;
};

/* Variables and functions */
char *buffer_cstr(struct buffer *);
int /*<<< orphan*/ buffer_end(struct buffer *);
scalar_t__ buffer_insert(struct buffer **, int, char const *, int);
struct buffer *buffer_new(int);
scalar_t__ buffer_pushc(struct buffer **, char const);
int /*<<< orphan*/ printf(char *, char *);
int test_new_insert(void) {
  const char str[] = "this is madness. madness is longer than 32 chars.";
  struct buffer *b = buffer_new(32);
  size_t i;
  int ret = -1;
  if (b == NULL)
    return -1;
  for (i = 0; i < sizeof str - 1; i++)
    if (buffer_pushc(&b, str[i]) < 0)
      goto error;
  if (buffer_insert(&b, 12, str, sizeof str - 1) < 0)
    goto error;
  printf("<%s>\n", buffer_cstr(b));
  ret = 1;
error:
  buffer_end(b);
  return ret;
}


x86 assembly (clang, -O0):
<test_new_insert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    -0x40(%rbp),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x32,%edx
call   1090 <memcpy@plt>
mov    $0x20,%edi
call   1040 <buffer_new@plt>
mov    %rax,-0x48(%rbp)
movl   $0xffffffff,-0x54(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11b9 <test_new_insert+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1257 <test_new_insert+0xe7>
movq   $0x0,-0x50(%rbp)
cmpq   $0x31,-0x50(%rbp)
jae    1203 <test_new_insert+0x93>
mov    -0x50(%rbp),%rax
lea    -0x48(%rbp),%rdi
movsbl -0x40(%rbp,%rax,1),%esi
call   1070 <buffer_pushc@plt>
cmp    $0x0,%rax
jge    11ed <test_new_insert+0x7d>
jmp    1248 <test_new_insert+0xd8>
jmp    11f2 <test_new_insert+0x82>
mov    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
jmp    11c1 <test_new_insert+0x51>
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rdi
mov    $0xc,%esi
mov    $0x31,%ecx
call   1080 <buffer_insert@plt>
cmp    $0x0,%rax
jge    1229 <test_new_insert+0xb9>
jmp    1248 <test_new_insert+0xd8>
mov    -0x48(%rbp),%rdi
call   1030 <buffer_cstr@plt>
mov    %rax,%rsi
lea    0xdf6(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x54(%rbp)
mov    -0x48(%rbp),%rdi
call   1050 <buffer_end@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_new_insert>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
movaps 0xeb1(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe95(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe79(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x2e,0x40(%rsp)
mov    $0x20,%edi
call   1040 <buffer_new@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     11ee <test_new_insert+0x8e>
lea    0x8(%rsp),%rdi
mov    $0x74,%esi
call   1070 <buffer_pushc@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     1233 <test_new_insert+0xd3>
mov    $0x1,%ebp
lea    0x8(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
cmp    $0x31,%rbp
je     11f6 <test_new_insert+0x96>
movsbl 0x10(%rsp,%rbp,1),%esi
mov    %rbx,%rdi
call   1070 <buffer_pushc@plt>
add    $0x1,%rbp
test   %rax,%rax
jns    11d0 <test_new_insert+0x70>
jmp    1233 <test_new_insert+0xd3>
mov    $0xffffffff,%r14d
jmp    123d <test_new_insert+0xdd>
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rdx
mov    $0xc,%esi
mov    $0x31,%ecx
call   1080 <buffer_insert@plt>
test   %rax,%rax
js     1233 <test_new_insert+0xd3>
mov    0x8(%rsp),%rdi
call   1030 <buffer_cstr@plt>
lea    0xe0d(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    $0x1,%r14d
mov    0x8(%rsp),%rdi
call   1050 <buffer_end@plt>
mov    %r14d,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
@__const.test_new_insert.str = [50 x i8] c"this is madness. madness is longer than 32 chars.\00"
@.str = [6 x i8] c"<%s>\0A\00"
define i32 @test_new_insert() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca %struct.buffer*
%4 = alloca i64
%5 = alloca i32
%6 = bitcast [50 x i8]* %2 to i8*
%7 = call %struct.buffer* @buffer_new(i32 32)
store %struct.buffer* %7, %struct.buffer** %3
store i32 -1, i32* %5
%8 = load %struct.buffer*, %struct.buffer** %3
%9 = icmp eq %struct.buffer* %8, null
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %39
11: ; preds = %0
store i64 0, i64* %4
br label %12
12: ; preds = %23, %11
%13 = load i64, i64* %4
%14 = icmp ult i64 %13, 49
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = load i64, i64* %4
%17 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = call i64 @buffer_pushc(%struct.buffer** %3, i8 signext %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %35
22: ; preds = %15
br label %23
23: ; preds = %22
%24 = load i64, i64* %4
%25 = add i64 %24, 1
store i64 %25, i64* %4
br label %12
26: ; preds = %12
%27 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%28 = call i64 @buffer_insert(%struct.buffer** %3, i32 12, i8* %27, i32 49)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %26
br label %35
31: ; preds = %26
%32 = load %struct.buffer*, %struct.buffer** %3
%33 = call i8* @buffer_cstr(%struct.buffer* %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %33)
store i32 1, i32* %5
br label %35
35: ; preds = %31, %30, %21
%36 = load %struct.buffer*, %struct.buffer** %3
%37 = call i32 @buffer_end(%struct.buffer* %36)
%38 = load i32, i32* %5
store i32 %38, i32* %1
br label %39
39: ; preds = %35, %10
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
@__const.test_new_insert.str = [50 x i8] c"this is madness. madness is longer than 32 chars.\00"
@.str = [6 x i8] c"<%s>\0A\00"
define i32 @test_new_insert() {
%1 = alloca [50 x i8]
%2 = alloca %struct.buffer*
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.buffer** %2 to i8*
%5 = tail call %struct.buffer* @buffer_new(i32 32)
store %struct.buffer* %5, %struct.buffer** %2
%6 = icmp eq %struct.buffer* %5, null
br i1 %6, label %30, label %7
7: ; preds = %0
%8 = call i64 @buffer_pushc(%struct.buffer** %2, i8 signext 116)
%9 = icmp slt i64 %8, 0
br i1 %9, label %26, label %10
10: ; preds = %7, %14
%11 = phi i64 [ %12, %14 ], [ 0, %7 ]
%12 = add nuw nsw i64 %11, 1
%13 = icmp eq i64 %12, 49
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 %12
%16 = load i8, i8* %15
%17 = call i64 @buffer_pushc(%struct.buffer** %2, i8 signext %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %26, label %10
19: ; preds = %10
%20 = call i64 @buffer_insert(%struct.buffer** %2, i32 12, i8* %3, i32 49)
%21 = icmp slt i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = load %struct.buffer*, %struct.buffer** %2
%24 = call i8* @buffer_cstr(%struct.buffer* %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %14, %7, %19, %22
%27 = phi i32 [ -1, %19 ], [ 1, %22 ], [ -1, %7 ], [ -1, %14 ]
%28 = load %struct.buffer*, %struct.buffer** %2
%29 = call i32 @buffer_end(%struct.buffer* %28)
br label %30
30: ; preds = %0, %26
%31 = phi i32 [ %27, %26 ], [ -1, %0 ]
ret i32 %31
}