#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_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int open(char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  FILE *fp;
  int fd;
  char buf[] = "hello, world\n";
  fd = open("data.dat", O_WRONLY | O_CREAT | O_TRUNC);
  write(fd, buf, strlen(buf));
  fp = fdopen(fd, "w");
  fputs("tcp/ip\n", fp);
  write(fd, buf, strlen(buf));
  fputs("hahaha", fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe83(%rip),%rax
mov    %rax,-0x2a(%rbp)
mov    0xe81(%rip),%eax
mov    %eax,-0x22(%rbp)
mov    0xe7b(%rip),%ax
mov    %ax,-0x1e(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e53(%rip),%rax
or     (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
lea    0xe57(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x2a(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x2a(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
lea    0xe29(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe17(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x2a(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x2a(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%rsi
lea    0xdea(%rip),%rdi
call   1060 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0xa646c726f7720,%rax
mov    %rax,0x6(%rsp)
mov    0x2e4a(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movabs $0x77202c6f6c6c6568,%rax
2c 20 77
mov    %rax,(%rsp)
mov    0x2e52(%rip),%rax
or     (%rax),%esi
lea    0xe67(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe4b(%rip),%rsi
mov    %ebp,%edi
call   1060 <fdopen@plt>
mov    %rax,%rbx
lea    0xe3c(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe17(%rip),%rdi
mov    $0x6,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [14 x i8] c"hello, world\0A\00"
@.str = [9 x i8] c"data.dat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"tcp/ip\0A\00"
@.str.3 = [7 x i8] c"hahaha\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [14 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [14 x i8]* %8 to i8*
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = getelementptr inbounds [14 x i8], [14 x i8]* %8, i64 0, i64 0
%18 = getelementptr inbounds [14 x i8], [14 x i8]* %8, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
%21 = load i32, i32* %7
%22 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %6
%24 = call i32 @fputs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32* %23)
%25 = load i32, i32* %7
%26 = getelementptr inbounds [14 x i8], [14 x i8]* %8, i64 0, i64 0
%27 = getelementptr inbounds [14 x i8], [14 x i8]* %8, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @write(i32 %25, i8* %26, i32 %28)
%30 = load i32*, i32** %6
%31 = call i32 @fputs(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* %30)
%32 = load i32*, i32** %6
%33 = call i32 @fclose(i32* %32)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [14 x i8] c"hello, world\0A\00"
@.str = [9 x i8] c"data.dat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [8 x i8] c"tcp/ip\0A\00"
@.str.3 = [7 x i8] c"hahaha\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [14 x i8]
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 @strlen(i8* %4)
%12 = call i32 @write(i32 %10, i8* %4, i32 %11)
%13 = call i32* @fdopen(i32 %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = call i64 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i64 7, i64 1, i32* %13)
%15 = call i32 @strlen(i8* %4)
%16 = call i32 @write(i32 %10, i8* %4, i32 %15)
%17 = call i64 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64 6, i64 1, i32* %13)
%18 = call i32 @fclose(i32* %13)
ret i32 0
}