#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_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, scalar_t__);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd1 = open("wiki.html", O_RDWR);
  if (fd1 < 0)
    return 0;
  int fd2 = open("wiki.html", O_RDWR);
  char c1, c2;
  struct timeval start_time, end_time;
  gettimeofday(&start_time, NULL);
  lseek(fd2, -1, SEEK_END);
  read(fd1, &c1, 1);
  read(fd2, &c2, 1);
  while (c1 != c2) {
    lseek(fd1, -1, SEEK_CUR);
    lseek(fd2, -1, SEEK_CUR);
    write(fd1, &c2, 1);
    write(fd2, &c1, 1);
    lseek(fd2, -2, SEEK_CUR);
    read(fd1, &c1, 1);
    read(fd2, &c2, 1);
  }
  gettimeofday(&end_time, NULL);
  printf("%lu", (end_time.tv_usec - start_time.tv_usec) +
                    ((end_time.tv_sec - start_time.tv_sec) * 1000000));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    119d <main+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    12ce <main+0x16e>
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe53(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0xc(%rbp),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0xc(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl -0xd(%rbp),%eax
movsbl -0xe(%rbp),%ecx
cmp    %ecx,%eax
je     1295 <main+0x135>
mov    -0x8(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    -0xc(%rbp),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffe,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    -0xc(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
jmp    11fa <main+0x9a>
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
mov    -0x28(%rbp),%rsi
sub    -0x18(%rbp),%rsi
mov    -0x30(%rbp),%eax
sub    -0x20(%rbp),%eax
imul   $0xf4240,%eax,%eax
cltq
add    %rax,%rsi
lea    0xd48(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e75(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe83(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12b0 <main+0x150>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
lea    0xe6a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <read@plt>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1070 <read@plt>
mov    0xf(%rsp),%al
cmp    0xe(%rsp),%al
je     1277 <main+0x117>
mov    0x2de3(%rip),%r12
lea    0xe(%rsp),%r14
lea    0xf(%rsp),%r15
nop
mov    (%r12),%edx
mov    %ebx,%edi
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    (%r12),%edx
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    (%r12),%edx
mov    %ebp,%edi
mov    $0xfffffffe,%esi
call   1050 <lseek@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movzbl 0xf(%rsp),%eax
cmp    0xe(%rsp),%al
jne    1200 <main+0xa0>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1060 <gettimeofday@plt>
mov    0x18(%rsp),%rax
sub    0x28(%rsp),%rax
movslq 0x10(%rsp),%rcx
movslq 0x20(%rsp),%rdx
sub    %rdx,%rcx
imul   $0xf4240,%rcx,%rsi
add    %rax,%rsi
lea    0xd5f(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [10 x i8] c"wiki.html\00"
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [4 x i8] c"%lu\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
store i32 0, i32* %1
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 0, i32* %1
br label %64
13: ; preds = %0
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %3
%16 = call i32 @gettimeofday(%struct.timeval* %6, i32* null)
%17 = load i32, i32* %3
%18 = load i32, i32* @SEEK_END
%19 = call i32 @lseek(i32 %17, i32 -1, i32 %18)
%20 = load i32, i32* %2
%21 = call i32 @read(i32 %20, i8* %4, i32 1)
%22 = load i32, i32* %3
%23 = call i32 @read(i32 %22, i8* %5, i32 1)
br label %24
24: ; preds = %30, %13
%25 = load i8, i8* %4
%26 = sext i8 %25 to i32
%27 = load i8, i8* %5
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %26, %28
br i1 %29, label %30, label %48
30: ; preds = %24
%31 = load i32, i32* %2
%32 = load i32, i32* @SEEK_CUR
%33 = call i32 @lseek(i32 %31, i32 -1, i32 %32)
%34 = load i32, i32* %3
%35 = load i32, i32* @SEEK_CUR
%36 = call i32 @lseek(i32 %34, i32 -1, i32 %35)
%37 = load i32, i32* %2
%38 = call i32 @write(i32 %37, i8* %5, i32 1)
%39 = load i32, i32* %3
%40 = call i32 @write(i32 %39, i8* %4, i32 1)
%41 = load i32, i32* %3
%42 = load i32, i32* @SEEK_CUR
%43 = call i32 @lseek(i32 %41, i32 -2, i32 %42)
%44 = load i32, i32* %2
%45 = call i32 @read(i32 %44, i8* %4, i32 1)
%46 = load i32, i32* %3
%47 = call i32 @read(i32 %46, i8* %5, i32 1)
br label %24
48: ; preds = %24
%49 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%50 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%51 = load i64, i64* %50
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
%53 = load i64, i64* %52
%54 = sub nsw i64 %51, %53
%55 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%56 = load i32, i32* %55
%57 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%58 = load i32, i32* %57
%59 = sub nsw i32 %56, %58
%60 = mul nsw i32 %59, 1000000
%61 = sext i32 %60 to i64
%62 = add nsw i64 %54, %61
%63 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %62)
store i32 0, i32* %1
br label %64
64: ; preds = %48, %12
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [10 x i8] c"wiki.html\00"
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [4 x i8] c"%lu\00"
define i32 @main() {
%1 = alloca i8
%2 = alloca i8
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %51, label %8
8: ; preds = %0
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = bitcast %struct.timeval* %3 to i8*
%12 = bitcast %struct.timeval* %4 to i8*
%13 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%14 = load i32, i32* @SEEK_END
%15 = tail call i32 @lseek(i32 %10, i32 -1, i32 %14)
%16 = call i32 @read(i32 %6, i8* %1, i32 1)
%17 = call i32 @read(i32 %10, i8* %2, i32 1)
%18 = load i8, i8* %1
%19 = load i8, i8* %2
%20 = icmp eq i8 %18, %19
br i1 %20, label %35, label %21
21: ; preds = %8, %21
%22 = load i32, i32* @SEEK_CUR
%23 = tail call i32 @lseek(i32 %6, i32 -1, i32 %22)
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @lseek(i32 %10, i32 -1, i32 %24)
%26 = call i32 @write(i32 %6, i8* %2, i32 1)
%27 = call i32 @write(i32 %10, i8* %1, i32 1)
%28 = load i32, i32* @SEEK_CUR
%29 = tail call i32 @lseek(i32 %10, i32 -2, i32 %28)
%30 = call i32 @read(i32 %6, i8* %1, i32 1)
%31 = call i32 @read(i32 %10, i8* %2, i32 1)
%32 = load i8, i8* %1
%33 = load i8, i8* %2
%34 = icmp eq i8 %32, %33
br i1 %34, label %35, label %21
35: ; preds = %21, %8
%36 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%40 = load i64, i64* %39
%41 = sub i64 %38, %40
%42 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%45 = load i32, i32* %44
%46 = sub nsw i32 %43, %45
%47 = mul nsw i32 %46, 1000000
%48 = sext i32 %47 to i64
%49 = add nsw i64 %41, %48
%50 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %49)
br label %51
51: ; preds = %0, %35
ret i32 0
}