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

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ gbuf_delete(struct gbuf *, size_t);
int /*<<< orphan*/ gbuf_move_to(struct gbuf *, size_t);
size_t gbuf_offset(struct gbuf *);
int /*<<< orphan*/ gbuf_retract(struct gbuf *, size_t);
scalar_t__ validate_range(struct gbuf *, size_t, size_t);
int gbuf_erase(struct gbuf *buf, size_t offset, size_t size) {
  size_t lsize, rsize;
  assert(buf != NULL);
  if (validate_range(buf, offset, size)) {
    return -1;
  }
  if (size == 0) {
    return 0;
  }
  if (offset + size < gbuf_offset(buf)) {
    (void)gbuf_move_to(buf, offset + size);
    rsize = 0;
    lsize = size;
  } else if (offset > gbuf_offset(buf)) {
    (void)gbuf_move_to(buf, offset);
    rsize = size;
    lsize = 0;
  } else {
    lsize = gbuf_offset(buf) - offset;
    rsize = size - lsize;
  }
  if (lsize > 0) {
    (void)gbuf_retract(buf, lsize);
  }
  if (rsize > 0) {
    (void)gbuf_delete(buf, rsize);
  }
  return 0;
}


x86 assembly (clang, -O0):
<gbuf_erase>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <validate_range@plt>
cmp    $0x0,%rax
je     11ad <gbuf_erase+0x4d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ab <gbuf_erase+0x14b>
cmpq   $0x0,-0x20(%rbp)
jne    11c4 <gbuf_erase+0x64>
movl   $0x0,-0x4(%rbp)
jmp    12ab <gbuf_erase+0x14b>
mov    -0x18(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <gbuf_offset@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jae    120f <gbuf_erase+0xaf>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0x20(%rbp),%rsi
call   1060 <gbuf_move_to@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1274 <gbuf_erase+0x114>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <gbuf_offset@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
jbe    1252 <gbuf_erase+0xf2>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <gbuf_move_to@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
jmp    126f <gbuf_erase+0x10f>
mov    -0x10(%rbp),%rdi
call   1030 <gbuf_offset@plt>
sub    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1274 <gbuf_erase+0x114>
cmpq   $0x0,-0x28(%rbp)
jbe    128c <gbuf_erase+0x12c>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <gbuf_retract@plt>
cmpq   $0x0,-0x30(%rbp)
jbe    12a4 <gbuf_erase+0x144>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1070 <gbuf_delete@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gbuf_erase>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1040 <validate_range@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jne    120c <gbuf_erase+0xac>
xor    %r14d,%r14d
test   %r15,%r15
je     120c <gbuf_erase+0xac>
lea    (%r15,%rbp,1),%r12
mov    %rbx,%rdi
call   1030 <gbuf_offset@plt>
mov    %rbx,%rdi
cmp    %rax,%r12
jae    11c1 <gbuf_erase+0x61>
mov    %r12,%rsi
call   1060 <gbuf_move_to@plt>
xor    %r12d,%r12d
jmp    11ee <gbuf_erase+0x8e>
call   1030 <gbuf_offset@plt>
mov    %rbx,%rdi
cmp    %rbp,%rax
jae    11d8 <gbuf_erase+0x78>
mov    %rbp,%rsi
call   1060 <gbuf_move_to@plt>
jmp    1201 <gbuf_erase+0xa1>
call   1030 <gbuf_offset@plt>
mov    %r15,%r12
sub    %rbp,%rax
sub    %rax,%r12
mov    %rax,%r15
test   %rax,%rax
je     11f9 <gbuf_erase+0x99>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <gbuf_retract@plt>
mov    %r12,%r15
test   %r12,%r12
je     120c <gbuf_erase+0xac>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <gbuf_delete@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.gbuf = type { i32 }
define i32 @gbuf_erase(%struct.gbuf* %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.gbuf*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store %struct.gbuf* %0, %struct.gbuf** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%10 = load %struct.gbuf*, %struct.gbuf** %5
%11 = icmp ne %struct.gbuf* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load %struct.gbuf*, %struct.gbuf** %5
%15 = load i64, i64* %6
%16 = load i64, i64* %7
%17 = call i64 @validate_range(%struct.gbuf* %14, i64 %15, i64 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 -1, i32* %4
br label %72
20: ; preds = %3
%21 = load i64, i64* %7
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 0, i32* %4
br label %72
24: ; preds = %20
%25 = load i64, i64* %6
%26 = load i64, i64* %7
%27 = add i64 %25, %26
%28 = load %struct.gbuf*, %struct.gbuf** %5
%29 = call i64 @gbuf_offset(%struct.gbuf* %28)
%30 = icmp ult i64 %27, %29
br i1 %30, label %31, label %38
31: ; preds = %24
%32 = load %struct.gbuf*, %struct.gbuf** %5
%33 = load i64, i64* %6
%34 = load i64, i64* %7
%35 = add i64 %33, %34
%36 = call i32 @gbuf_move_to(%struct.gbuf* %32, i64 %35)
store i64 0, i64* %9
%37 = load i64, i64* %7
store i64 %37, i64* %8
br label %57
38: ; preds = %24
%39 = load i64, i64* %6
%40 = load %struct.gbuf*, %struct.gbuf** %5
%41 = call i64 @gbuf_offset(%struct.gbuf* %40)
%42 = icmp ugt i64 %39, %41
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load %struct.gbuf*, %struct.gbuf** %5
%45 = load i64, i64* %6
%46 = call i32 @gbuf_move_to(%struct.gbuf* %44, i64 %45)
%47 = load i64, i64* %7
store i64 %47, i64* %9
store i64 0, i64* %8
br label %56
48: ; preds = %38
%49 = load %struct.gbuf*, %struct.gbuf** %5
%50 = call i64 @gbuf_offset(%struct.gbuf* %49)
%51 = load i64, i64* %6
%52 = sub i64 %50, %51
store i64 %52, i64* %8
%53 = load i64, i64* %7
%54 = load i64, i64* %8
%55 = sub i64 %53, %54
store i64 %55, i64* %9
br label %56
56: ; preds = %48, %43
br label %57
57: ; preds = %56, %31
%58 = load i64, i64* %8
%59 = icmp ugt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = load %struct.gbuf*, %struct.gbuf** %5
%62 = load i64, i64* %8
%63 = call i32 @gbuf_retract(%struct.gbuf* %61, i64 %62)
br label %64
64: ; preds = %60, %57
%65 = load i64, i64* %9
%66 = icmp ugt i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %64
%68 = load %struct.gbuf*, %struct.gbuf** %5
%69 = load i64, i64* %9
%70 = call i32 @gbuf_delete(%struct.gbuf* %68, i64 %69)
br label %71
71: ; preds = %67, %64
store i32 0, i32* %4
br label %72
72: ; preds = %71, %23, %19
%73 = load i32, i32* %4
ret i32 %73
}

LLVM IR (-O2):
%struct.gbuf = type { i32 }
define i32 @gbuf_erase(%struct.gbuf* %0, i64 %1, i64 %2) {
%4 = icmp ne %struct.gbuf* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%7 = tail call i64 @validate_range(%struct.gbuf* %0, i64 %1, i64 %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %37
9: ; preds = %3
%10 = icmp eq i64 %2, 0
br i1 %10, label %37, label %11
11: ; preds = %9
%12 = add i64 %2, %1
%13 = tail call i64 @gbuf_offset(%struct.gbuf* %0)
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @gbuf_move_to(%struct.gbuf* %0, i64 %12)
br label %27
17: ; preds = %11
%18 = tail call i64 @gbuf_offset(%struct.gbuf* %0)
%19 = icmp ult i64 %18, %1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @gbuf_move_to(%struct.gbuf* %0, i64 %1)
br label %34
22: ; preds = %17
%23 = tail call i64 @gbuf_offset(%struct.gbuf* %0)
%24 = sub i64 %23, %1
%25 = sub i64 %2, %24
%26 = icmp eq i64 %24, 0
br i1 %26, label %31, label %27
27: ; preds = %15, %22
%28 = phi i64 [ 0, %15 ], [ %25, %22 ]
%29 = phi i64 [ %2, %15 ], [ %24, %22 ]
%30 = tail call i32 @gbuf_retract(%struct.gbuf* %0, i64 %29)
br label %31
31: ; preds = %27, %22
%32 = phi i64 [ %28, %27 ], [ %25, %22 ]
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %20, %31
%35 = phi i64 [ %2, %20 ], [ %32, %31 ]
%36 = tail call i32 @gbuf_delete(%struct.gbuf* %0, i64 %35)
br label %37
37: ; preds = %31, %34, %9, %3
%38 = phi i32 [ -1, %3 ], [ 0, %9 ], [ 0, %34 ], [ 0, %31 ]
ret i32 %38
}