#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 O_CREAT;
int O_WRONLY;
int S_IREAD;
int S_IRGRP;
int S_IWRITE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  int fd;
  FILE *stream;
  unlink("test.txt");
  fd = open("test.txt", O_CREAT | O_WRONLY | S_IREAD | S_IRGRP | S_IWRITE);
  stream = fdopen(fd, "w");
  if (stream == NULL) {
    printf("fdopen ³ö´í.\n");
  } else {
    fprintf(stream, "Hello world!\n");
    fclose(stream);
  }
  printf("The conten of the test.txt is:\n");
  system("cat test.txt");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xeb9(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
or     (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
lea    0xe80(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe28(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fd <main+0x8d>
lea    0xe0f(%rip),%rdi
call   1060 <printf@plt>
jmp    1216 <main+0xa6>
mov    -0x20(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe05(%rip),%rdi
call   1060 <printf@plt>
lea    0xe19(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe9c(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e71(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
lea    0xe44(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
je     11ee <main+0x7e>
mov    %rax,%rbx
lea    0xe30(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11fa <main+0x8a>
lea    0xe47(%rip),%rdi
call   1040 <puts@plt>
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
lea    0xe03(%rip),%rdi
call   1060 <system@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IREAD = global i32 0
@S_IRGRP = global i32 0
@S_IWRITE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [18 x i8] c"fdopen \C2\B3\C3\B6\C2\B4\C3\AD.\0A\00"
@.str.3 = [14 x i8] c"Hello world!\0A\00"
@.str.4 = [32 x i8] c"The conten of the test.txt is:\0A\00"
@.str.5 = [13 x i8] c"cat test.txt\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_WRONLY
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IREAD
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWRITE
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %7
%21 = load i32*, i32** %7
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %30
25: ; preds = %2
%26 = load i32*, i32** %7
%27 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%28 = load i32*, i32** %7
%29 = call i32 @fclose(i32* %28)
br label %30
30: ; preds = %25, %23
%31 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IREAD = global i32 0
@S_IRGRP = global i32 0
@S_IWRITE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [14 x i8] c"Hello world!\0A\00"
@.str.5 = [13 x i8] c"cat test.txt\00"
@str = [31 x i8] c"The conten of the test.txt is:\00"
@str.6 = [17 x i8] c"fdopen \C2\B3\C3\B6\C2\B4\C3\AD.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IREAD
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IWRITE
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = tail call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.6, i64 0, i64 0))
br label %21
18: ; preds = %2
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i64 13, i64 1, i32* %14)
%20 = tail call i32 @fclose(i32* %14)
br label %21
21: ; preds = %18, %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}