#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 atoi(char *);
int close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int my_dup2(int, int);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  int fd, new_fd, flags, new_flags;
  if ((argc != 2) || (strcmp(argv[1], "--help") == 0)) {
    fprintf(stdout, "Usage: %s file_descriptor\n", argv[0]);
  } else {
    new_fd = atoi(argv[1]);
  }
  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_dup2(fd, new_fd);
  if (new_fd == -1) {
    fprintf(stderr, "error on my_dup2\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)
cmpl   $0x2,-0x8(%rbp)
jne    11be <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe51(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11e1 <main+0x61>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    11f1 <main+0x71>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    0x2dd7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
mov    0x2db7(%rip),%rax
or     (%rax),%edx
lea    0xe00(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1259 <main+0xd9>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <my_dup2@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1296 <main+0x116>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xdb4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12d9 <main+0x159>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
lea    0xd60(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%eax
mov    0x2ce0(%rip),%rcx
and    (%rcx),%eax
mov    0x2cc7(%rip),%rcx
cmp    (%rcx),%eax
jne    1330 <main+0x1b0>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    1366 <main+0x1e6>
mov    0x2c78(%rip),%rax
mov    (%rax),%edi
lea    0xd60(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c51(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    139c <main+0x21c>
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2c1b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2bf5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11a8 <main+0x28>
mov    0x8(%rbx),%rdi
lea    0xe62(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11c4 <main+0x44>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe4c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    11d0 <main+0x50>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    0x2e21(%rip),%rax
mov    0x2dfa(%rip),%r12
mov    (%r12),%ebp
or     (%rax),%ebp
mov    0x2dbd(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe25(%rip),%rdi
mov    %ebp,%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
jne    121e <main+0x9e>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
jmp    12e1 <main+0x161>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14d,%esi
call   1040 <my_dup2@plt>
cmp    $0xffffffff,%eax
jne    1244 <main+0xc4>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdf6(%rip),%rsi
jmp    12e1 <main+0x161>
mov    %eax,%r14d
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    126f <main+0xef>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
jmp    12e1 <main+0x161>
mov    %eax,%r15d
mov    0x2d27(%rip),%r13
mov    0x0(%r13),%edi
lea    0xdd3(%rip),%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d4b(%rip),%rax
and    (%rax),%r15d
cmp    (%r12),%r15d
jne    12b8 <main+0x138>
mov    0x0(%r13),%edi
lea    0xddd(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     12d1 <main+0x151>
mov    %r14d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12f6 <main+0x176>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

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

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