#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 */
typedef int pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, long long *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
long long RunDu(char *Path) {
  int Ret;
  pid_t Pid;
  int Pipe_stdout[2];
  long long Length;
  Ret = pipe(Pipe_stdout);
  if (Ret == -1) {
    perror("pipe()");
    exit(20);
  }
  Pid = fork();
  if (Pid == -1) {
    perror("fork()");
    exit(21);
  }
  if (Pid) {
    int Status;
    FILE *From;
    From = fdopen(Pipe_stdout[0], "r");
    fscanf(From, "%lld", &Length);
    close(Pipe_stdout[0]);
    close(Pipe_stdout[1]);
    waitpid(Pid, &Status, 0);
    return Length * 1024;
  } else {
    dup2(Pipe_stdout[1], 1);
    execlp("du", "du", "-s", Path, (char *)NULL);
    perror("exec()");
    return -1;
  }
}


x86 assembly (clang, -O0):
<RunDu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d8 <RunDu+0x38>
lea    0xe37(%rip),%rdi
call   1090 <perror@plt>
mov    $0x14,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1202 <RunDu+0x62>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
mov    $0x15,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x18(%rbp)
je     1262 <RunDu+0xc2>
mov    -0x20(%rbp),%edi
lea    0xdf8(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xde6(%rip),%rsi
lea    -0x28(%rbp),%rdx
call   1040 <fscanf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x2c(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x28(%rbp),%rax
shl    $0xa,%rax
mov    %rax,-0x8(%rbp)
jmp    12a2 <RunDu+0x102>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rcx
lea    0xd9b(%rip),%rsi
lea    0xd97(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10b0 <execlp@plt>
lea    0xd86(%rip),%rdi
call   1090 <perror@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RunDu>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1263 <RunDu+0xc3>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1224 <RunDu+0x84>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1279 <RunDu+0xd9>
mov    (%rsp),%edi
lea    0xe33(%rip),%rsi
call   1070 <fdopen@plt>
lea    0xe29(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x8(%rsp),%rax
shl    $0xa,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x4(%rsp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
lea    0xddc(%rip),%rdi
lea    0xdd8(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   10b0 <execlp@plt>
lea    0xdc6(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffffffffffff,%rax
jmp    121c <RunDu+0x7c>
lea    0xd96(%rip),%rdi
call   1090 <perror@plt>
mov    $0x14,%edi
call   10a0 <exit@plt>
lea    0xd87(%rip),%rdi
call   1090 <perror@plt>
mov    $0x15,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"pipe()\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [5 x i8] c"%lld\00"
@.str.4 = [3 x i8] c"du\00"
@.str.5 = [3 x i8] c"-s\00"
@.str.6 = [7 x i8] c"exec()\00"
define i64 @RunDu(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %3
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 20)
unreachable
17: ; preds = %1
%18 = call i32 (...) @fork()
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 21)
unreachable
24: ; preds = %17
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %43
27: ; preds = %24
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%29 = load i32, i32* %28
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %9
%31 = load i32*, i32** %9
%32 = call i32 @fscanf(i32* %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64* %7)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %5
%40 = call i32 @waitpid(i32 %39, i32* %8, i32 0)
%41 = load i64, i64* %7
%42 = mul nsw i64 %41, 1024
store i64 %42, i64* %2
br label %50
43: ; preds = %24
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%45 = load i32, i32* %44
%46 = call i32 @dup2(i32 %45, i32 1)
%47 = load i8*, i8** %3
%48 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %47, i8* null)
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i64 -1, i64* %2
br label %50
50: ; preds = %43, %27
%51 = load i64, i64* %2
ret i64 %51
}

LLVM IR (-O2):
@.str = [7 x i8] c"pipe()\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [5 x i8] c"%lld\00"
@.str.4 = [3 x i8] c"du\00"
@.str.5 = [3 x i8] c"-s\00"
@.str.6 = [7 x i8] c"exec()\00"
define i64 @RunDu(i8* %0) {
%2 = alloca [2 x i32]
%3 = alloca i64
%4 = alloca i32
%5 = bitcast [2 x i32]* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 20)
unreachable
13: ; preds = %1
%14 = call i32 (...) @fork()
switch i32 %14, label %18 [
i32 -1, label %15
i32 0, label %31
]
15: ; preds = %13
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 21)
unreachable
18: ; preds = %13
%19 = bitcast i32* %4 to i8*
%20 = load i32, i32* %7
%21 = call i32* @fdopen(i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64* %3)
%23 = load i32, i32* %7
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = call i32 @waitpid(i32 %14, i32* %4, i32 0)
%29 = load i64, i64* %3
%30 = shl nsw i64 %29, 10
br label %37
31: ; preds = %13
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @dup2(i32 %33, i32 1)
%35 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* null)
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %37
37: ; preds = %31, %18
%38 = phi i64 [ %30, %18 ], [ -1, %31 ]
ret i64 %38
}