#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 statvfs {
  double f_blocks;
  double f_frsize;
  double f_bfree;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *smprintf(char *, double, char const, double, char const);
scalar_t__ statvfs(char *, struct statvfs *);
int /*<<< orphan*/ stderr;
char *freespace(char *mntpt) {
  struct statvfs data;
  double total, used = 0;
  int ctotal, cused = 0;
  const char unit[] = {'k', 'M', 'G', 'T'};
  if ((statvfs(mntpt, &data)) < 0) {
    fprintf(stderr, "can't get info on disk.\n");
    return ("?");
  }
  total = (data.f_blocks * data.f_frsize);
  used = ((data.f_blocks - data.f_bfree) * data.f_frsize);
  while (total > 1024) {
    total /= 1024;
    ctotal++;
  }
  while (used > 1024) {
    used /= 1024;
    cused++;
  }
  return (smprintf("%.2f%c %.2f%c", total, unit[ctotal - 1], used,
                   unit[cused - 1]));
}


x86 assembly (clang, -O0):
<freespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x0,-0x40(%rbp)
mov    0xeb7(%rip),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1050 <statvfs@plt>
cmp    $0x0,%rax
jge    1190 <freespace+0x60>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe9e(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1264 <freespace+0x134>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x18(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  0xe40(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11ee <freespace+0xbe>
movsd  -0x30(%rbp),%xmm0
movsd  0xe29(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11b3 <freespace+0x83>
jmp    11f3 <freespace+0xc3>
movsd  -0x38(%rbp),%xmm0
movsd  0xe00(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    122e <freespace+0xfe>
movsd  -0x38(%rbp),%xmm0
movsd  0xde9(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11f3 <freespace+0xc3>
movsd  -0x30(%rbp),%xmm0
mov    -0x3c(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x44(%rbp,%rax,1),%cl
movsd  -0x38(%rbp),%xmm1
mov    -0x40(%rbp),%eax
sub    $0x1,%eax
cltq
lea    0xdd4(%rip),%rdi
movsbl %cl,%esi
movsbl -0x44(%rbp,%rax,1),%edx
call   1030 <smprintf@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freespace>:
sub    $0x18,%rsp
mov    %rsp,%rsi
call   1050 <statvfs@plt>
test   %rax,%rax
js     118f <freespace+0x5f>
movsd  (%rsp),%xmm1
movsd  0x8(%rsp),%xmm2
movapd %xmm1,%xmm0
mulsd  %xmm2,%xmm0
subsd  0x10(%rsp),%xmm1
mulsd  %xmm2,%xmm1
ucomisd 0xe9a(%rip),%xmm0
jbe    11b0 <freespace+0x80>
movsd  0xe98(%rip),%xmm2
movsd  0xe88(%rip),%xmm3
nopl   0x0(%rax,%rax,1)
mulsd  %xmm2,%xmm0
add    $0x1,%eax
ucomisd %xmm3,%xmm0
ja     1180 <freespace+0x50>
jmp    11b0 <freespace+0x80>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe82(%rip),%rax
add    $0x18,%rsp
ret
mov    $0xffffffff,%ecx
ucomisd 0xe43(%rip),%xmm1
jbe    11ed <freespace+0xbd>
mov    $0xffffffff,%ecx
movsd  0xe3c(%rip),%xmm2
movsd  0xe2c(%rip),%xmm3
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mulsd  %xmm2,%xmm1
add    $0x1,%ecx
ucomisd %xmm3,%xmm1
ja     11e0 <freespace+0xb0>
cltq
lea    0xe1a(%rip),%rdx
movsbl (%rax,%rdx,1),%esi
movslq %ecx,%rax
movsbl (%rax,%rdx,1),%edx
lea    0xe27(%rip),%rdi
call   1030 <smprintf@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.statvfs = type { double, double, double }
@__const.freespace.unit = [4 x i8] c"kMGT"
@stderr = global i32 0
@.str = [25 x i8] c"can't get info on disk.\0A\00"
@.str.1 = [2 x i8] c"?\00"
@.str.2 = [14 x i8] c"%.2f%c %.2f%c\00"
define i8* @freespace(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.statvfs
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4 x i8]
store i8* %0, i8** %3
store double 0.000000e+00, double* %6
store i32 0, i32* %8
%10 = bitcast [4 x i8]* %9 to i8*
%11 = load i8*, i8** %3
%12 = call i64 @statvfs(i8* %11, %struct.statvfs* %4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %62
17: ; preds = %1
%18 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %4, i32 0, i32 0
%19 = load double, double* %18
%20 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %4, i32 0, i32 1
%21 = load double, double* %20
%22 = fmul double %19, %21
store double %22, double* %5
%23 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %4, i32 0, i32 0
%24 = load double, double* %23
%25 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %4, i32 0, i32 2
%26 = load double, double* %25
%27 = fsub double %24, %26
%28 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %4, i32 0, i32 1
%29 = load double, double* %28
%30 = fmul double %27, %29
store double %30, double* %6
br label %31
31: ; preds = %34, %17
%32 = load double, double* %5
%33 = fcmp ogt double %32, 1.024000e+03
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load double, double* %5
%36 = fdiv double %35, 1.024000e+03
store double %36, double* %5
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
br label %31
39: ; preds = %31
br label %40
40: ; preds = %43, %39
%41 = load double, double* %6
%42 = fcmp ogt double %41, 1.024000e+03
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load double, double* %6
%45 = fdiv double %44, 1.024000e+03
store double %45, double* %6
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %40
48: ; preds = %40
%49 = load double, double* %5
%50 = load i32, i32* %7
%51 = sub nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = load double, double* %6
%56 = load i32, i32* %8
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 %58
%60 = load i8, i8* %59
%61 = call i8* @smprintf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), double %49, i8 signext %54, double %55, i8 signext %60)
store i8* %61, i8** %2
br label %62
62: ; preds = %48, %14
%63 = load i8*, i8** %2
ret i8* %63
}

LLVM IR (-O2):
%struct.statvfs = type { double, double, double }
@__const.freespace.unit = [4 x i8] c"kMGT"
@stderr = global i32 0
@.str = [25 x i8] c"can't get info on disk.\0A\00"
@.str.1 = [2 x i8] c"?\00"
@.str.2 = [14 x i8] c"%.2f%c %.2f%c\00"
define i8* @freespace(i8* %0) {
%2 = alloca %struct.statvfs
%3 = bitcast %struct.statvfs* %2 to i8*
%4 = call i64 @statvfs(i8* %0, %struct.statvfs* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %46
9: ; preds = %1
%10 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %2, i64 0, i32 0
%11 = load double, double* %10
%12 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %2, i64 0, i32 1
%13 = load double, double* %12
%14 = fmul double %11, %13
%15 = getelementptr inbounds %struct.statvfs, %struct.statvfs* %2, i64 0, i32 2
%16 = load double, double* %15
%17 = fsub double %11, %16
%18 = fmul double %13, %17
%19 = fcmp ogt double %14, 1.024000e+03
br i1 %19, label %24, label %20
20: ; preds = %24, %9
%21 = phi double [ %14, %9 ], [ %27, %24 ]
%22 = phi i32 [ undef, %9 ], [ %25, %24 ]
%23 = fcmp ogt double %18, 1.024000e+03
br i1 %23, label %30, label %36
24: ; preds = %9, %24
%25 = phi i32 [ %28, %24 ], [ undef, %9 ]
%26 = phi double [ %27, %24 ], [ %14, %9 ]
%27 = fmul double %26, 0x3F50000000000000
%28 = add nsw i32 %25, 1
%29 = fcmp ogt double %27, 1.024000e+03
br i1 %29, label %24, label %20
30: ; preds = %20, %30
%31 = phi i32 [ %34, %30 ], [ 0, %20 ]
%32 = phi double [ %33, %30 ], [ %18, %20 ]
%33 = fmul double %32, 0x3F50000000000000
%34 = add nuw nsw i32 %31, 1
%35 = fcmp ogt double %33, 1.024000e+03
br i1 %35, label %30, label %36
36: ; preds = %30, %20
%37 = phi double [ %18, %20 ], [ %33, %30 ]
%38 = phi i32 [ -1, %20 ], [ %31, %30 ]
%39 = sext i32 %22 to i64
%40 = getelementptr inbounds [4 x i8], [4 x i8]* @__const.freespace.unit, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = sext i32 %38 to i64
%43 = getelementptr inbounds [4 x i8], [4 x i8]* @__const.freespace.unit, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = call i8* @smprintf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), double %21, i8 signext %41, double %37, i8 signext %44)
br label %46
46: ; preds = %36, %6
%47 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %6 ], [ %45, %36 ]
ret i8* %47
}