#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*/ uint64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
int /*<<< orphan*/ stderr;
int u64_fseek(int /*<<< orphan*/ *, int /*<<< orphan*/);
int read_mem_rand(char **line, size_t *n, FILE *m, uint64_t off) {
  int p = u64_fseek(m, off);
  if (p == -1) {
    perror("fseek /proc/$pid/mem failed");
    return -1;
  }
  unsigned char v[16];
  if (*n < sizeof v * 3 + 1) {
    fprintf(stderr, "internal read mem error\n");
    return -1;
  }
  size_t r = fread(v, 1, sizeof v, m);
  if (r != sizeof v) {
    if (ferror(m))
      perror(0);
    return -1;
  }
  char *s = *line;
  for (size_t i = 0; i < sizeof v; ++i, s += 3)
    sprintf(s, "%.2x ", (unsigned)v[i]);
  s[-1] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<read_mem_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <u64_fseek@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11a8 <read_mem_rand+0x48>
lea    0xe69(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <read_mem_rand+0x12a>
mov    -0x18(%rbp),%rax
cmpq   $0x31,(%rax)
jae    11d7 <read_mem_rand+0x77>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <read_mem_rand+0x12a>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10,%edx
call   1040 <fread@plt>
cltq
mov    %rax,-0x48(%rbp)
cmpq   $0x10,-0x48(%rbp)
je     1227 <read_mem_rand+0xc7>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     121b <read_mem_rand+0xbb>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <read_mem_rand+0x12a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
cmpq   $0x10,-0x58(%rbp)
jae    127b <read_mem_rand+0x11b>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
movzbl -0x40(%rbp,%rax,1),%edx
lea    0xddc(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x50(%rbp)
jmp    123a <read_mem_rand+0xda>
mov    -0x50(%rbp),%rax
movb   $0x0,-0x1(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mem_rand>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%rdi
mov    %ecx,%esi
call   1070 <u64_fseek@plt>
cmp    $0xffffffff,%eax
je     1306 <read_mem_rand+0x1a6>
cmpq   $0x30,(%rbx)
ja     11aa <read_mem_rand+0x4a>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe81(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    12fa <read_mem_rand+0x19a>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x10,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    $0x10,%eax
jne    12e8 <read_mem_rand+0x188>
mov    (%r14),%rbx
movzbl (%rsp),%edx
lea    0xe5f(%rip),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x3(%rbx),%rdi
movzbl 0x1(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x6(%rbx),%rdi
movzbl 0x2(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x9(%rbx),%rdi
movzbl 0x3(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0xc(%rbx),%rdi
movzbl 0x4(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0xf(%rbx),%rdi
movzbl 0x5(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x12(%rbx),%rdi
movzbl 0x6(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x15(%rbx),%rdi
movzbl 0x7(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x18(%rbx),%rdi
movzbl 0x8(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x1b(%rbx),%rdi
movzbl 0x9(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x1e(%rbx),%rdi
movzbl 0xa(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x21(%rbx),%rdi
movzbl 0xb(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x24(%rbx),%rdi
movzbl 0xc(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x27(%rbx),%rdi
movzbl 0xd(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x2a(%rbx),%rdi
movzbl 0xe(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
lea    0x2d(%rbx),%rdi
movzbl 0xf(%rsp),%edx
mov    %r14,%rsi
call   1080 <sprintf@plt>
movb   $0x0,0x2f(%rbx)
xor    %ebx,%ebx
jmp    12fa <read_mem_rand+0x19a>
mov    %r15,%rdi
call   1030 <ferror@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1317 <read_mem_rand+0x1b7>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xcf3(%rip),%rdi
call   1060 <perror@plt>
jmp    11a0 <read_mem_rand+0x40>
xor    %edi,%edi
call   1060 <perror@plt>
jmp    12fa <read_mem_rand+0x19a>

LLVM IR (-O0):
@.str = [28 x i8] c"fseek /proc/$pid/mem failed\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"internal read mem error\0A\00"
@.str.2 = [6 x i8] c"%.2x \00"
define i32 @read_mem_rand(i8** %0, i64* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [16 x i8]
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i64
store i8** %0, i8*** %6
store i64* %1, i64** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
%15 = load i32*, i32** %8
%16 = load i32, i32* %9
%17 = call i32 @u64_fseek(i32* %15, i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %4
%21 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %64
22: ; preds = %4
%23 = load i64*, i64** %7
%24 = load i64, i64* %23
%25 = icmp ult i64 %24, 49
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %64
29: ; preds = %22
%30 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%31 = load i32*, i32** %8
%32 = call i32 @fread(i8* %30, i32 1, i32 16, i32* %31)
%33 = sext i32 %32 to i64
store i64 %33, i64* %12
%34 = load i64, i64* %12
%35 = icmp ne i64 %34, 16
br i1 %35, label %36, label %43
36: ; preds = %29
%37 = load i32*, i32** %8
%38 = call i64 @ferror(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* null)
br label %42
42: ; preds = %40, %36
store i32 -1, i32* %5
br label %64
43: ; preds = %29
%44 = load i8**, i8*** %6
%45 = load i8*, i8** %44
store i8* %45, i8** %13
store i64 0, i64* %14
br label %46
46: ; preds = %56, %43
%47 = load i64, i64* %14
%48 = icmp ult i64 %47, 16
br i1 %48, label %49, label %61
49: ; preds = %46
%50 = load i8*, i8** %13
%51 = load i64, i64* %14
%52 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 %51
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = call i32 @sprintf(i8* %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %49
%57 = load i64, i64* %14
%58 = add i64 %57, 1
store i64 %58, i64* %14
%59 = load i8*, i8** %13
%60 = getelementptr inbounds i8, i8* %59, i64 3
store i8* %60, i8** %13
br label %46
61: ; preds = %46
%62 = load i8*, i8** %13
%63 = getelementptr inbounds i8, i8* %62, i64 -1
store i8 0, i8* %63
store i32 0, i32* %5
br label %64
64: ; preds = %61, %42, %26, %20
%65 = load i32, i32* %5
ret i32 %65
}

LLVM IR (-O2):
@.str = [28 x i8] c"fseek /proc/$pid/mem failed\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"internal read mem error\0A\00"
@.str.2 = [6 x i8] c"%.2x \00"
define i32 @read_mem_rand(i8** nocapture %0, i64* nocapture %1, i32* %2, i32 %3) {
%5 = alloca [16 x i8]
%6 = tail call i32 @u64_fseek(i32* %2, i32 %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %108
10: ; preds = %4
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%12 = load i64, i64* %1
%13 = icmp ult i64 %12, 49
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %106
17: ; preds = %10
%18 = call i32 @fread(i8* %11, i32 1, i32 16, i32* %2)
%19 = icmp eq i32 %18, 16
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = tail call i64 @ferror(i32* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %106, label %23
23: ; preds = %20
%24 = tail call i32 @perror(i8* null)
br label %106
25: ; preds = %17
%26 = load i8*, i8** %0
%27 = load i8, i8* %11
%28 = zext i8 %27 to i32
%29 = tail call i32 @sprintf(i8* dereferenceable(1) %26, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds i8, i8* %26, i64 3
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 1
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = tail call i32 @sprintf(i8* dereferenceable(1) %30, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds i8, i8* %26, i64 6
%36 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 2
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = tail call i32 @sprintf(i8* dereferenceable(1) %35, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds i8, i8* %26, i64 9
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 3
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = tail call i32 @sprintf(i8* dereferenceable(1) %40, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = getelementptr inbounds i8, i8* %26, i64 12
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 4
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = tail call i32 @sprintf(i8* dereferenceable(1) %45, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %48)
%50 = getelementptr inbounds i8, i8* %26, i64 15
%51 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 5
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = tail call i32 @sprintf(i8* dereferenceable(1) %50, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %53)
%55 = getelementptr inbounds i8, i8* %26, i64 18
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 6
%57 = load i8, i8* %56
%58 = zext i8 %57 to i32
%59 = tail call i32 @sprintf(i8* dereferenceable(1) %55, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = getelementptr inbounds i8, i8* %26, i64 21
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 7
%62 = load i8, i8* %61
%63 = zext i8 %62 to i32
%64 = tail call i32 @sprintf(i8* dereferenceable(1) %60, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %63)
%65 = getelementptr inbounds i8, i8* %26, i64 24
%66 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 8
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = tail call i32 @sprintf(i8* dereferenceable(1) %65, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %68)
%70 = getelementptr inbounds i8, i8* %26, i64 27
%71 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 9
%72 = load i8, i8* %71
%73 = zext i8 %72 to i32
%74 = tail call i32 @sprintf(i8* dereferenceable(1) %70, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %73)
%75 = getelementptr inbounds i8, i8* %26, i64 30
%76 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 10
%77 = load i8, i8* %76
%78 = zext i8 %77 to i32
%79 = tail call i32 @sprintf(i8* dereferenceable(1) %75, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %78)
%80 = getelementptr inbounds i8, i8* %26, i64 33
%81 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 11
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = tail call i32 @sprintf(i8* dereferenceable(1) %80, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %83)
%85 = getelementptr inbounds i8, i8* %26, i64 36
%86 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 12
%87 = load i8, i8* %86
%88 = zext i8 %87 to i32
%89 = tail call i32 @sprintf(i8* dereferenceable(1) %85, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %88)
%90 = getelementptr inbounds i8, i8* %26, i64 39
%91 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 13
%92 = load i8, i8* %91
%93 = zext i8 %92 to i32
%94 = tail call i32 @sprintf(i8* dereferenceable(1) %90, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %93)
%95 = getelementptr inbounds i8, i8* %26, i64 42
%96 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 14
%97 = load i8, i8* %96
%98 = zext i8 %97 to i32
%99 = tail call i32 @sprintf(i8* dereferenceable(1) %95, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %98)
%100 = getelementptr inbounds i8, i8* %26, i64 45
%101 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 15
%102 = load i8, i8* %101
%103 = zext i8 %102 to i32
%104 = tail call i32 @sprintf(i8* dereferenceable(1) %100, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %103)
%105 = getelementptr inbounds i8, i8* %26, i64 47
store i8 0, i8* %105
br label %106
106: ; preds = %25, %23, %20, %14
%107 = phi i32 [ -1, %14 ], [ 0, %25 ], [ -1, %23 ], [ -1, %20 ]
br label %108
108: ; preds = %106, %8
%109 = phi i32 [ -1, %8 ], [ %107, %106 ]
ret i32 %109
}