#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_APPEND;
int O_RDWR;
int open(char *, int);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ write(int, void const *, int /*<<< orphan*/);
int main() {
  char buf[1024];
  int fd1, fd2;
  const char *s1 = "hello world!\r\n";
  const char *s2 = "world hello!\r\n";
  fd1 = open("./a.txt", O_RDWR | O_APPEND);
  fd2 = open("./a.txt", O_RDWR | O_APPEND);
  write(fd1, (const void *)s1, strlen(s1));
  write(fd2, (const void *)s2, strlen(s2));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeb7(%rip),%rax
mov    %rax,-0x420(%rbp)
lea    0xeb8(%rip),%rax
mov    %rax,-0x428(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
lea    0xea7(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x414(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
lea    0xe83(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x418(%rbp)
mov    -0x414(%rbp),%eax
mov    %eax,-0x444(%rbp)
mov    -0x420(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x418(%rbp),%eax
mov    %eax,-0x434(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2eaa(%rip),%r14
mov    0x2e8b(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
lea    0xecc(%rip),%rbx
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%ebp
mov    (%r15),%esi
or     (%r14),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
mov    %eax,%r14d
lea    0xe8c(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe80(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"hello world!\0D\0A\00"
@.str.1 = [15 x i8] c"world hello!\0D\0A\00"
@.str.2 = [8 x i8] c"./a.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8** %6
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_APPEND
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_APPEND
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %3
%16 = load i8*, i8** %5
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @write(i32 %15, i8* %16, i32 %18)
%20 = load i32, i32* %4
%21 = load i8*, i8** %6
%22 = load i8*, i8** %6
%23 = call i32 @strlen(i8* %22)
%24 = call i32 @write(i32 %20, i8* %21, i32 %23)
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"hello world!\0D\0A\00"
@.str.1 = [15 x i8] c"world hello!\0D\0A\00"
@.str.2 = [8 x i8] c"./a.txt\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_APPEND
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_APPEND
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %11)
ret i32 0
}