#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 getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, const char *argv[]) {
  int fd, c = 0;
  char b, fstr[32] = "";
  sprintf(fstr, "/proc/%d/mem", getpid());
  fd = open(fstr, O_RDONLY);
  while (read(fd, &b, 1) == 1) {
    printf("READ: %c\n", b);
    ++c;
  }
  printf("End. %d\n", c);
  return 0;
}


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   $0x0,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xe57(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    11fb <main+0x9b>
movsbl -0x19(%rbp),%esi
lea    0xe25(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c3 <main+0x63>
mov    -0x18(%rbp),%esi
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe89(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   1070 <sprintf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
lea    0xf(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11e4 <main+0x94>
xor    %ebx,%ebx
lea    0xe55(%rip),%r14
lea    0xf(%rsp),%r15
nopl   (%rax)
movsbl 0xf(%rsp),%esi
mov    %r14,%rdi
call   1040 <printf@plt>
add    $0x1,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
je     11c0 <main+0x70>
lea    0xe2c(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/proc/%d/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"READ: %c\0A\00"
@.str.2 = [9 x i8] c"End. %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
%10 = bitcast [32 x i8]* %9 to i8*
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%12 = call i32 (...) @getpid()
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
br label %17
17: ; preds = %21, %2
%18 = load i32, i32* %6
%19 = call i32 @read(i32 %18, i8* %8, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i8, i8* %8
%23 = sext i8 %22 to i32
%24 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %17
27: ; preds = %17
%28 = load i32, i32* %7
%29 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"/proc/%d/mem\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"READ: %c\0A\00"
@.str.2 = [9 x i8] c"End. %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (...) @getpid()
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %5, i32 %8)
%10 = call i32 @read(i32 %9, i8* %3, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %20
12: ; preds = %2, %12
%13 = phi i32 [ %17, %12 ], [ 0, %2 ]
%14 = load i8, i8* %3
%15 = sext i8 %14 to i32
%16 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = add nuw nsw i32 %13, 1
%18 = call i32 @read(i32 %9, i8* %3, i32 1)
%19 = icmp eq i32 %18, 1
br i1 %19, label %12, label %20
20: ; preds = %12, %2
%21 = phi i32 [ 0, %2 ], [ %17, %12 ]
%22 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %21)
ret i32 0
}