#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 vmbuf {
  int dummy;
};

/* Variables and functions */
int vmbuf_wavail(struct vmbuf *);
char *vmbuf_wloc(struct vmbuf *);
scalar_t__ vmbuf_wseek(struct vmbuf *, int);
int vmbuf_strcpy(struct vmbuf *vmbuf, const char *src) {
  do {
    char *w = vmbuf_wloc(vmbuf);
    char *dst = w;
    char *last = w + vmbuf_wavail(vmbuf);
    for (; dst < last && *src; *dst++ = *src++)
      ;
    if (0 > vmbuf_wseek(vmbuf, dst - w))
      return -1;
  } while (*src);
  *(char *)vmbuf_wloc(vmbuf) = 0;
  return 0;
}


x86 assembly (clang, -O0):
<vmbuf_strcpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <vmbuf_wloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <vmbuf_wavail@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x30(%rbp),%rcx
mov    %al,-0x39(%rbp)
jae    1199 <vmbuf_strcpy+0x69>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    11a9 <vmbuf_strcpy+0x79>
jmp    11d5 <vmbuf_strcpy+0xa5>
jmp    11ae <vmbuf_strcpy+0x7e>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
jmp    1176 <vmbuf_strcpy+0x46>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1050 <vmbuf_wseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    1205 <vmbuf_strcpy+0xd5>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <vmbuf_strcpy+0xfa>
jmp    120a <vmbuf_strcpy+0xda>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1140 <vmbuf_strcpy+0x10>
mov    -0x10(%rbp),%rdi
call   1030 <vmbuf_wloc@plt>
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmbuf_strcpy>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <vmbuf_wloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1040 <vmbuf_wavail@plt>
test   %eax,%eax
jle    1190 <vmbuf_strcpy+0x60>
movslq %eax,%rcx
mov    %r15,%rax
add    %rcx,%rax
mov    %r15,%rsi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rbx),%ecx
test   %cl,%cl
je     1193 <vmbuf_strcpy+0x63>
add    $0x1,%rbx
mov    %cl,(%rsi)
add    $0x1,%rsi
cmp    %rax,%rsi
jb     1170 <vmbuf_strcpy+0x40>
jmp    1193 <vmbuf_strcpy+0x63>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rsi
sub    %r15d,%esi
mov    %r14,%rdi
call   1050 <vmbuf_wseek@plt>
test   %rax,%rax
js     11b7 <vmbuf_strcpy+0x87>
cmpb   $0x0,(%rbx)
jne    1140 <vmbuf_strcpy+0x10>
mov    %r14,%rdi
call   1030 <vmbuf_wloc@plt>
movb   $0x0,(%rax)
xor    %eax,%eax
jmp    11bc <vmbuf_strcpy+0x8c>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.vmbuf = type { i32 }
define i32 @vmbuf_strcpy(%struct.vmbuf* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.vmbuf*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store %struct.vmbuf* %0, %struct.vmbuf** %4
store i8* %1, i8** %5
br label %9
9: ; preds = %48, %2
%10 = load %struct.vmbuf*, %struct.vmbuf** %4
%11 = call i8* @vmbuf_wloc(%struct.vmbuf* %10)
store i8* %11, i8** %6
%12 = load i8*, i8** %6
store i8* %12, i8** %7
%13 = load i8*, i8** %6
%14 = load %struct.vmbuf*, %struct.vmbuf** %4
%15 = call i32 @vmbuf_wavail(%struct.vmbuf* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8* %17, i8** %8
br label %18
18: ; preds = %30, %9
%19 = load i8*, i8** %7
%20 = load i8*, i8** %8
%21 = icmp ult i8* %19, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 0
br label %27
27: ; preds = %22, %18
%28 = phi i1 [ false, %18 ], [ %26, %22 ]
br i1 %28, label %29, label %36
29: ; preds = %27
br label %30
30: ; preds = %29
%31 = load i8*, i8** %5
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %5
%33 = load i8, i8* %31
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %7
store i8 %33, i8* %34
br label %18
36: ; preds = %27
%37 = load %struct.vmbuf*, %struct.vmbuf** %4
%38 = load i8*, i8** %7
%39 = load i8*, i8** %6
%40 = ptrtoint i8* %38 to i64
%41 = ptrtoint i8* %39 to i64
%42 = sub i64 %40, %41
%43 = trunc i64 %42 to i32
%44 = call i64 @vmbuf_wseek(%struct.vmbuf* %37, i32 %43)
%45 = icmp sgt i64 0, %44
br i1 %45, label %46, label %47
46: ; preds = %36
store i32 -1, i32* %3
br label %55
47: ; preds = %36
br label %48
48: ; preds = %47
%49 = load i8*, i8** %5
%50 = load i8, i8* %49
%51 = icmp ne i8 %50, 0
br i1 %51, label %9, label %52
52: ; preds = %48
%53 = load %struct.vmbuf*, %struct.vmbuf** %4
%54 = call i8* @vmbuf_wloc(%struct.vmbuf* %53)
store i8 0, i8* %54
store i32 0, i32* %3
br label %55
55: ; preds = %52, %46
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.vmbuf = type { i32 }
define i32 @vmbuf_strcpy(%struct.vmbuf* %0, i8* nocapture %1) {
br label %3
3: ; preds = %28, %2
%4 = phi i8* [ %1, %2 ], [ %20, %28 ]
%5 = tail call i8* @vmbuf_wloc(%struct.vmbuf* %0)
%6 = tail call i32 @vmbuf_wavail(%struct.vmbuf* %0)
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %5, i64 %7
%9 = icmp sgt i32 %6, 0
br i1 %9, label %10, label %19
10: ; preds = %3, %15
%11 = phi i8* [ %17, %15 ], [ %5, %3 ]
%12 = phi i8* [ %16, %15 ], [ %4, %3 ]
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %12, i64 1
%17 = getelementptr inbounds i8, i8* %11, i64 1
store i8 %13, i8* %11
%18 = icmp ult i8* %17, %8
br i1 %18, label %10, label %19
19: ; preds = %10, %15, %3
%20 = phi i8* [ %4, %3 ], [ %16, %15 ], [ %12, %10 ]
%21 = phi i8* [ %5, %3 ], [ %17, %15 ], [ %11, %10 ]
%22 = ptrtoint i8* %21 to i64
%23 = ptrtoint i8* %5 to i64
%24 = sub i64 %22, %23
%25 = trunc i64 %24 to i32
%26 = tail call i64 @vmbuf_wseek(%struct.vmbuf* %0, i32 %25)
%27 = icmp sgt i64 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %19
%29 = load i8, i8* %20
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %3
31: ; preds = %28
%32 = tail call i8* @vmbuf_wloc(%struct.vmbuf* %0)
store i8 0, i8* %32
br label %33
33: ; preds = %19, %31
%34 = phi i32 [ 0, %31 ], [ -1, %19 ]
ret i32 %34
}