#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*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, int);
void print_progress(int urgent, int received_bytes) {
  static unsigned int i = 1;
  static int first = -1;
  static int last_bytes = 0;
  char buffer[100];
  char *ptr;
  if (((received_bytes - last_bytes) >> 20) > 0 || (i & 0x3FF) == 0 || urgent) {
    if (!first) {
      sprintf(buffer, "%d KBytes", (last_bytes >> 10));
      for (ptr = buffer; *ptr != 0; ++ptr)
        *ptr = '\b';
      printf(buffer);
    }
    printf("%d KBytes", (received_bytes >> 10));
    i = 1;
    first = 0;
    last_bytes = received_bytes;
  }
  ++i;
}


x86 assembly (clang, -O0):
<print_progress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
sub    0x2f02(%rip),%eax
sar    $0x14,%eax
cmp    $0x0,%eax
jg     1164 <print_progress+0x44>
mov    0x2ee4(%rip),%eax
and    $0x3ff,%eax
cmp    $0x0,%eax
je     1164 <print_progress+0x44>
cmpl   $0x0,-0x4(%rbp)
je     11f6 <print_progress+0xd6>
cmpl   $0x0,0x2ec9(%rip)
jne    11c5 <print_progress+0xa5>
lea    -0x70(%rbp),%rdi
mov    0x2ec1(%rip),%edx
sar    $0xa,%edx
lea    0xe7b(%rip),%rsi
call   1040 <sprintf@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11ba <print_progress+0x9a>
mov    -0x78(%rbp),%rax
movb   $0x8,(%rax)
mov    -0x78(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x78(%rbp)
jmp    1192 <print_progress+0x72>
lea    -0x70(%rbp),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%esi
sar    $0xa,%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,0x2e4d(%rip)
movl   $0x0,0x2e47(%rip)
mov    -0x8(%rbp),%eax
mov    %eax,0x2e46(%rip)
mov    0x2e34(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e2b(%rip)
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_progress>:
push   %rbx
sub    $0x70,%rsp
mov    %esi,%ebx
mov    0x2f0f(%rip),%edx
mov    %esi,%eax
sub    %edx,%eax
cmp    $0xfffff,%eax
jg     114c <print_progress+0x2c>
test   %edi,%edi
jne    114c <print_progress+0x2c>
mov    0x2eee(%rip),%eax
mov    %eax,%ecx
and    $0x3ff,%ecx
jne    11bc <print_progress+0x9c>
cmpb   $0x1,0x2ee5(%rip)
jne    1197 <print_progress+0x77>
sar    $0xa,%edx
lea    0xea1(%rip),%rsi
mov    %rsp,%rdi
call   1040 <sprintf@plt>
cmpb   $0x0,(%rsp)
je     118d <print_progress+0x6d>
lea    0x1(%rsp),%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x8,-0x1(%rax)
cmpb   $0x0,(%rax)
lea    0x1(%rax),%rax
jne    1180 <print_progress+0x60>
mov    %rsp,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%esi
sar    $0xa,%esi
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movb   $0x1,0x2e87(%rip)
mov    %ebx,0x2e85(%rip)
mov    $0x1,%eax
add    $0x1,%eax
mov    %eax,0x2e6b(%rip)
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@print_progress.i = internal global i32 1
@print_progress.first = internal global i32 -1
@print_progress.last_bytes = internal global i32 0
@.str = [10 x i8] c"%d KBytes\00"
define void @print_progress(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = alloca i8*
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = load i32, i32* @print_progress.last_bytes
%9 = sub nsw i32 %7, %8
%10 = ashr i32 %9, 20
%11 = icmp sgt i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %2
%13 = load i32, i32* @print_progress.i
%14 = and i32 %13, 1023
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = load i32, i32* %3
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %46
19: ; preds = %16, %12, %2
%20 = load i32, i32* @print_progress.first
%21 = icmp ne i32 %20, 0
br i1 %21, label %41, label %22
22: ; preds = %19
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%24 = load i32, i32* @print_progress.last_bytes
%25 = ashr i32 %24, 10
%26 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
store i8* %27, i8** %6
br label %28
28: ; preds = %35, %22
%29 = load i8*, i8** %6
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %28
%34 = load i8*, i8** %6
store i8 8, i8* %34
br label %35
35: ; preds = %33
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %6
br label %28
38: ; preds = %28
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* %39)
br label %41
41: ; preds = %38, %19
%42 = load i32, i32* %4
%43 = ashr i32 %42, 10
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %43)
store i32 1, i32* @print_progress.i
store i32 0, i32* @print_progress.first
%45 = load i32, i32* %4
store i32 %45, i32* @print_progress.last_bytes
br label %46
46: ; preds = %41, %16
%47 = load i32, i32* @print_progress.i
%48 = add i32 %47, 1
store i32 %48, i32* @print_progress.i
ret void
}

LLVM IR (-O2):
@print_progress.i = internal unnamed_addr global i32 1
@print_progress.first = internal unnamed_addr global i1 false
@print_progress.last_bytes = internal unnamed_addr global i32 0
@.str = [10 x i8] c"%d KBytes\00"
define void @print_progress(i32 %0, i32 %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @print_progress.last_bytes
%6 = sub nsw i32 %1, %5
%7 = icmp sgt i32 %6, 1048575
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @print_progress.i
%10 = and i32 %9, 1023
%11 = icmp eq i32 %10, 0
%12 = icmp ne i32 %0, 0
%13 = or i1 %12, %11
br i1 %13, label %14, label %31
14: ; preds = %8, %2
%15 = load i1, i1* @print_progress.first
br i1 %15, label %16, label %28
16: ; preds = %14
%17 = ashr i32 %5, 10
%18 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8, i8* %4
%20 = icmp eq i8 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %16, %21
%22 = phi i8* [ %23, %21 ], [ %4, %16 ]
store i8 8, i8* %22
%23 = getelementptr inbounds i8, i8* %22, i64 1
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 0
br i1 %25, label %26, label %21
26: ; preds = %21, %16
%27 = call i32 (i8*, ...) @printf(i8* %4)
br label %28
28: ; preds = %26, %14
%29 = ashr i32 %1, 10
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %29)
store i1 true, i1* @print_progress.first
store i32 %1, i32* @print_progress.last_bytes
br label %31
31: ; preds = %8, %28
%32 = phi i32 [ %9, %8 ], [ 1, %28 ]
%33 = add i32 %32, 1
store i32 %33, i32* @print_progress.i
ret void
}