#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 timeval {
  int tv_usec;
  int tv_sec;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
scalar_t__ fork();
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  pid_t child = fork();
  if (child < 0) {
    perror("fork failed");
    return 1;
  } else if (child == 0) {
    int fd = open("/root/tmp/foo", O_RDONLY | O_CREAT);
    char buf[256];
    unsigned int bytes = 0;
    struct timeval time0, time1;
    gettimeofday(&time0, NULL);
    while (1) {
      lseek(fd, 0, SEEK_SET);
      bytes += read(fd, buf, 256);
      if (bytes >= 1000000000)
        break;
    }
    gettimeofday(&time1, NULL);
    int dt_us =
        time1.tv_usec - time0.tv_usec + 1000000 * (time1.tv_sec - time0.tv_sec);
    printf("Data: 1000 MB\nTime: %d us\nRate: %d MB/s\n", dt_us,
           1000000000 / dt_us);
  } else {
    int fd = open("/root/tmp/foo", O_CREAT | O_TRUNC | O_WRONLY);
    while (1) {
      lseek(fd, 0, SEEK_SET);
      write(fd,
            "------------------------------------------------------------------"
            "--\n",
            70);
      lseek(fd, 0, SEEK_SET);
      write(fd,
            "__________________________________________________________________"
            "__\n",
            70);
      lseek(fd, 0, SEEK_SET);
      write(fd,
            "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
            "MM\n",
            70);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c0 <main+0x40>
lea    0xe51(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1379 <main+0x1f9>
cmpq   $0x0,-0x10(%rbp)
jne    12b4 <main+0x134>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
lea    0xe28(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x124(%rbp)
lea    -0x130(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x14(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %rax,%rcx
mov    -0x124(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x124(%rbp)
cmpl   $0x3b9aca00,-0x124(%rbp)
ca 9a 3b
jb     1254 <main+0xd4>
jmp    1259 <main+0xd9>
jmp    1206 <main+0x86>
lea    -0x138(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x138(%rbp),%eax
sub    -0x130(%rbp),%eax
mov    -0x134(%rbp),%ecx
sub    -0x12c(%rbp),%ecx
imul   $0xf4240,%ecx,%ecx
add    %ecx,%eax
mov    %eax,-0x13c(%rbp)
mov    -0x13c(%rbp),%esi
mov    $0x3b9aca00,%eax
cltd
idivl  -0x13c(%rbp)
mov    %eax,%edx
lea    0xd70(%rip),%rdi
call   1040 <printf@plt>
jmp    136d <main+0x1ed>
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
mov    0x2d2c(%rip),%rax
or     (%rax),%esi
mov    0x2ceb(%rip),%rax
or     (%rax),%esi
lea    0xd36(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x140(%rbp)
mov    -0x140(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x140(%rbp),%edi
lea    0xd3f(%rip),%rsi
mov    $0x46,%edx
call   1030 <write@plt>
mov    -0x140(%rbp),%edi
mov    0x2cad(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x140(%rbp),%edi
lea    0xd58(%rip),%rsi
mov    $0x46,%edx
call   1030 <write@plt>
mov    -0x140(%rbp),%edi
mov    0x2c80(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x140(%rbp),%edi
lea    0xd71(%rip),%rsi
mov    $0x46,%edx
call   1030 <write@plt>
jmp    12e1 <main+0x161>
jmp    1372 <main+0x1f2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     12f6 <main+0x176>
je     1246 <main+0xc6>
mov    0x2e4a(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df8(%rip),%rax
or     (%rax),%esi
lea    0xe43(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
mov    0x2df1(%rip),%r13
lea    0xe65(%rip),%r14
lea    0xea4(%rip),%r15
lea    0xee3(%rip),%r12
nopl   0x0(%rax)
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x46,%edx
call   1030 <write@plt>
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x46,%edx
call   1030 <write@plt>
mov    0x0(%r13),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x46,%edx
call   1030 <write@plt>
jmp    11f0 <main+0x70>
mov    0x2d8b(%rip),%rax
mov    0x2da4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdad(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x2d4d(%rip),%r15
lea    0x10(%rsp),%r14
mov    (%r15),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
mov    %ebx,%ebx
add    %rax,%rbx
cmp    $0x3b9aca00,%ebx
jb     1280 <main+0x100>
xor    %ebx,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    (%rsp),%eax
sub    0x8(%rsp),%eax
mov    0x4(%rsp),%ecx
sub    0xc(%rsp),%ecx
imul   $0xf4240,%ecx,%esi
add    %eax,%esi
mov    $0x3b9aca00,%eax
xor    %edx,%edx
idiv   %esi
lea    0xd3f(%rip),%rdi
mov    %eax,%edx
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd03(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
jmp    12e2 <main+0x162>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [14 x i8] c"/root/tmp/foo\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [41 x i8] c"Data: 1000 MB\0ATime: %d us\0ARate: %d MB/s\0A\00"
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [70 x i8] c"--------------------------------------------------------------------\0A\00"
@.str.4 = [70 x i8] c"____________________________________________________________________\0A\00"
@.str.5 = [70 x i8] c"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = call i64 (...) @fork()
store i64 %10, i64* %2
%11 = load i64, i64* %2
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %82
15: ; preds = %0
%16 = load i64, i64* %2
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %57
18: ; preds = %15
%19 = load i32, i32* @O_RDONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 %22, i32* %3
store i32 0, i32* %5
%23 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
br label %24
24: ; preds = %18, %38
%25 = load i32, i32* %3
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = load i32, i32* %3
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%30 = call i64 @read(i32 %28, i8* %29, i32 256)
%31 = load i32, i32* %5
%32 = zext i32 %31 to i64
%33 = add nsw i64 %32, %30
%34 = trunc i64 %33 to i32
store i32 %34, i32* %5
%35 = load i32, i32* %5
%36 = icmp uge i32 %35, 1000000000
br i1 %36, label %37, label %38
37: ; preds = %24
br label %39
38: ; preds = %24
br label %24
39: ; preds = %37
%40 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%44 = load i32, i32* %43
%45 = sub nsw i32 %42, %44
%46 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%49 = load i32, i32* %48
%50 = sub nsw i32 %47, %49
%51 = mul nsw i32 1000000, %50
%52 = add nsw i32 %45, %51
store i32 %52, i32* %8
%53 = load i32, i32* %8
%54 = load i32, i32* %8
%55 = sdiv i32 1000000000, %54
%56 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %53, i32 %55)
br label %80
57: ; preds = %15
%58 = load i32, i32* @O_CREAT
%59 = load i32, i32* @O_TRUNC
%60 = or i32 %58, %59
%61 = load i32, i32* @O_WRONLY
%62 = or i32 %60, %61
%63 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %62)
store i32 %63, i32* %9
br label %64
64: ; preds = %57, %64
%65 = load i32, i32* %9
%66 = load i32, i32* @SEEK_SET
%67 = call i32 @lseek(i32 %65, i32 0, i32 %66)
%68 = load i32, i32* %9
%69 = call i32 @write(i32 %68, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i32 70)
%70 = load i32, i32* %9
%71 = load i32, i32* @SEEK_SET
%72 = call i32 @lseek(i32 %70, i32 0, i32 %71)
%73 = load i32, i32* %9
%74 = call i32 @write(i32 %73, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 70)
%75 = load i32, i32* %9
%76 = load i32, i32* @SEEK_SET
%77 = call i32 @lseek(i32 %75, i32 0, i32 %76)
%78 = load i32, i32* %9
%79 = call i32 @write(i32 %78, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0), i32 70)
br label %64
80: ; preds = %39
br label %81
81: ; preds = %80
store i32 0, i32* %1
br label %82
82: ; preds = %81, %13
%83 = load i32, i32* %1
ret i32 %83
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@.str = [12 x i8] c"fork failed\00"
@.str.1 = [14 x i8] c"/root/tmp/foo\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [41 x i8] c"Data: 1000 MB\0ATime: %d us\0ARate: %d MB/s\0A\00"
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.3 = [70 x i8] c"--------------------------------------------------------------------\0A\00"
@.str.4 = [70 x i8] c"____________________________________________________________________\0A\00"
@.str.5 = [70 x i8] c"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = tail call i64 (...) @fork()
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %61
8: ; preds = %0
%9 = icmp eq i64 %4, 0
br i1 %9, label %10, label %44
10: ; preds = %8
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%16 = bitcast %struct.timeval* %2 to i8*
%17 = bitcast %struct.timeval* %3 to i8*
%18 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
br label %19
19: ; preds = %19, %10
%20 = phi i64 [ 0, %10 ], [ %25, %19 ]
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %14, i32 0, i32 %21)
%23 = call i64 @read(i32 %14, i8* %15, i32 256)
%24 = and i64 %20, 4294967295
%25 = add nsw i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = icmp ugt i32 %26, 999999999
br i1 %27, label %28, label %19
28: ; preds = %19
%29 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%33 = load i32, i32* %32
%34 = sub nsw i32 %31, %33
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%38 = load i32, i32* %37
%39 = sub nsw i32 %36, %38
%40 = mul nsw i32 %39, 1000000
%41 = add nsw i32 %40, %34
%42 = sdiv i32 1000000000, %41
%43 = tail call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %41, i32 %42)
br label %61
44: ; preds = %8
%45 = load i32, i32* @O_CREAT
%46 = load i32, i32* @O_TRUNC
%47 = or i32 %46, %45
%48 = load i32, i32* @O_WRONLY
%49 = or i32 %47, %48
%50 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %51, %44
%52 = load i32, i32* @SEEK_SET
%53 = tail call i32 @lseek(i32 %50, i32 0, i32 %52)
%54 = tail call i32 @write(i32 %50, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i32 70)
%55 = load i32, i32* @SEEK_SET
%56 = tail call i32 @lseek(i32 %50, i32 0, i32 %55)
%57 = tail call i32 @write(i32 %50, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.4, i64 0, i64 0), i32 70)
%58 = load i32, i32* @SEEK_SET
%59 = tail call i32 @lseek(i32 %50, i32 0, i32 %58)
%60 = tail call i32 @write(i32 %50, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0), i32 70)
br label %51
61: ; preds = %28, %6
%62 = phi i32 [ 1, %6 ], [ 0, %28 ]
ret i32 %62
}