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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
unsigned int fread(void *, int, unsigned int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, unsigned long, int /*<<< orphan*/);
unsigned int fwrite(void *, int, unsigned int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int kmem_generic(int b_read, unsigned long offset, void *buf,
                 unsigned int count) {
  int rc = -1;
  FILE *fp = NULL;
  fp = fopen("/dev/kmem", "rb+");
  if (!fp) {
    printf("ERROR: fopen()\n");
    goto cleanup;
  }
  if (fseek(fp, offset, SEEK_SET)) {
    printf("ERROR: fseek()\n");
    goto cleanup;
  }
  if (b_read) {
    if (fread(buf, 1, count, fp) != count) {
      printf("ERROR: fread()\n");
      goto cleanup;
    }
  } else {
    if (fwrite(buf, 1, count, fp) != count) {
      printf("ERROR: fwrite()\n");
      goto cleanup;
    }
  }
  rc = 0;
cleanup:
  if (fp) {
    fclose(fp);
  }
  return rc;
}


x86 assembly (clang, -O0):
<kmem_generic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe74(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <kmem_generic+0x58>
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
jmp    1262 <kmem_generic+0x102>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     11e9 <kmem_generic+0x89>
lea    0xe3f(%rip),%rdi
call   1050 <printf@plt>
jmp    1262 <kmem_generic+0x102>
cmpl   $0x0,-0x4(%rbp)
je     1227 <kmem_generic+0xc7>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x1c(%rbp),%eax
je     1222 <kmem_generic+0xc2>
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
jmp    1262 <kmem_generic+0x102>
jmp    125b <kmem_generic+0xfb>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
cmp    -0x1c(%rbp),%eax
je     1256 <kmem_generic+0xf6>
lea    0xdf2(%rip),%rdi
call   1050 <printf@plt>
jmp    1262 <kmem_generic+0x102>
jmp    125b <kmem_generic+0xfb>
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1276 <kmem_generic+0x116>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kmem_generic>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %edi,%r12d
lea    0xe85(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c3 <kmem_generic+0x63>
mov    %rax,%rbx
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <fseek@plt>
test   %rax,%rax
je     11df <kmem_generic+0x7f>
lea    0xe8d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11d4 <kmem_generic+0x74>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbx,%rcx
test   %r12d,%r12d
je     1207 <kmem_generic+0xa7>
call   1040 <fread@plt>
xor    %ebp,%ebp
cmp    %r14d,%eax
je     11b9 <kmem_generic+0x59>
lea    0xe28(%rip),%rdi
jmp    11af <kmem_generic+0x4f>
call   1080 <fwrite@plt>
xor    %ebp,%ebp
cmp    %r14d,%eax
je     11b9 <kmem_generic+0x59>
lea    0xe03(%rip),%rdi
jmp    11af <kmem_generic+0x4f>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/kmem\00"
@.str.1 = [4 x i8] c"rb+\00"
@.str.2 = [16 x i8] c"ERROR: fopen()\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [16 x i8] c"ERROR: fseek()\0A\00"
@.str.4 = [16 x i8] c"ERROR: fread()\0A\00"
@.str.5 = [17 x i8] c"ERROR: fwrite()\0A\00"
define i32 @kmem_generic(i32 %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store i32 %0, i32* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 -1, i32* %9
store i32* null, i32** %10
%11 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %10
%12 = load i32*, i32** %10
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %48
16: ; preds = %4
%17 = load i32*, i32** %10
%18 = load i64, i64* %6
%19 = load i32, i32* @SEEK_SET
%20 = call i64 @fseek(i32* %17, i64 %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %48
24: ; preds = %16
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = load i8*, i8** %7
%29 = load i32, i32* %8
%30 = load i32*, i32** %10
%31 = call i32 @fread(i8* %28, i32 1, i32 %29, i32* %30)
%32 = load i32, i32* %8
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %48
36: ; preds = %27
br label %47
37: ; preds = %24
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = load i32*, i32** %10
%41 = call i32 @fwrite(i8* %38, i32 1, i32 %39, i32* %40)
%42 = load i32, i32* %8
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %46
44: ; preds = %37
%45 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %48
46: ; preds = %37
br label %47
47: ; preds = %46, %36
store i32 0, i32* %9
br label %48
48: ; preds = %47, %44, %34, %22, %14
%49 = load i32*, i32** %10
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32*, i32** %10
%53 = call i32 @fclose(i32* %52)
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* %9
ret i32 %55
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/kmem\00"
@.str.1 = [4 x i8] c"rb+\00"
@SEEK_SET = global i32 0
@str = [15 x i8] c"ERROR: fopen()\00"
@str.6 = [16 x i8] c"ERROR: fwrite()\00"
@str.7 = [15 x i8] c"ERROR: fread()\00"
@str.8 = [15 x i8] c"ERROR: fseek()\00"
define i32 @kmem_generic(i32 %0, i64 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %19, label %7
7: ; preds = %4
%8 = load i32, i32* @SEEK_SET
%9 = tail call i64 @fseek(i32* %5, i64 %1, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %7
%12 = icmp eq i32 %0, 0
br i1 %12, label %16, label %13
13: ; preds = %11
%14 = tail call i32 @fread(i8* %2, i32 1, i32 %3, i32* %5)
%15 = icmp eq i32 %14, %3
br i1 %15, label %24, label %21
16: ; preds = %11
%17 = tail call i32 @fwrite(i8* %2, i32 1, i32 %3, i32* %5)
%18 = icmp eq i32 %17, %3
br i1 %18, label %24, label %21
19: ; preds = %4
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %27
21: ; preds = %16, %13, %7
%22 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0), %7 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.7, i64 0, i64 0), %13 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0), %16 ]
%23 = tail call i32 @puts(i8* dereferenceable(1) %22)
br label %24
24: ; preds = %21, %13, %16
%25 = phi i32 [ 0, %13 ], [ 0, %16 ], [ -1, %21 ]
%26 = tail call i32 @fclose(i32* %5)
br label %27
27: ; preds = %19, %24
%28 = phi i32 [ %25, %24 ], [ -1, %19 ]
ret i32 %28
}