#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 uint8_t;
typedef int uint32_t;
typedef int uint16_t;
typedef int int32_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ MAP_SHARED;
double M_PI;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ floor(int);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int volatile, int volatile);
int sinf(double);
int /*<<< orphan*/ sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int main() {
  int fd, i;
  volatile void *cfg;
  volatile uint32_t *sts, *fifo;
  volatile uint16_t *level;
  volatile uint8_t *rst;
  int32_t value;
  if ((fd = open("/dev/mem", O_RDWR)) < 0) {
    perror("open");
    return EXIT_FAILURE;
  }
  sts = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, MAP_SHARED,
             fd, 0x40000000);
  cfg = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, MAP_SHARED,
             fd, 0x40001000);
  fifo = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ | PROT_WRITE, MAP_SHARED,
              fd, 0x40002000);
  rst = (uint8_t *)(cfg + 0);
  level = (uint16_t *)(cfg + 2);
  *level = 21990;
  *rst &= ~3;
  *rst |= 2;
  for (i = 0; i < 10; ++i)
    *fifo = 0;
  usleep(100);
  printf("%d %d\n", sts[0], sts[1]);
  *rst |= 1;
  while (1) {
    while (sts[0] > 544)
      usleep(100);
    for (i = 0; i < 480; ++i) {
      value = (int32_t)floor(sinf(2.0 * M_PI * i / 48) * 32000 + 0.5);
      *fifo = value;
    }
    printf("%d %d\n", sts[0], sts[1]);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11cb <main+0x4b>
lea    0xe6a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%esi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    0x2dc5(%rip),%rax
or     (%rax),%edx
mov    0x2df4(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x40000000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%esi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    0x2d83(%rip),%rax
or     (%rax),%edx
mov    0x2db2(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x40001000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
mov    0x2d41(%rip),%rax
or     (%rax),%edx
mov    0x2d70(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x40002000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movl   $0x55e6,(%rax)
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
and    $0xfffffffc,%ecx
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
or     $0x2,%ecx
mov    %ecx,(%rax)
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    12ee <main+0x16e>
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12cc <main+0x14c>
mov    $0x64,%edi
call   10a0 <usleep@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xd1a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%rbp),%rax
mov    (%rax),%ecx
or     $0x1,%ecx
mov    %ecx,(%rax)
jmp    1321 <main+0x1a1>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    $0x220,%eax
jle    1341 <main+0x1c1>
mov    $0x64,%edi
call   10a0 <usleep@plt>
jmp    1321 <main+0x1a1>
movl   $0x0,-0xc(%rbp)
cmpl   $0x1e0,-0xc(%rbp)
jge    13bb <main+0x23b>
movsd  0xcb3(%rip),%xmm0
mov    0x2c6c(%rip),%rax
mulsd  (%rax),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movsd  0xc8f(%rip),%xmm1
divsd  %xmm1,%xmm0
call   1030 <sinf@plt>
imul   $0x7d00,%eax,%eax
cvtsi2sd %eax,%xmm0
movsd  0xc6c(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1090 <floor@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1348 <main+0x1c8>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
lea    0xc57(%rip),%rdi
call   1050 <printf@plt>
jmp    131c <main+0x19c>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1366 <main+0x1e6>
mov    %eax,%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    %rax,%r12
call   1080 <sysconf@plt>
mov    0x2e36(%rip),%rcx
mov    0x2de7(%rip),%r13
mov    0x0(%r13),%edx
or     (%rcx),%edx
mov    %rcx,%rbp
mov    0x2e0f(%rip),%r15
mov    (%r15),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebx,%r8d
mov    $0x40000000,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    (%r12),%edi
call   1080 <sysconf@plt>
mov    0x0(%r13),%edx
or     0x0(%rbp),%edx
mov    (%r15),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebx,%r8d
mov    $0x40001000,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    0x0(%r13),%edx
or     0x0(%rbp),%edx
mov    (%r15),%ecx
xor    %edi,%edi
mov    %eax,%esi
mov    %ebx,%r8d
mov    $0x40002000,%r9d
call   1040 <mmap@plt>
movl   $0x55e6,0x2(%r12)
andl   $0xfffffffc,(%r12)
orl    $0x2,(%r12)
mov    %rax,%rbx
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
movl   $0x0,(%rax)
mov    $0x64,%edi
call   10a0 <usleep@plt>
mov    (%r14),%esi
mov    0x4(%r14),%edx
lea    0xd79(%rip),%r15
mov    %r15,%rdi
call   1050 <printf@plt>
orl    $0x1,(%r12)
mov    0x2d0f(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r14),%eax
cmp    $0x221,%eax
jl     12f4 <main+0x174>
nopw   0x0(%rax,%rax,1)
mov    $0x64,%edi
call   10a0 <usleep@plt>
mov    (%r14),%eax
cmp    $0x220,%eax
jg     12e0 <main+0x160>
xorpd  %xmm1,%xmm1
mov    $0x1e0,%ebp
nopl   (%rax)
movsd  %xmm1,(%rsp)
movsd  (%r12),%xmm0
addsd  %xmm0,%xmm0
mulsd  %xmm1,%xmm0
divsd  0xce5(%rip),%xmm0
call   1030 <sinf@plt>
imul   $0x7d00,%eax,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
addsd  0xcd3(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1090 <floor@plt>
movsd  (%rsp),%xmm1
mov    %eax,(%rbx)
addsd  0xcc3(%rip),%xmm1
add    $0xffffffff,%ebp
jne    1300 <main+0x180>
mov    (%r14),%esi
mov    0x4(%r14),%edx
mov    %r15,%rdi
call   1050 <printf@plt>
jmp    12d0 <main+0x150>
lea    0xcb4(%rip),%rdi
call   1070 <perror@plt>
mov    0x2c3f(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [7 x i8] c"%d %d\0A\00"
@M_PI = global double 0.000000e+00
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i32 0, i32* %1
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
ret i32 %15
16: ; preds = %0
%17 = load i32, i32* @_SC_PAGESIZE
%18 = call i32 @sysconf(i32 %17)
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %2
%24 = call i8* @mmap(i32* null, i32 %18, i32 %21, i32 %22, i32 %23, i32 1073741824)
%25 = bitcast i8* %24 to i32*
store i32* %25, i32** %5
%26 = load i32, i32* @_SC_PAGESIZE
%27 = call i32 @sysconf(i32 %26)
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @PROT_WRITE
%30 = or i32 %28, %29
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %2
%33 = call i8* @mmap(i32* null, i32 %27, i32 %30, i32 %31, i32 %32, i32 1073745920)
store i8* %33, i8** %4
%34 = load i32, i32* @_SC_PAGESIZE
%35 = call i32 @sysconf(i32 %34)
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %36, %37
%39 = load i32, i32* @MAP_SHARED
%40 = load i32, i32* %2
%41 = call i8* @mmap(i32* null, i32 %35, i32 %38, i32 %39, i32 %40, i32 1073750016)
%42 = bitcast i8* %41 to i32*
store i32* %42, i32** %6
%43 = load i8*, i8** %4
%44 = getelementptr i8, i8* %43, i64 0
%45 = bitcast i8* %44 to i32*
store i32* %45, i32** %8
%46 = load i8*, i8** %4
%47 = getelementptr i8, i8* %46, i64 2
%48 = bitcast i8* %47 to i32*
store i32* %48, i32** %7
%49 = load i32*, i32** %7
store volatile i32 21990, i32* %49
%50 = load i32*, i32** %8
%51 = load volatile i32, i32* %50
%52 = and i32 %51, -4
store volatile i32 %52, i32* %50
%53 = load i32*, i32** %8
%54 = load volatile i32, i32* %53
%55 = or i32 %54, 2
store volatile i32 %55, i32* %53
store i32 0, i32* %3
br label %56
56: ; preds = %61, %16
%57 = load i32, i32* %3
%58 = icmp slt i32 %57, 10
br i1 %58, label %59, label %64
59: ; preds = %56
%60 = load i32*, i32** %6
store volatile i32 0, i32* %60
br label %61
61: ; preds = %59
%62 = load i32, i32* %3
%63 = add nsw i32 %62, 1
store i32 %63, i32* %3
br label %56
64: ; preds = %56
%65 = call i32 @usleep(i32 100)
%66 = load i32*, i32** %5
%67 = getelementptr inbounds i32, i32* %66, i64 0
%68 = load volatile i32, i32* %67
%69 = load i32*, i32** %5
%70 = getelementptr inbounds i32, i32* %69, i64 1
%71 = load volatile i32, i32* %70
%72 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %68, i32 %71)
%73 = load i32*, i32** %8
%74 = load volatile i32, i32* %73
%75 = or i32 %74, 1
store volatile i32 %75, i32* %73
br label %76
76: ; preds = %64, %107
br label %77
77: ; preds = %82, %76
%78 = load i32*, i32** %5
%79 = getelementptr inbounds i32, i32* %78, i64 0
%80 = load volatile i32, i32* %79
%81 = icmp sgt i32 %80, 544
br i1 %81, label %82, label %84
82: ; preds = %77
%83 = call i32 @usleep(i32 100)
br label %77
84: ; preds = %77
store i32 0, i32* %3
br label %85
85: ; preds = %104, %84
%86 = load i32, i32* %3
%87 = icmp slt i32 %86, 480
br i1 %87, label %88, label %107
88: ; preds = %85
%89 = load double, double* @M_PI
%90 = fmul double 2.000000e+00, %89
%91 = load i32, i32* %3
%92 = sitofp i32 %91 to double
%93 = fmul double %90, %92
%94 = fdiv double %93, 4.800000e+01
%95 = call i32 @sinf(double %94)
%96 = mul nsw i32 %95, 32000
%97 = sitofp i32 %96 to double
%98 = fadd double %97, 5.000000e-01
%99 = fptosi double %98 to i32
%100 = call i64 @floor(i32 %99)
%101 = trunc i64 %100 to i32
store i32 %101, i32* %9
%102 = load i32, i32* %9
%103 = load i32*, i32** %6
store volatile i32 %102, i32* %103
br label %104
104: ; preds = %88
%105 = load i32, i32* %3
%106 = add nsw i32 %105, 1
store i32 %106, i32* %3
br label %85
107: ; preds = %85
%108 = load i32*, i32** %5
%109 = getelementptr inbounds i32, i32* %108, i64 0
%110 = load volatile i32, i32* %109
%111 = load i32*, i32** %5
%112 = getelementptr inbounds i32, i32* %111, i64 1
%113 = load volatile i32, i32* %112
%114 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %110, i32 %113)
br label %76
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [7 x i8] c"%d %d\0A\00"
@M_PI = global double 0.000000e+00
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
ret i32 %6
7: ; preds = %0
%8 = load i32, i32* @_SC_PAGESIZE
%9 = tail call i32 @sysconf(i32 %8)
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i8* @mmap(i32* null, i32 %9, i32 %12, i32 %13, i32 %2, i32 1073741824)
%15 = load i32, i32* @_SC_PAGESIZE
%16 = tail call i32 @sysconf(i32 %15)
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_SHARED
%21 = tail call i8* @mmap(i32* null, i32 %16, i32 %19, i32 %20, i32 %2, i32 1073745920)
%22 = load i32, i32* @_SC_PAGESIZE
%23 = tail call i32 @sysconf(i32 %22)
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %25, %24
%27 = load i32, i32* @MAP_SHARED
%28 = tail call i8* @mmap(i32* null, i32 %23, i32 %26, i32 %27, i32 %2, i32 1073750016)
%29 = bitcast i8* %28 to i32*
%30 = bitcast i8* %21 to i32*
%31 = getelementptr i8, i8* %21, i64 2
%32 = bitcast i8* %31 to i32*
store volatile i32 21990, i32* %32
%33 = load volatile i32, i32* %30
%34 = and i32 %33, -4
store volatile i32 %34, i32* %30
%35 = load volatile i32, i32* %30
%36 = or i32 %35, 2
store volatile i32 %36, i32* %30
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
store volatile i32 0, i32* %29
%37 = bitcast i8* %14 to i32*
%38 = tail call i32 @usleep(i32 100)
%39 = load volatile i32, i32* %37
%40 = getelementptr inbounds i8, i8* %14, i64 4
%41 = bitcast i8* %40 to i32*
%42 = load volatile i32, i32* %41
%43 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %39, i32 %42)
%44 = load volatile i32, i32* %30
%45 = or i32 %44, 1
store volatile i32 %45, i32* %30
br label %46
46: ; preds = %70, %7
%47 = load volatile i32, i32* %37
%48 = icmp sgt i32 %47, 544
br i1 %48, label %49, label %53
49: ; preds = %46, %49
%50 = tail call i32 @usleep(i32 100)
%51 = load volatile i32, i32* %37
%52 = icmp sgt i32 %51, 544
br i1 %52, label %49, label %53
53: ; preds = %49, %46
br label %54
54: ; preds = %53, %54
%55 = phi i32 [ %68, %54 ], [ 0, %53 ]
%56 = load double, double* @M_PI
%57 = fmul double %56, 2.000000e+00
%58 = sitofp i32 %55 to double
%59 = fmul double %57, %58
%60 = fdiv double %59, 4.800000e+01
%61 = tail call i32 @sinf(double %60)
%62 = mul nsw i32 %61, 32000
%63 = sitofp i32 %62 to double
%64 = fadd double %63, 5.000000e-01
%65 = fptosi double %64 to i32
%66 = tail call i64 @floor(i32 %65)
%67 = trunc i64 %66 to i32
store volatile i32 %67, i32* %29
%68 = add nuw nsw i32 %55, 1
%69 = icmp eq i32 %68, 480
br i1 %69, label %70, label %54
70: ; preds = %54
%71 = load volatile i32, i32* %37
%72 = load volatile i32, i32* %41
%73 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %71, i32 %72)
br label %46
}