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

/* Variables and functions */
scalar_t__ ring_overwrite(struct ring *, char *, unsigned int);
unsigned int ring_read(struct ring *, char *, int);
unsigned int ring_overmove(struct ring *to, struct ring *from) {
  char buffer[4096];
  unsigned int count;
  unsigned int total = 0;
  while ((count = ring_read(from, buffer, 4096)))
    total += ring_overwrite(to, buffer, count);
  return total;
}


x86 assembly (clang, -O0):
<ring_overmove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1018(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <ring_read@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
je     118e <ring_overmove+0x6e>
mov    -0x8(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    -0x1014(%rbp),%edx
call   1040 <ring_overwrite@plt>
mov    %rax,%rcx
mov    -0x1018(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x1018(%rbp)
jmp    113d <ring_overmove+0x1d>
mov    -0x1018(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ring_overmove>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rsi
mov    %r15,%rdi
mov    $0x1000,%edx
call   1030 <ring_read@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1173 <ring_overmove+0x53>
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <ring_overwrite@plt>
add    %eax,%ebp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <ring_read@plt>
test   %eax,%eax
jne    1150 <ring_overmove+0x30>
mov    %ebp,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ring = type { i32 }
define i32 @ring_overmove(%struct.ring* %0, %struct.ring* %1) {
%3 = alloca %struct.ring*
%4 = alloca %struct.ring*
%5 = alloca [4096 x i8]
%6 = alloca i32
%7 = alloca i32
store %struct.ring* %0, %struct.ring** %3
store %struct.ring* %1, %struct.ring** %4
store i32 0, i32* %7
br label %8
8: ; preds = %13, %2
%9 = load %struct.ring*, %struct.ring** %4
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%11 = call i32 @ring_read(%struct.ring* %9, i8* %10, i32 4096)
store i32 %11, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %22
13: ; preds = %8
%14 = load %struct.ring*, %struct.ring** %3
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%16 = load i32, i32* %6
%17 = call i64 @ring_overwrite(%struct.ring* %14, i8* %15, i32 %16)
%18 = load i32, i32* %7
%19 = zext i32 %18 to i64
%20 = add nsw i64 %19, %17
%21 = trunc i64 %20 to i32
store i32 %21, i32* %7
br label %8
22: ; preds = %8
%23 = load i32, i32* %7
ret i32 %23
}

LLVM IR (-O2):
%struct.ring = type { i32 }
define i32 @ring_overmove(%struct.ring* %0, %struct.ring* %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @ring_read(%struct.ring* %1, i8* %4, i32 4096)
%6 = icmp eq i32 %5, 0
br i1 %6, label %15, label %7
7: ; preds = %2, %7
%8 = phi i32 [ %13, %7 ], [ %5, %2 ]
%9 = phi i32 [ %12, %7 ], [ 0, %2 ]
%10 = call i64 @ring_overwrite(%struct.ring* %0, i8* %4, i32 %8)
%11 = trunc i64 %10 to i32
%12 = add i32 %9, %11
%13 = call i32 @ring_read(%struct.ring* %1, i8* %4, i32 4096)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %7
15: ; preds = %7, %2
%16 = phi i32 [ 0, %2 ], [ %12, %7 ]
ret i32 %16
}