#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fsync(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int random();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sim_fd;
  int x, y;
  char buffer[10];
  sim_fd = open("/sys/devices/platform/vms/coordinates", O_RDWR);
  if (sim_fd < 0) {
    perror("Couldn't open vms coordinate file\n");
    exit(-1);
  }
  while (1) {
    x = random() % 20;
    y = random() % 20;
    if (x % 2)
      x = -x;
    if (y % 2)
      y = -y;
    sprintf(buffer, "%d %d %d", x, y, 0);
    write(sim_fd, buffer, strlen(buffer));
    fsync(sim_fd);
    sleep(1);
  }
  close(sim_fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11de <main+0x4e>
lea    0xe57(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    11e3 <main+0x53>
mov    $0x0,%al
call   1050 <random@plt>
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    $0x14,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1223 <main+0x93>
xor    %eax,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     123f <main+0xaf>
xor    %eax,%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x26(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xdf9(%rip),%rsi
xor    %r8d,%r8d
call   1090 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
lea    -0x26(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x26(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <fsync@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    11e3 <main+0x53>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1266 <main+0xd6>
mov    %eax,%ebp
lea    0xe89(%rip),%r14
lea    0xe(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1050 <random@plt>
movslq %eax,%rbx
imul   $0x66666667,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x23,%rax
add    %ecx,%eax
shl    $0x2,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
xor    %eax,%eax
call   1050 <random@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
shl    $0x2,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
mov    %ebx,%edx
neg    %edx
test   $0x1,%bl
cmove  %ebx,%edx
mov    %eax,%ecx
neg    %ecx
test   $0x1,%al
cmove  %eax,%ecx
mov    %r15,%rdi
mov    %r14,%rsi
xor    %r8d,%r8d
call   1090 <sprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <fsync@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    11d0 <main+0x40>
lea    0xdb9(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"/sys/devices/platform/vms/coordinates\00"
@O_RDWR = global i32 0
@.str.1 = [35 x i8] c"Couldn't open vms coordinate file\0A\00"
@.str.2 = [9 x i8] c"%d %d %d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
br label %18
18: ; preds = %17, %36
%19 = call i32 (...) @random()
%20 = srem i32 %19, 20
store i32 %20, i32* %7
%21 = call i32 (...) @random()
%22 = srem i32 %21, 20
store i32 %22, i32* %8
%23 = load i32, i32* %7
%24 = srem i32 %23, 2
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i32, i32* %7
%28 = sub nsw i32 0, %27
store i32 %28, i32* %7
br label %29
29: ; preds = %26, %18
%30 = load i32, i32* %8
%31 = srem i32 %30, 2
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* %8
%35 = sub nsw i32 0, %34
store i32 %35, i32* %8
br label %36
36: ; preds = %33, %29
%37 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = call i32 @sprintf(i8* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %38, i32 %39, i32 0)
%41 = load i32, i32* %6
%42 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%43 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @write(i32 %41, i8* %42, i32 %44)
%46 = load i32, i32* %6
%47 = call i32 @fsync(i32 %46)
%48 = call i32 @sleep(i32 1)
br label %18
}

LLVM IR (-O2):
@.str = [38 x i8] c"/sys/devices/platform/vms/coordinates\00"
@O_RDWR = global i32 0
@.str.1 = [35 x i8] c"Couldn't open vms coordinate file\0A\00"
@.str.2 = [9 x i8] c"%d %d %d\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %2, %11
%12 = call i32 (...) @random()
%13 = srem i32 %12, 20
%14 = call i32 (...) @random()
%15 = srem i32 %14, 20
%16 = and i32 %13, 1
%17 = icmp eq i32 %16, 0
%18 = sub nsw i32 0, %13
%19 = select i1 %17, i32 %13, i32 %18
%20 = and i32 %15, 1
%21 = icmp eq i32 %20, 0
%22 = sub nsw i32 0, %15
%23 = select i1 %21, i32 %15, i32 %22
%24 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %23, i32 0)
%25 = call i32 @strlen(i8* %4)
%26 = call i32 @write(i32 %6, i8* %4, i32 %25)
%27 = call i32 @fsync(i32 %6)
%28 = call i32 @sleep(i32 1)
br label %11
}