#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int, int);
int open(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (mem_fd == -1) {
    fprintf(stderr, "Can't open /dev/mem!\n");
    return -1;
  }
  void *p =
      mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0x53FCC000);
  if (p == MAP_FAILED) {
    fprintf(stderr, "mmap 0x%x failed.\n", 0x53FCC000);
    return -1;
  }
  volatile unsigned int *gpio1_dr = (unsigned int *)((unsigned int)p + 0);
  volatile unsigned int *gpio1_dir = (unsigned int *)((unsigned int)p + 4);
  *gpio1_dir |= (1 << 4);
  while (1) {
    *gpio1_dr |= (1 << 4);
    sleep(1);
    *gpio1_dr &= ~(1 << 4);
    sleep(1);
  }
  return 0;
}


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    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
lea    0xe91(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a4 <main+0x64>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <main+0x12f>
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0x53fcc000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
jne    1215 <main+0xd5>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    $0x53fcc000,%edx
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <main+0x12f>
mov    -0x20(%rbp),%rax
add    $0x0,%eax
mov    %eax,%eax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%eax
mov    %eax,%eax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%ecx
or     $0x10,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
or     $0x10,%ecx
mov    %ecx,-0x38(%rbp)
mov    %ecx,(%rax)
mov    $0x1,%edi
call   1060 <sleep@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
and    $0xffffffef,%ecx
mov    %ecx,-0x34(%rbp)
mov    %ecx,(%rax)
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    123a <main+0xfa>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e81(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea6(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11cc <main+0x8c>
mov    0x2e8d(%rip),%rcx
mov    0x2e46(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %eax,%r8d
mov    $0x53fcc000,%r9d
call   1030 <mmap@plt>
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
je     11e5 <main+0xa5>
mov    %eax,%ebx
add    $0x4,%eax
orl    $0x10,(%rax)
nopl   0x0(%rax,%rax,1)
orl    $0x10,(%rbx)
mov    $0x1,%edi
call   1060 <sleep@plt>
andl   $0xffffffef,(%rbx)
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11b0 <main+0x70>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1201 <main+0xc1>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    $0x53fcc000,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Can't open /dev/mem!\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [19 x i8] c"mmap 0x%x failed.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_SYNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %55
19: ; preds = %2
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %20, %21
%23 = load i32, i32* @MAP_SHARED
%24 = load i32, i32* %6
%25 = call i8* @mmap(i32 0, i32 4096, i32 %22, i32 %23, i32 %24, i32 1409073152)
store i8* %25, i8** %7
%26 = load i8*, i8** %7
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 1409073152)
store i32 -1, i32* %3
br label %55
32: ; preds = %19
%33 = load i8*, i8** %7
%34 = ptrtoint i8* %33 to i32
%35 = add i32 %34, 0
%36 = zext i32 %35 to i64
%37 = inttoptr i64 %36 to i32*
store i32* %37, i32** %8
%38 = load i8*, i8** %7
%39 = ptrtoint i8* %38 to i32
%40 = add i32 %39, 4
%41 = zext i32 %40 to i64
%42 = inttoptr i64 %41 to i32*
store i32* %42, i32** %9
%43 = load i32*, i32** %9
%44 = load volatile i32, i32* %43
%45 = or i32 %44, 16
store volatile i32 %45, i32* %43
br label %46
46: ; preds = %32, %46
%47 = load i32*, i32** %8
%48 = load volatile i32, i32* %47
%49 = or i32 %48, 16
store volatile i32 %49, i32* %47
%50 = call i32 @sleep(i32 1)
%51 = load i32*, i32** %8
%52 = load volatile i32, i32* %51
%53 = and i32 %52, -17
store volatile i32 %53, i32* %51
%54 = call i32 @sleep(i32 1)
br label %46
55: ; preds = %29, %16
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Can't open /dev/mem!\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [19 x i8] c"mmap 0x%x failed.\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %38
11: ; preds = %2
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i8* @mmap(i32 0, i32 4096, i32 %14, i32 %15, i32 %6, i32 1409073152)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %19, label %22
19: ; preds = %11
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 1409073152)
br label %38
22: ; preds = %11
%23 = ptrtoint i8* %16 to i64
%24 = and i64 %23, 4294967295
%25 = inttoptr i64 %24 to i32*
%26 = add i64 %23, 4
%27 = and i64 %26, 4294967295
%28 = inttoptr i64 %27 to i32*
%29 = load volatile i32, i32* %28
%30 = or i32 %29, 16
store volatile i32 %30, i32* %28
br label %31
31: ; preds = %31, %22
%32 = load volatile i32, i32* %25
%33 = or i32 %32, 16
store volatile i32 %33, i32* %25
%34 = tail call i32 @sleep(i32 1)
%35 = load volatile i32, i32* %25
%36 = and i32 %35, -17
store volatile i32 %36, i32* %25
%37 = tail call i32 @sleep(i32 1)
br label %31
38: ; preds = %19, %8
ret i32 -1
}