#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(void) {
  int fd;
  fd = open("student.txt", O_RDWR | O_CREAT, 0666);
  if (fd < 0) {
    perror("open");
  }
  char *content = "hello linux i am here";
  write(fd, content, strlen(content) + 1);
  lseek(fd, 0, SEEK_SET);
  char buf[256];
  read(fd, buf, 256);
  printf("文件内容是:%s\n", buf);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ce <main+0x4e>
lea    0xe43(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe3c(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x118(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x11c(%rbp),%edi
mov    -0x118(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
lea    -0x110(%rbp),%rsi
lea    0xde3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    0x2e5f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11fd <main+0x8d>
lea    0xe67(%rip),%rsi
mov    $0x16,%edx
mov    %ebx,%edi
call   1030 <write@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x100,%edx
call   1070 <read@plt>
lea    0xe46(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe08(%rip),%rdi
call   1090 <perror@plt>
jmp    11a3 <main+0x33>

LLVM IR (-O0):
@.str = [12 x i8] c"student.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [22 x i8] c"hello linux i am here\00"
@SEEK_SET = global i32 0
@.str.3 = [20 x i8] c"\E6\96\87\E4\BB\B6\E5\86\85\E5\AE\B9\E6\98\AF:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [256 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8** %3
%14 = load i32, i32* %2
%15 = load i8*, i8** %3
%16 = load i8*, i8** %3
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = call i32 @write(i32 %14, i8* %15, i64 %18)
%20 = load i32, i32* %2
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @lseek(i32 %20, i32 0, i32 %21)
%23 = load i32, i32* %2
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 256)
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%27 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"student.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [22 x i8] c"hello linux i am here\00"
@SEEK_SET = global i32 0
@.str.3 = [20 x i8] c"\E6\96\87\E4\BB\B6\E5\86\85\E5\AE\B9\E6\98\AF:%s\0A\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4, i32 438)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = tail call i32 @write(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 22)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i32 @lseek(i32 %5, i32 0, i32 %11)
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%14 = call i32 @read(i32 %5, i8* %13, i32 256)
%15 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%16 = call i32 @close(i32 %5)
ret i32 0
}