#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 /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned long);
int read(int, void *, size_t);
int read_kmem(off_t offset, void *buf, size_t count) {
  int fd;
  int n;
  fd = open("/dev/kmem", O_RDONLY);
  if (fd < 0) {
    perror("open /dev/kmem failed");
    return -1;
  }
  lseek(fd, offset, SEEK_SET);
  n = read(fd, buf, count);
  if (n != count)
    perror("/dev/kmem read failed");
  else
    printf("/dev/kmem read buf = %ld\n", *(unsigned long *)buf);
  close(fd);
  return n;
}


x86 assembly (clang, -O0):
<read_kmem>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ad <read_kmem+0x4d>
lea    0xe6e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <read_kmem+0xb4>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11f3 <read_kmem+0x93>
lea    0xe37(%rip),%rdi
call   1080 <perror@plt>
jmp    1206 <read_kmem+0xa6>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe35(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_kmem>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe81(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11d5 <read_kmem+0x75>
mov    %eax,%ebx
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1060 <read@plt>
mov    %eax,%ebp
cltq
cmp    %r15,%rax
jne    11e8 <read_kmem+0x88>
mov    (%r14),%rsi
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2e(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11c8 <read_kmem+0x68>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
jmp    11c1 <read_kmem+0x61>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/kmem\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"open /dev/kmem failed\00"
@SEEK_SET = global i32 0
@.str.2 = [22 x i8] c"/dev/kmem read failed\00"
@.str.3 = [26 x i8] c"/dev/kmem read buf = %ld\0A\00"
define i32 @read_kmem(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %40
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i32, i32* %5
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @lseek(i32 %17, i32 %18, i32 %19)
%21 = load i32, i32* %8
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = call i32 @read(i32 %21, i8* %22, i64 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = load i64, i64* %7
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %36
31: ; preds = %16
%32 = load i8*, i8** %6
%33 = bitcast i8* %32 to i64*
%34 = load i64, i64* %33
%35 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %34)
br label %36
36: ; preds = %31, %29
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %9
store i32 %39, i32* %4
br label %40
40: ; preds = %36, %14
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/kmem\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"open /dev/kmem failed\00"
@SEEK_SET = global i32 0
@.str.2 = [22 x i8] c"/dev/kmem read failed\00"
@.str.3 = [26 x i8] c"/dev/kmem read buf = %ld\0A\00"
define i32 @read_kmem(i32 %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %23
9: ; preds = %3
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %5, i32 %0, i32 %10)
%12 = tail call i32 @read(i32 %5, i8* %1, i64 %2)
%13 = sext i32 %12 to i64
%14 = icmp eq i64 %13, %2
br i1 %14, label %17, label %15
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %21
17: ; preds = %9
%18 = bitcast i8* %1 to i64*
%19 = load i64, i64* %18
%20 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %19)
br label %21
21: ; preds = %17, %15
%22 = tail call i32 @close(i32 %5)
br label %23
23: ; preds = %21, %7
%24 = phi i32 [ -1, %7 ], [ %12, %21 ]
ret i32 %24
}