#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 /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int dup(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, ...);
int /*<<< orphan*/ write(int, char *, int);
int main(__attribute__((unused)) int _argc,
         __attribute__((unused)) char **argv) {
  int fd1, fd2, fd3;
  const char *filename = "example2.txt";
  fd1 = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  fd2 = dup(fd1);
  fd3 = open(filename, O_RDWR);
  write(fd1, "Hello,", 6);
  write(fd2, " world", 6);
  lseek(fd2, 0, SEEK_SET);
  write(fd1, "HELLO,", 6);
  write(fd3, "Gidday", 6);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe93(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e73(%rip),%rax
or     (%rax),%esi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2e30(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe35(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
lea    0xe28(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    0xe08(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%edi
lea    0xdfb(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e7a(%rip),%r15
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
or     (%r15),%esi
mov    0x2e7f(%rip),%rax
or     (%rax),%esi
mov    0x2e3e(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe74(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <dup@plt>
mov    %eax,%ebp
mov    (%r15),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%r14d
lea    0xe55(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1030 <write@plt>
lea    0xe49(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xe2b(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1030 <write@plt>
lea    0xe1f(%rip),%rsi
mov    %r14d,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"example2.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [7 x i8] c"Hello,\00"
@.str.2 = [7 x i8] c" world\00"
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"HELLO,\00"
@.str.4 = [7 x i8] c"Gidday\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %15, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = call i32 @dup(i32 %20)
store i32 %21, i32* %7
%22 = load i8*, i8** %9
%23 = load i32, i32* @O_RDWR
%24 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %6
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%27 = load i32, i32* %7
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%29 = load i32, i32* %7
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %29, i32 0, i32 %30)
%32 = load i32, i32* %6
%33 = call i32 @write(i32 %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%34 = load i32, i32* %8
%35 = call i32 @write(i32 %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"example2.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [7 x i8] c"Hello,\00"
@.str.2 = [7 x i8] c" world\00"
@SEEK_SET = global i32 0
@.str.3 = [7 x i8] c"HELLO,\00"
@.str.4 = [7 x i8] c"Gidday\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %10)
%12 = tail call i32 @dup(i32 %11)
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%16 = tail call i32 @write(i32 %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%17 = load i32, i32* @SEEK_SET
%18 = tail call i32 @lseek(i32 %12, i32 0, i32 %17)
%19 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%20 = tail call i32 @write(i32 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 6)
%21 = load i32, i32* @EXIT_SUCCESS
%22 = tail call i32 @exit(i32 %21)
unreachable
}