#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_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, unsigned long long);
int read(int, char *, int);
int main(int argc, const char *argv[]) {
  unsigned long long offset = 0;
  char b;
  int memfd = open("/dev/mem", O_RDONLY);
  while (read(memfd, &b, 1) == 1) {
    ++offset;
  }
  printf("%d\n", offset);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
lea    0xea2(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1191 <main+0x61>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1166 <main+0x36>
mov    -0x18(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
mov    $0xffffffffffffffff,%rbx
lea    0xf(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
add    $0x1,%rbx
cmp    $0x1,%eax
je     1160 <main+0x30>
lea    0xe8a(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 0, i64* %6
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %8
br label %11
11: ; preds = %15, %2
%12 = load i32, i32* %8
%13 = call i32 @read(i32 %12, i8* %7, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i64, i64* %6
%17 = add i64 %16, 1
store i64 %17, i64* %6
br label %11
18: ; preds = %11
%19 = load i64, i64* %6
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %19)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
br label %6
6: ; preds = %6, %2
%7 = phi i64 [ 0, %2 ], [ %10, %6 ]
%8 = call i32 @read(i32 %5, i8* %3, i32 1)
%9 = icmp eq i32 %8, 1
%10 = add i64 %7, 1
br i1 %9, label %6, label %11
11: ; preds = %6
%12 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64 %7)
ret i32 0
}