#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, long,
                           unsigned long volatile *, unsigned long,
                           unsigned long volatile, unsigned long **,
                           unsigned long *, unsigned long,
                           unsigned long volatile);
int getpid();
scalar_t__ malloc(int);
unsigned long mem_addr_vir2phy(unsigned long);
int /*<<< orphan*/ sleep(int);
long test() {
  volatile unsigned long a = 1;
  unsigned long *p = NULL;
  p = (unsigned long *)malloc(sizeof(unsigned long));
  *p = 1;
  unsigned long p_phy_addr = mem_addr_vir2phy((unsigned long)p);
  unsigned long a_phy_addr = mem_addr_vir2phy((unsigned long)&a);
  FILE *fp = fopen("hello.txt", "a+");
  long i = 0;
  while (1) {
    fprintf(fp,
            "pid = %d, %ld, a's addr = %p [0x%lx : %ld]"
            ", p point addr = %p q's addr = %p [0x%lx : %ld]\n",
            getpid(), i, &a, a_phy_addr, a, &p, p, p_phy_addr,
            (volatile unsigned long)*p);
    fflush(fp);
    sleep(1);
    i++;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
movq   $0x1,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x1,(%rax)
mov    -0x20(%rbp),%rdi
call   1050 <mem_addr_vir2phy@plt>
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <mem_addr_vir2phy@plt>
mov    %rax,-0x30(%rbp)
lea    0xe3b(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%r9
mov    -0x18(%rbp),%r14
mov    -0x20(%rbp),%r11
mov    -0x28(%rbp),%r10
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
lea    0xdf9(%rip),%rsi
lea    -0x18(%rbp),%r8
lea    -0x20(%rbp),%rbx
mov    %r14,(%rsp)
mov    %rbx,0x8(%rsp)
mov    %r11,0x10(%rsp)
mov    %r10,0x18(%rsp)
mov    %rax,0x20(%rsp)
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1070 <fflush@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    %rax,-0x40(%rbp)
jmp    11dd <test+0x6d>

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x1,0x8(%rsp)
mov    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,0x10(%rsp)
movq   $0x1,(%rax)
mov    %rax,%rdi
call   1050 <mem_addr_vir2phy@plt>
mov    %rax,%r15
lea    0x8(%rsp),%rdi
call   1050 <mem_addr_vir2phy@plt>
mov    %rax,%r12
lea    0xe44(%rip),%rdi
lea    0xe47(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
lea    0xe3b(%rip),%r13
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x10(%rsp),%rbx
sub    $0x8,%rsp
mov    %r14,%rdi
mov    %r13,%rsi
mov    %eax,%edx
mov    %rbp,%rcx
lea    0x10(%rsp),%r8
mov    %r12,%r9
push   (%rbx)
push   %r15
push   %rbx
lea    0x30(%rsp),%rax
push   %rax
push   0x30(%rsp)
call   1040 <fprintf@plt>
add    $0x30,%rsp
mov    %r14,%rdi
call   1070 <fflush@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
add    $0x1,%rbp
jmp    11e0 <test+0x70>

LLVM IR (-O0):
@.str = [10 x i8] c"hello.txt\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [91 x i8] c"pid = %d, %ld, a's addr = %p [0x%lx : %ld], p point addr = %p q's addr = %p [0x%lx : %ld]\0A\00"
define i64 @test() {
%1 = alloca i64
%2 = alloca i64*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
store volatile i64 1, i64* %1
store i64* null, i64** %2
%7 = call i64 @malloc(i32 8)
%8 = inttoptr i64 %7 to i64*
store i64* %8, i64** %2
%9 = load i64*, i64** %2
store i64 1, i64* %9
%10 = load i64*, i64** %2
%11 = ptrtoint i64* %10 to i64
%12 = call i64 @mem_addr_vir2phy(i64 %11)
store i64 %12, i64* %3
%13 = ptrtoint i64* %1 to i64
%14 = call i64 @mem_addr_vir2phy(i64 %13)
store i64 %14, i64* %4
%15 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %5
store i64 0, i64* %6
br label %16
16: ; preds = %0, %16
%17 = load i32*, i32** %5
%18 = call i32 (...) @getpid()
%19 = load i64, i64* %6
%20 = load i64, i64* %4
%21 = load volatile i64, i64* %1
%22 = load i64*, i64** %2
%23 = load i64, i64* %3
%24 = load i64*, i64** %2
%25 = load i64, i64* %24
%26 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str.2, i64 0, i64 0), i32 %18, i64 %19, i64* %1, i64 %20, i64 %21, i64** %2, i64* %22, i64 %23, i64 %25)
%27 = load i32*, i32** %5
%28 = call i32 @fflush(i32* %27)
%29 = call i32 @sleep(i32 1)
%30 = load i64, i64* %6
%31 = add nsw i64 %30, 1
store i64 %31, i64* %6
br label %16
}

LLVM IR (-O2):
@.str = [10 x i8] c"hello.txt\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [91 x i8] c"pid = %d, %ld, a's addr = %p [0x%lx : %ld], p point addr = %p q's addr = %p [0x%lx : %ld]\0A\00"
define i64 @test() {
%1 = alloca i64
%2 = alloca i64*
%3 = bitcast i64* %1 to i8*
store volatile i64 1, i64* %1
%4 = bitcast i64** %2 to i8*
%5 = tail call i64 @malloc(i32 8)
%6 = inttoptr i64 %5 to i64*
store i64* %6, i64** %2
store i64 1, i64* %6
%7 = tail call i64 @mem_addr_vir2phy(i64 %5)
%8 = ptrtoint i64* %1 to i64
%9 = call i64 @mem_addr_vir2phy(i64 %8)
%10 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %11, %0
%12 = phi i64 [ 0, %0 ], [ %20, %11 ]
%13 = call i32 (...) @getpid()
%14 = load volatile i64, i64* %1
%15 = load i64*, i64** %2
%16 = load i64, i64* %15
%17 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str.2, i64 0, i64 0), i32 %13, i64 %12, i64* %1, i64 %9, i64 %14, i64** %2, i64* %15, i64 %7, i64 %16)
%18 = call i32 @fflush(i32* %10)
%19 = call i32 @sleep(i32 1)
%20 = add nuw nsw i64 %12, 1
br label %11
}