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

/* Variables and functions */
scalar_t__ CLOCKS_PER_SEC;
scalar_t__ clock();
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
size_t receive_file(int, char *);
size_t send_file(int, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int main(int argc, char **argv) {
  char dst_file[256];
  pid_t pid;
  int p[2];
  size_t sent, received;
  clock_t start, duration;
  double sec, speed;
  if (argc < 2) {
    printf("Usage: %s <file>\n", argv[0]);
    return 1;
  }
  snprintf(dst_file, sizeof(dst_file), "%s.1", argv[1]);
  pipe(p);
  pid = fork();
  if (pid == 0) {
    close(p[0]);
    sent = send_file(p[1], argv[1]);
    close(p[1]);
  } else if (pid < 0) {
    perror("fork() failed");
    return 1;
  } else {
    start = clock();
    close(p[1]);
    received = receive_file(p[0], dst_file);
    close(p[0]);
    duration = clock() - start;
    sec = ((double)duration / (double)CLOCKS_PER_SEC);
    speed = ((double)received / (1024.0 * 1024.0)) / sec;
    printf("Sent %zi bytes in %f seconds (%f MiB/s)\n", received, sec, speed);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d4 <main+0x44>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136e <main+0x1de>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x100,%esi
lea    0xe53(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    1251 <main+0xc1>
mov    -0x120(%rbp),%edi
call   1060 <close@plt>
mov    -0x11c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   10a0 <send_file@plt>
mov    %rax,-0x128(%rbp)
mov    -0x11c(%rbp),%edi
call   1060 <close@plt>
jmp    1367 <main+0x1d7>
cmpq   $0x0,-0x118(%rbp)
jge    1277 <main+0xe7>
lea    0xde1(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136e <main+0x1de>
xor    %eax,%eax
mov    %al,-0x151(%rbp)
call   1030 <clock@plt>
mov    %rax,-0x138(%rbp)
mov    -0x11c(%rbp),%edi
call   1060 <close@plt>
mov    -0x120(%rbp),%edi
lea    -0x110(%rbp),%rsi
call   1090 <receive_file@plt>
mov    %rax,-0x130(%rbp)
mov    -0x120(%rbp),%edi
call   1060 <close@plt>
mov    -0x151(%rbp),%al
call   1030 <clock@plt>
mov    -0x138(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x140(%rbp)
cvtsi2sdq -0x140(%rbp),%xmm0
ff ff
mov    0x2d02(%rip),%rax
cvtsi2sdq (%rax),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x148(%rbp)
ff
movaps 0xd12(%rip),%xmm1
movq   -0x130(%rbp),%xmm0
ff
punpckldq %xmm1,%xmm0
movapd 0xd0e(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  0xcd7(%rip),%xmm1
divsd  %xmm1,%xmm0
divsd  -0x148(%rbp),%xmm0
ff
movsd  %xmm0,-0x150(%rbp)
ff
mov    -0x130(%rbp),%rsi
movsd  -0x148(%rbp),%xmm0
ff
movsd  -0x150(%rbp),%xmm1
ff
lea    0xcfa(%rip),%rdi
mov    $0x2,%al
call   1040 <printf@plt>
jmp    1367 <main+0x1d7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11be <main+0x2e>
mov    (%rbx),%rsi
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    12a7 <main+0x117>
mov    0x8(%rbx),%rcx
lea    0xe71(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     1288 <main+0xf8>
js     12b5 <main+0x125>
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r14
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
call   1090 <receive_file@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1030 <clock@plt>
sub    %r14,%rax
cvtsi2sd %rax,%xmm0
mov    0x2da9(%rip),%rax
cvtsi2sdq (%rax),%xmm1
movq   %rbx,%xmm2
punpckldq 0xdaf(%rip),%xmm2
divsd  %xmm1,%xmm0
subpd  0xdb3(%rip),%xmm2
movapd %xmm2,%xmm1
unpckhpd %xmm2,%xmm1
addsd  %xmm2,%xmm1
mulsd  0xdaf(%rip),%xmm1
divsd  %xmm0,%xmm1
lea    0xdd1(%rip),%rdi
mov    %rbx,%rsi
mov    $0x2,%al
call   1040 <printf@plt>
jmp    12a7 <main+0x117>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%edi
mov    0x8(%rbx),%rsi
call   10a0 <send_file@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd83(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebp
jmp    12a7 <main+0x117>

LLVM IR (-O0):
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [5 x i8] c"%s.1\00"
@.str.2 = [14 x i8] c"fork() failed\00"
@CLOCKS_PER_SEC = global i64 0
@.str.3 = [41 x i8] c"Sent %zi bytes in %f seconds (%f MiB/s)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i64
%8 = alloca [2 x i32]
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca double
%14 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %82
22: ; preds = %2
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @snprintf(i8* %23, i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%29 = call i32 @pipe(i32* %28)
%30 = call i64 (...) @fork()
store i64 %30, i64* %7
%31 = load i64, i64* %7
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %46
33: ; preds = %22
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%38 = load i32, i32* %37
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i64 @send_file(i32 %38, i8* %41)
store i64 %42, i64* %9
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
br label %81
46: ; preds = %22
%47 = load i64, i64* %7
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %82
51: ; preds = %46
%52 = call i64 (...) @clock()
store i64 %52, i64* %11
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%57 = load i32, i32* %56
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%59 = call i64 @receive_file(i32 %57, i8* %58)
store i64 %59, i64* %10
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
%63 = call i64 (...) @clock()
%64 = load i64, i64* %11
%65 = sub nsw i64 %63, %64
store i64 %65, i64* %12
%66 = load i64, i64* %12
%67 = sitofp i64 %66 to double
%68 = load i64, i64* @CLOCKS_PER_SEC
%69 = sitofp i64 %68 to double
%70 = fdiv double %67, %69
store double %70, double* %13
%71 = load i64, i64* %10
%72 = uitofp i64 %71 to double
%73 = fdiv double %72, 0x4130000000000000
%74 = load double, double* %13
%75 = fdiv double %73, %74
store double %75, double* %14
%76 = load i64, i64* %10
%77 = load double, double* %13
%78 = load double, double* %14
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i64 %76, double %77, double %78)
br label %80
80: ; preds = %51
br label %81
81: ; preds = %80, %33
store i32 0, i32* %3
br label %82
82: ; preds = %81, %49, %17
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [5 x i8] c"%s.1\00"
@.str.2 = [14 x i8] c"fork() failed\00"
@CLOCKS_PER_SEC = global i64 0
@.str.3 = [41 x i8] c"Sent %zi bytes in %f seconds (%f MiB/s)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca [2 x i32]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast [2 x i32]* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %51
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %11
%20 = load i32, i32* %15
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%23 = load i32, i32* %22
%24 = load i8*, i8** %12
%25 = call i64 @send_file(i32 %23, i8* %24)
%26 = load i32, i32* %22
%27 = call i32 @close(i32 %26)
br label %51
28: ; preds = %11
%29 = icmp slt i64 %17, 0
br i1 %29, label %30, label %32
30: ; preds = %28
%31 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %51
32: ; preds = %28
%33 = call i64 (...) @clock()
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i32, i32* %15
%38 = call i64 @receive_file(i32 %37, i8* %5)
%39 = load i32, i32* %15
%40 = call i32 @close(i32 %39)
%41 = call i64 (...) @clock()
%42 = sub nsw i64 %41, %33
%43 = sitofp i64 %42 to double
%44 = load i64, i64* @CLOCKS_PER_SEC
%45 = sitofp i64 %44 to double
%46 = fdiv double %43, %45
%47 = uitofp i64 %38 to double
%48 = fmul double %47, 0x3EB0000000000000
%49 = fdiv double %48, %46
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i64 %38, double %46, double %49)
br label %51
51: ; preds = %19, %32, %30, %8
%52 = phi i32 [ 1, %8 ], [ 1, %30 ], [ 0, %32 ], [ 0, %19 ]
ret i32 %52
}