#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_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ F_GETFL;
int O_ACCMODE;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int my_dup(int);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int fd, new_fd, flags, new_flags;
  flags = O_CREAT | O_RDWR;
  fd = open("foo", flags, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    fprintf(stderr, "error on open\n");
    exit(EXIT_FAILURE);
  }
  new_fd = my_dup(fd);
  if (new_fd == -1) {
    fprintf(stderr, "error on my_dup\n");
    exit(EXIT_FAILURE);
  }
  new_flags = fcntl(new_fd, F_GETFL);
  if (new_flags == -1) {
    fprintf(stderr, "error on fcntl\n");
    exit(EXIT_FAILURE);
  }
  fprintf(stdout, "new fd: %d, old fd: %d, new flags: %d, old flags: %d\n ",
          new_fd, fd, new_flags, flags);
  if ((new_flags & O_ACCMODE) == O_RDWR) {
    fprintf(stdout, "new fd also has same access flags\n");
  }
  if (close(fd) == -1) {
    fprintf(stderr, "error on close\n");
    exit(EXIT_FAILURE);
  }
  if (close(new_fd) == -1) {
    fprintf(stderr, "error on close\n");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


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)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2e52(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
lea    0xe59(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11de <main+0x7e>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <my_dup@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1218 <main+0xb8>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    125b <main+0xfb>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%eax
mov    0x2d5e(%rip),%rcx
and    (%rcx),%eax
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%eax
jne    12b2 <main+0x152>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    12e8 <main+0x188>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    131e <main+0x1be>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e86(%rip),%rax
mov    0x2e5f(%rip),%r12
mov    (%r12),%ebp
or     (%rax),%ebp
mov    0x2e22(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe68(%rip),%rdi
mov    %ebp,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
jne    11b9 <main+0x59>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
jmp    1279 <main+0x119>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <my_dup@plt>
cmp    $0xffffffff,%eax
jne    11dc <main+0x7c>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
jmp    1279 <main+0x119>
mov    %eax,%r14d
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1207 <main+0xa7>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
jmp    1279 <main+0x119>
mov    %eax,%r15d
mov    0x2d8f(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe18(%rip),%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2db3(%rip),%rax
and    (%rax),%r15d
cmp    (%r12),%r15d
jne    1250 <main+0xf0>
mov    0x0(%r13),%edi
lea    0xe22(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1269 <main+0x109>
mov    %r14d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    128e <main+0x12e>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [4 x i8] c"foo\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"error on open\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [17 x i8] c"error on my_dup\0A\00"
@F_GETFL = global i32 0
@.str.3 = [16 x i8] c"error on fcntl\0A\00"
@stdout = global i32 0
@.str.4 = [55 x i8] c"new fd: %d, old fd: %d, new flags: %d, old flags: %d\0A \00"
@O_ACCMODE = global i32 0
@.str.5 = [35 x i8] c"new fd also has same access flags\0A\00"
@.str.6 = [16 x i8] c"error on close\0A\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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %14, %15
%17 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %2
%26 = load i32, i32* %6
%27 = call i32 @my_dup(i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %25
%36 = load i32, i32* %7
%37 = load i32, i32* @F_GETFL
%38 = call i32 @fcntl(i32 %36, i32 %37)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %35
%47 = load i32, i32* @stdout
%48 = load i32, i32* %7
%49 = load i32, i32* %6
%50 = load i32, i32* %9
%51 = load i32, i32* %8
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 %48, i32 %49, i32 %50, i32 %51)
%53 = load i32, i32* %9
%54 = load i32, i32* @O_ACCMODE
%55 = and i32 %53, %54
%56 = load i32, i32* @O_RDWR
%57 = icmp eq i32 %55, %56
br i1 %57, label %58, label %61
58: ; preds = %46
%59 = load i32, i32* @stdout
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %58, %46
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = load i32, i32* @stderr
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %61
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %79
74: ; preds = %70
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
79: ; preds = %70
%80 = load i32, i32* @EXIT_SUCCESS
%81 = call i32 @exit(i32 %80)
unreachable
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [4 x i8] c"foo\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@stderr = global i32 0
@.str.1 = [15 x i8] c"error on open\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [17 x i8] c"error on my_dup\0A\00"
@F_GETFL = global i32 0
@.str.3 = [16 x i8] c"error on fcntl\0A\00"
@stdout = global i32 0
@.str.4 = [55 x i8] c"new fd: %d, old fd: %d, new flags: %d, old flags: %d\0A \00"
@O_ACCMODE = global i32 0
@.str.5 = [35 x i8] c"new fd also has same access flags\0A\00"
@.str.6 = [16 x i8] c"error on close\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = tail call i32 @my_dup(i32 %9)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
%25 = load i32, i32* @F_GETFL
%26 = tail call i32 @fcntl(i32 %17, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = load i32, i32* @stdout
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 %17, i32 %9, i32 %26, i32 %5)
%36 = load i32, i32* @O_ACCMODE
%37 = and i32 %36, %26
%38 = load i32, i32* @O_RDWR
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = load i32, i32* @stdout
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
br label %43
43: ; preds = %40, %33
%44 = tail call i32 @close(i32 %9)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = tail call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %43
%52 = tail call i32 @close(i32 %17)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = tail call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = tail call i32 @exit(i32 %57)
unreachable
59: ; preds = %51
%60 = load i32, i32* @EXIT_SUCCESS
%61 = tail call i32 @exit(i32 %60)
unreachable
}