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

/* 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];
  strcpy(buf, "mem is char dev");
  printf("buf: %s\n", buf);
  fp0 = fopen("/dev/memdev", "r+");
  if (fp0 == NULL) {
    printf("Open memdev0 error\n");
    return -1;
  }
  fwrite(buf, sizeof(buf), 1, fp0);
  fseek(fp0, 0, SEEK_SET);
  strcpy(buf, "buff 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    -0x1010(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1010(%rbp),%rsi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe70(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11de <main+0x7e>
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <main+0x10a>
lea    -0x1010(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1000,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
lea    -0x1010(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x1010(%rbp),%rsi
lea    0xde3(%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    0xdb4(%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),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe7a(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe6f(%rip),%rdi
lea    0xe74(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1230 <main+0xc0>
mov    %rax,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    0x2e03(%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    0xe11(%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    1242 <main+0xd2>
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 = [16 x i8] c"mem is char dev\00"
@.str.1 = [9 x i8] c"buf: %s\0A\00"
@.str.2 = [12 x i8] c"/dev/memdev\00"
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [20 x i8] c"Open memdev0 error\0A\00"
@SEEK_SET = global i32 0
@.str.5 = [13 x i8] c"buff 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 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %1
br label %29
13: ; preds = %0
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%15 = load i32*, i32** %2
%16 = call i32 @fwrite(i8* %14, i32 4096, i32 1, i32* %15)
%17 = load i32*, i32** %2
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %17, i32 0, i32 %18)
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%25 = load i32*, i32** %2
%26 = call i32 @fread(i8* %24, i32 4096, i32 1, i32* %25)
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 0, i32* %1
br label %29
29: ; preds = %13, %11
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str = [16 x i8] c"mem is char dev\00"
@.str.1 = [9 x i8] c"buf: %s\0A\00"
@.str.2 = [12 x i8] c"/dev/memdev\00"
@.str.3 = [3 x i8] c"r+\00"
@SEEK_SET = global i32 0
@.str.5 = [13 x i8] c"buff is NULL\00"
@str = [19 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 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%5 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %17
9: ; preds = %0
%10 = call i32 @fwrite(i8* %2, i32 4096, i32 1, i32* %5)
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @fseek(i32* %5, i32 0, i32 %11)
%13 = call i32 @strcpy(i8* %2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%15 = call i32 @fread(i8* %2, i32 4096, i32 1, i32* %5)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %17
17: ; preds = %9, %7
%18 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %18
}