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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  FILE *fp0 = NULL;
  char Buf[4096];
  printf("hello mytest !");
  strcpy(Buf, "Mem is char dev!");
  printf("BUF: %s\n", Buf);
  fp0 = fopen("/dev/demo", "r+");
  if (fp0 == NULL) {
    printf("Open Memdev0 Error!\n");
    return -1;
  }
  fwrite(Buf, sizeof(Buf), 1, fp0);
  fseek(fp0, 0, SEEK_SET);
  strcpy(Buf, "Buf is NULL!");
  printf("BUF: %s\n", Buf);
  fread(Buf, sizeof(Buf), 1, fp0);
  printf("BUF: %s\n", Buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x1010(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1010(%rbp),%rsi
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe72(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ec <main+0x8c>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <main+0x118>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1000,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
lea    -0x1010(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1010(%rbp),%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1000,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
lea    -0x1010(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
lea    0xe7c(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe7a(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe79(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe71(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     123e <main+0xce>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
lea    0xe38(%rip),%rsi
mov    %rbp,%rdi
call   1030 <strcpy@plt>
lea    0xe13(%rip),%r15
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0xe0>
lea    0xdfe(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"hello mytest !\00"
@.str.1 = [17 x i8] c"Mem is char dev!\00"
@.str.2 = [9 x i8] c"BUF: %s\0A\00"
@.str.3 = [10 x i8] c"/dev/demo\00"
@.str.4 = [3 x i8] c"r+\00"
@.str.5 = [21 x i8] c"Open Memdev0 Error!\0A\00"
@SEEK_SET = global i32 0
@.str.6 = [13 x i8] c"Buf is NULL!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [4096 x i8]
store i32 0, i32* %1
store i32* null, i32** %2
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%9 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %30
14: ; preds = %0
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %2
%17 = call i32 @fwrite(i8* %15, i32 4096, i32 1, i32* %16)
%18 = load i32*, i32** %2
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%26 = load i32*, i32** %2
%27 = call i32 @fread(i8* %25, i32 4096, i32 1, i32* %26)
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 0, i32* %1
br label %30
30: ; preds = %14, %12
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [15 x i8] c"hello mytest !\00"
@.str.1 = [17 x i8] c"Mem is char dev!\00"
@.str.2 = [9 x i8] c"BUF: %s\0A\00"
@.str.3 = [10 x i8] c"/dev/demo\00"
@.str.4 = [3 x i8] c"r+\00"
@SEEK_SET = global i32 0
@.str.6 = [13 x i8] c"Buf is NULL!\00"
@str = [20 x i8] c"Open Memdev0 Error!\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %18
10: ; preds = %0
%11 = call i32 @fwrite(i8* %2, i32 4096, i32 1, i32* %6)
%12 = load i32, i32* @SEEK_SET
%13 = call i32 @fseek(i32* %6, i32 0, i32 %12)
%14 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%16 = call i32 @fread(i8* %2, i32 4096, i32 1, i32* %6)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %18
18: ; preds = %10, %8
%19 = phi i32 [ -1, %8 ], [ 0, %10 ]
ret i32 %19
}