#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*/ printf(char *, int, long, unsigned long volatile *,
                          unsigned long, unsigned long volatile,
                          unsigned long **, unsigned long *, unsigned long,
                          unsigned long volatile);
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) {
    printf(
        "pid = %5d, times = %4ld, a's addr = %p [(physical : 0x%lx) : 0x%lx\n"
        "p point's addr = %p q's addr = %p [(physical : 0x%lx) : 0x%lx]\n",
        getpid(), i, &a, a_phy_addr, (volatile unsigned long)a, &p, p,
        p_phy_addr, (volatile unsigned long)*p);
    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   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movq   $0x1,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <mem_addr_vir2phy@plt>
mov    %rax,-0x28(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <mem_addr_vir2phy@plt>
mov    %rax,-0x30(%rbp)
lea    0xe2b(%rip),%rdi
lea    0xe2e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%r8
mov    -0x18(%rbp),%r9
mov    -0x20(%rbp),%r11
mov    -0x28(%rbp),%r10
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
lea    0xdf5(%rip),%rdi
lea    -0x18(%rbp),%rcx
lea    -0x20(%rbp),%rbx
mov    %rbx,(%rsp)
mov    %r11,0x8(%rsp)
mov    %r10,0x10(%rsp)
mov    %rax,0x18(%rsp)
call   1040 <printf@plt>
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    0xe21(%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   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    %rax,-0x40(%rbp)
jmp    11ed <test+0x6d>

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x1,(%rsp)
mov    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,0x8(%rsp)
movq   $0x1,(%rax)
mov    %rax,%rdi
call   1060 <mem_addr_vir2phy@plt>
mov    %rax,0x20(%rsp)
mov    %rsp,%rdi
call   1060 <mem_addr_vir2phy@plt>
mov    %rax,0x18(%rsp)
lea    0xe33(%rip),%rdi
lea    0xe36(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,0x10(%rsp)
xor    %r13d,%r13d
mov    0x18(%rsp),%rbx
mov    0x20(%rsp),%r12
mov    0x10(%rsp),%r14
xor    %eax,%eax
call   1030 <getpid@plt>
mov    (%rsp),%r9
mov    0x8(%rsp),%rbp
lea    0xe06(%rip),%rdi
mov    %eax,%esi
mov    %r13,%rdx
mov    %rsp,%rcx
mov    %rbx,%r8
push   0x0(%rbp)
push   %r12
push   %rbp
lea    0x20(%rsp),%r15
push   %r15
call   1040 <printf@plt>
add    $0x20,%rsp
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x8(%rsp),%rbp
sub    $0x8,%rsp
mov    %r14,%rdi
lea    0xe4e(%rip),%rsi
mov    %eax,%edx
mov    %r13,%rcx
lea    0x8(%rsp),%r8
mov    %rbx,%r9
push   0x0(%rbp)
push   %r12
push   %rbp
push   %r15
push   0x28(%rsp)
call   1050 <fprintf@plt>
add    $0x30,%rsp
mov    %r14,%rdi
call   1080 <fflush@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
add    $0x1,%r13
jmp    11f0 <test+0x70>

LLVM IR (-O0):
@.str = [10 x i8] c"hello.txt\00"
@.str.1 = [3 x i8] c"a+\00"
@.str.2 = [131 x i8] c"pid = %5d, times = %4ld, a's addr = %p [(physical : 0x%lx) : 0x%lx\0Ap point's addr = %p q's addr = %p [(physical : 0x%lx) : 0x%lx]\0A\00"
@.str.3 = [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 = call i32 (...) @getpid()
%18 = load i64, i64* %6
%19 = load i64, i64* %4
%20 = load volatile i64, i64* %1
%21 = load i64*, i64** %2
%22 = load i64, i64* %3
%23 = load i64*, i64** %2
%24 = load i64, i64* %23
%25 = call i32 @printf(i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 %17, i64 %18, i64* %1, i64 %19, i64 %20, i64** %2, i64* %21, i64 %22, i64 %24)
%26 = load i32*, i32** %5
%27 = call i32 (...) @getpid()
%28 = load i64, i64* %6
%29 = load i64, i64* %4
%30 = load volatile i64, i64* %1
%31 = load i64*, i64** %2
%32 = load i64, i64* %3
%33 = load i64*, i64** %2
%34 = load i64, i64* %33
%35 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str.3, i64 0, i64 0), i32 %27, i64 %28, i64* %1, i64 %29, i64 %30, i64** %2, i64* %31, i64 %32, i64 %34)
%36 = load i32*, i32** %5
%37 = call i32 @fflush(i32* %36)
%38 = call i32 @sleep(i32 1)
%39 = load i64, i64* %6
%40 = add nsw i64 %39, 1
store i64 %40, 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 = [131 x i8] c"pid = %5d, times = %4ld, a's addr = %p [(physical : 0x%lx) : 0x%lx\0Ap point's addr = %p q's addr = %p [(physical : 0x%lx) : 0x%lx]\0A\00"
@.str.3 = [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 ], [ %25, %11 ]
%13 = call i32 (...) @getpid()
%14 = load volatile i64, i64* %1
%15 = load i64*, i64** %2
%16 = load i64, i64* %15
%17 = call i32 @printf(i8* getelementptr inbounds ([131 x i8], [131 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 (...) @getpid()
%19 = load volatile i64, i64* %1
%20 = load i64*, i64** %2
%21 = load i64, i64* %20
%22 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([91 x i8], [91 x i8]* @.str.3, i64 0, i64 0), i32 %18, i64 %12, i64* %1, i64 %9, i64 %19, i64** %2, i64* %20, i64 %7, i64 %21)
%23 = call i32 @fflush(i32* %10)
%24 = call i32 @sleep(i32 1)
%25 = add nuw nsw i64 %12, 1
br label %11
}