#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_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int dup(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int oldfd;
  int newfd;
  oldfd = open("test.txt", O_RDWR | O_CREAT | O_APPEND);
  printf("oldfd = %d\n", oldfd);
  if (oldfd < 0) {
    return -1;
  }
  write(oldfd, "ABC", 3);
  newfd = dup(oldfd);
  printf("newfd = %d\n", newfd);
  if (newfd < 0) {
    close(oldfd);
    return -2;
  }
  write(newfd, "DEF", 3);
  close(newfd);
  close(oldfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe76(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11ae <main+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1225 <main+0xd5>
mov    -0x8(%rbp),%edi
lea    0xe5d(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jge    11fa <main+0xaa>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1225 <main+0xd5>
mov    -0xc(%rbp),%edi
lea    0xe21(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e86(%rip),%rax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5c(%rip),%rax
or     (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe86(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
js     11da <main+0x8a>
lea    0xe80(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <dup@plt>
mov    %eax,%ebp
lea    0xe68(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebp,%ebp
js     11e1 <main+0x91>
lea    0xe62(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
jmp    11e6 <main+0x96>
mov    $0xffffffff,%ebp
jmp    11ed <main+0x9d>
mov    $0xfffffffe,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [12 x i8] c"oldfd = %d\0A\00"
@.str.2 = [4 x i8] c"ABC\00"
@.str.3 = [12 x i8] c"newfd = %d\0A\00"
@.str.4 = [4 x i8] c"DEF\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = load i32, i32* @O_APPEND
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
store i32 -1, i32* %1
br label %34
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%18 = load i32, i32* %2
%19 = call i32 @dup(i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
store i32 -2, i32* %1
br label %34
27: ; preds = %15
%28 = load i32, i32* %3
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
store i32 0, i32* %1
br label %34
34: ; preds = %27, %24, %14
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@.str.1 = [12 x i8] c"oldfd = %d\0A\00"
@.str.2 = [4 x i8] c"ABC\00"
@.str.3 = [12 x i8] c"newfd = %d\0A\00"
@.str.4 = [4 x i8] c"DEF\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_APPEND
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %6, 0
br i1 %8, label %20, label %9
9: ; preds = %0
%10 = tail call i32 @write(i32 %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%11 = tail call i32 @dup(i32 %6)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %11, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call i32 @write(i32 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%16 = tail call i32 @close(i32 %11)
br label %17
17: ; preds = %9, %14
%18 = phi i32 [ 0, %14 ], [ -2, %9 ]
%19 = tail call i32 @close(i32 %6)
br label %20
20: ; preds = %17, %0
%21 = phi i32 [ -1, %0 ], [ %18, %17 ]
ret i32 %21
}