#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 uint32_t;
typedef unsigned long off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/ *, unsigned long, int, int /*<<< orphan*/, int,
           unsigned long);
int munmap(void *, unsigned long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int main(int argc, char **argv) {
  int fd;
  void *map_base, *virt_addr;
  uint32_t read_result, write_result;
  uint32_t writeval = 't';
  off_t address = 0x00000001;
  int retval = -1;
  if ((fd = open("/dev/gpiomem", O_RDWR)) == -1) {
    perror("cannot open /dev/gpiomem");
    goto close;
  }
  map_base = mmap(NULL, 4096UL, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  address & ~(4096UL - 1));
  if (map_base == MAP_FAILED) {
    perror("cannot map gpio memory");
    goto unmap;
  }
  printf("Memory mapped at address %p.\n", map_base);
  virt_addr = (char *)map_base + (address & (4096UL - 1));
  read_result = *((uint32_t *)virt_addr);
  printf("Read value: %#010x\n", read_result);
  *((uint32_t *)virt_addr) = writeval;
  write_result = *((uint32_t *)virt_addr);
  printf("Written %#010x; readback %#010x\n", writeval, write_result);
  retval = 0;
unmap:
  if (munmap(map_base, 4096UL) == -1) {
    perror("cannot unmap gpio memory");
    retval = -1;
  }
close:
  close(fd);
  return retval;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x74,-0x34(%rbp)
movq   $0x1,-0x40(%rbp)
movl   $0xffffffff,-0x44(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11be <main+0x5e>
lea    0xe4c(%rip),%rdi
call   1080 <perror@plt>
jmp    12bf <main+0x15f>
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    0x2dfa(%rip),%rax
or     (%rax),%edx
mov    0x2e11(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x40(%rbp),%r9
and    $0xfffffffffffff000,%r9
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    121f <main+0xbf>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
jmp    1290 <main+0x130>
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
and    $0xfff,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%edx
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
jne    12ba <main+0x15a>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    12bf <main+0x15f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1224 <main+0xc4>
mov    0x2e6d(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
mov    $0x1000,%esi
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e02(%rip),%rax
cmp    (%rax),%rbx
je     122d <main+0xcd>
lea    0xe66(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x1(%rbx),%esi
lea    0xe71(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x74,0x1(%rbx)
lea    0xe70(%rip),%rdi
mov    $0x74,%esi
mov    $0x74,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1000,%esi
mov    %rbx,%rdi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
je     1241 <main+0xe1>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd5(%rip),%rdi
jmp    1248 <main+0xe8>
lea    0xde5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1203 <main+0xa3>
lea    0xe3b(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1215 <main+0xb5>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/gpiomem\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"cannot open /dev/gpiomem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [23 x i8] c"cannot map gpio memory\00"
@.str.3 = [30 x i8] c"Memory mapped at address %p.\0A\00"
@.str.4 = [20 x i8] c"Read value: %x\0A\00"
@.str.5 = [33 x i8] c"Written %x; readback %x\0A\00"
@.str.6 = [25 x i8] c"cannot unmap gpio memory\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 i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 116, i32* %11
store i64 1, i64* %12
store i32 -1, i32* %13
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %62
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 = load i64, i64* %12
%26 = and i64 %25, -4096
%27 = call i8* @mmap(i32* null, i64 4096, i32 %22, i32 %23, i32 %24, i64 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = load i8*, i8** @MAP_FAILED
%30 = icmp eq i8* %28, %29
br i1 %30, label %31, label %33
31: ; preds = %19
%32 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %55
33: ; preds = %19
%34 = load i8*, i8** %7
%35 = ptrtoint i8* %34 to i32
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = load i8*, i8** %7
%38 = load i64, i64* %12
%39 = and i64 %38, 4095
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8* %40, i8** %8
%41 = load i8*, i8** %8
%42 = bitcast i8* %41 to i32*
%43 = load i32, i32* %42
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %11
%47 = load i8*, i8** %8
%48 = bitcast i8* %47 to i32*
store i32 %46, i32* %48
%49 = load i8*, i8** %8
%50 = bitcast i8* %49 to i32*
%51 = load i32, i32* %50
store i32 %51, i32* %10
%52 = load i32, i32* %11
%53 = load i32, i32* %10
%54 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %52, i32 %53)
store i32 0, i32* %13
br label %55
55: ; preds = %33, %31
%56 = load i8*, i8** %7
%57 = call i32 @munmap(i8* %56, i64 4096)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %13
br label %61
61: ; preds = %59, %55
br label %62
62: ; preds = %61, %17
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* %13
ret i32 %65
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/gpiomem\00"
@O_RDWR = global i32 0
@.str.1 = [25 x i8] c"cannot open /dev/gpiomem\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [23 x i8] c"cannot map gpio memory\00"
@.str.3 = [30 x i8] c"Memory mapped at address %p.\0A\00"
@.str.4 = [20 x i8] c"Read value: %x\0A\00"
@.str.5 = [33 x i8] c"Written %x; readback %x\0A\00"
@.str.6 = [25 x i8] c"cannot unmap gpio memory\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %29, label %6
6: ; preds = %2
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %8, %7
%10 = load i32, i32* @MAP_SHARED
%11 = tail call i8* @mmap(i32* null, i64 4096, i32 %9, i32 %10, i32 %4, i64 0)
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %16
14: ; preds = %6
%15 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %25
16: ; preds = %6
%17 = ptrtoint i8* %11 to i64
%18 = trunc i64 %17 to i32
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds i8, i8* %11, i64 1
%21 = bitcast i8* %20 to i32*
%22 = load i32, i32* %21
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %22)
store i32 116, i32* %21
%24 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 116, i32 116)
br label %25
25: ; preds = %16, %14
%26 = phi i32 [ -1, %14 ], [ 0, %16 ]
%27 = tail call i32 @munmap(i8* %11, i64 4096)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25, %2
%30 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), %25 ]
%31 = tail call i32 @perror(i8* %30)
br label %32
32: ; preds = %29, %25
%33 = phi i32 [ %26, %25 ], [ -1, %29 ]
%34 = tail call i32 @close(i32 %4)
ret i32 %33
}