#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 /*<<< orphan*/ s;

/* Variables and functions */
int /*<<< orphan*/ buffer_clear(char **, char *);
int /*<<< orphan*/ buffer_close(int, char *);
int /*<<< orphan*/ buffer_create(int, int *, char **);
int /*<<< orphan*/ insert_item(char *, char **, char *);
int /*<<< orphan*/ memset(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, const char **argv) {
  char *shared_start, *shared_buffer;
  int shared_fileno;
  buffer_create(32768, &shared_fileno, &shared_start);
  buffer_clear(&shared_buffer, shared_start);
  int i = 0;
  char s[30];
  while (i < 1000) {
    memset(s, sizeof(s), 0);
    sprintf(s, "Hello world #%d", i);
    insert_item(s, &shared_buffer, shared_start);
    printf("shared_buffer = %p\n", shared_buffer);
    i = i + 1;
  }
  buffer_close(shared_fileno, shared_start);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x8000,%edi
lea    -0x24(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1090 <buffer_create@plt>
mov    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1070 <buffer_clear@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x3e8,-0x28(%rbp)
jge    120b <main+0x9b>
lea    -0x50(%rbp),%rdi
mov    $0x1e,%esi
xor    %edx,%edx
call   1050 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%edx
lea    0xe29(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rsi
call   1060 <insert_item@plt>
mov    -0x20(%rbp),%rsi
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11ac <main+0x3c>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <buffer_close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
lea    0x14(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    $0x8000,%edi
call   1090 <buffer_create@plt>
mov    0x8(%rsp),%rsi
lea    0x18(%rsp),%r14
mov    %r14,%rdi
call   1070 <buffer_clear@plt>
xor    %ebp,%ebp
lea    0x20(%rsp),%rbx
lea    0xe50(%rip),%r15
lea    0xe59(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1e,%esi
xor    %edx,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <insert_item@plt>
mov    0x18(%rsp),%rsi
mov    %r12,%rdi
call   1040 <printf@plt>
add    $0x1,%ebp
cmp    $0x3e8,%ebp
jne    11c0 <main+0x50>
mov    0x14(%rsp),%edi
mov    0x8(%rsp),%rsi
call   1030 <buffer_close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Hello world #%d\00"
@.str.1 = [20 x i8] c"shared_buffer = %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [30 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @buffer_create(i32 32768, i32* %8, i8** %6)
%12 = load i8*, i8** %6
%13 = call i32 @buffer_clear(i8** %7, i8* %12)
store i32 0, i32* %9
br label %14
14: ; preds = %17, %2
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 1000
br i1 %16, label %17, label %30
17: ; preds = %14
%18 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 30, i32 0)
%20 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%21 = load i32, i32* %9
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [30 x i8], [30 x i8]* %10, i64 0, i64 0
%24 = load i8*, i8** %6
%25 = call i32 @insert_item(i8* %23, i8** %7, i8* %24)
%26 = load i8*, i8** %7
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %9
%29 = add nsw i32 %28, 1
store i32 %29, i32* %9
br label %14
30: ; preds = %14
%31 = load i32, i32* %8
%32 = load i8*, i8** %6
%33 = call i32 @buffer_close(i32 %31, i8* %32)
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"Hello world #%d\00"
@.str.1 = [20 x i8] c"shared_buffer = %p\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [30 x i8]
%7 = bitcast i8** %3 to i8*
%8 = bitcast i8** %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = call i32 @buffer_create(i32 32768, i32* %5, i8** %3)
%11 = load i8*, i8** %3
%12 = call i32 @buffer_clear(i8** %4, i8* %11)
%13 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
br label %14
14: ; preds = %2, %14
%15 = phi i32 [ 0, %2 ], [ %22, %14 ]
%16 = call i32 @memset(i8* %13, i32 30, i32 0)
%17 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %15)
%18 = load i8*, i8** %3
%19 = call i32 @insert_item(i8* %13, i8** %4, i8* %18)
%20 = load i8*, i8** %4
%21 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = add nuw nsw i32 %15, 1
%23 = icmp eq i32 %22, 1000
br i1 %23, label %24, label %14
24: ; preds = %14
%25 = load i32, i32* %5
%26 = load i8*, i8** %3
%27 = call i32 @buffer_close(i32 %25, i8* %26)
ret i32 0
}