#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_a(char *, char *, int);
int /*<<< orphan*/ printf(char *, char *);
void bufwrite_a(char *p, int n) {
  static char buffer[1024];
  static char *bufptr = &buffer[0];
  while (n > 0) {
    int k, rem;
    if (bufptr == &buffer[1024])
      ;
    rem = 1024 - (bufptr - buffer);
    k = n > rem ? rem : n;
    memcpy_a(bufptr, p, k);
    bufptr += k;
    p += k;
    n -= k;
  }
  printf("buffer:%s\n", &buffer[0]);
}


x86 assembly (clang, -O0):
<bufwrite_a>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11e2 <bufwrite_a+0xc2>
lea    0x2f10(%rip),%rax
add    $0x400,%rax
cmp    %rax,0x2ee3(%rip)
jne    1158 <bufwrite_a+0x38>
jmp    1158 <bufwrite_a+0x38>
mov    0x2ed1(%rip),%rcx
lea    0x2eea(%rip),%rax
sub    %rax,%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    118b <bufwrite_a+0x6b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1191 <bufwrite_a+0x71>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e92(%rip),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <memcpy_a@plt>
mov    -0x10(%rbp),%ecx
mov    0x2e7c(%rip),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,0x2e6f(%rip)
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    112f <bufwrite_a+0xf>
lea    0xe17(%rip),%rdi
lea    0x2e60(%rip),%rsi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bufwrite_a>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1175 <bufwrite_a+0x55>
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2ef9(%rip),%rdi
lea    0x2f12(%rip),%r15
xchg   %ax,%ax
mov    %r15d,%ebp
sub    %edi,%ebp
add    $0x400,%ebp
cmp    %ebp,%ebx
cmovle %ebx,%ebp
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy_a@plt>
movslq %ebp,%rax
mov    0x2ecc(%rip),%rdi
add    %rax,%rdi
mov    %rdi,0x2ec2(%rip)
add    %rax,%r14
sub    %eax,%ebx
jg     1140 <bufwrite_a+0x20>
lea    0xe84(%rip),%rdi
lea    0x2ecd(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@bufwrite_a.buffer = internal global [1024 x i8] zeroinitializer
@bufwrite_a.bufptr = internal global i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @bufwrite_a.buffer, i32 0, i32 0)
@.str = [11 x i8] c"buffer:%s\0A\00"
define void @bufwrite_a(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %7
7: ; preds = %27, %2
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %44
10: ; preds = %7
%11 = load i8*, i8** @bufwrite_a.bufptr
%12 = icmp eq i8* %11, getelementptr inbounds ([1024 x i8], [1024 x i8]* @bufwrite_a.buffer, i64 1, i64 0)
br i1 %12, label %13, label %14
13: ; preds = %10
br label %14
14: ; preds = %13, %10
%15 = load i8*, i8** @bufwrite_a.bufptr
%16 = ptrtoint i8* %15 to i64
%17 = sub i64 %16, ptrtoint ([1024 x i8]* @bufwrite_a.buffer to i64)
%18 = sub nsw i64 1024, %17
%19 = trunc i64 %18 to i32
store i32 %19, i32* %6
%20 = load i32, i32* %4
%21 = load i32, i32* %6
%22 = icmp sgt i32 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = load i32, i32* %6
br label %27
25: ; preds = %14
%26 = load i32, i32* %4
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
store i32 %28, i32* %5
%29 = load i8*, i8** @bufwrite_a.bufptr
%30 = load i8*, i8** %3
%31 = load i32, i32* %5
%32 = call i32 @memcpy_a(i8* %29, i8* %30, i32 %31)
%33 = load i32, i32* %5
%34 = load i8*, i8** @bufwrite_a.bufptr
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** @bufwrite_a.bufptr
%37 = load i32, i32* %5
%38 = load i8*, i8** %3
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %3
%41 = load i32, i32* %5
%42 = load i32, i32* %4
%43 = sub nsw i32 %42, %41
store i32 %43, i32* %4
br label %7
44: ; preds = %7
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @bufwrite_a.buffer, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@bufwrite_a.buffer = internal global [1024 x i8] zeroinitializer
@bufwrite_a.bufptr = internal unnamed_addr global i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @bufwrite_a.buffer, i64 0, i64 0)
@.str = [11 x i8] c"buffer:%s\0A\00"
define void @bufwrite_a(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %22
4: ; preds = %2
%5 = load i8*, i8** @bufwrite_a.bufptr
br label %6
6: ; preds = %4, %6
%7 = phi i8* [ %18, %6 ], [ %5, %4 ]
%8 = phi i8* [ %19, %6 ], [ %0, %4 ]
%9 = phi i32 [ %20, %6 ], [ %1, %4 ]
%10 = ptrtoint i8* %7 to i64
%11 = trunc i64 %10 to i32
%12 = sub i32 trunc (i64 add (i64 ptrtoint ([1024 x i8]* @bufwrite_a.buffer to i64), i64 1024) to i32), %11
%13 = icmp sgt i32 %9, %12
%14 = select i1 %13, i32 %12, i32 %9
%15 = tail call i32 @memcpy_a(i8* %7, i8* %8, i32 %14)
%16 = load i8*, i8** @bufwrite_a.bufptr
%17 = sext i32 %14 to i64
%18 = getelementptr inbounds i8, i8* %16, i64 %17
store i8* %18, i8** @bufwrite_a.bufptr
%19 = getelementptr inbounds i8, i8* %8, i64 %17
%20 = sub nsw i32 %9, %14
%21 = icmp sgt i32 %20, 0
br i1 %21, label %6, label %22
22: ; preds = %6, %2
%23 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @bufwrite_a.buffer, i64 0, i64 0))
ret void
}