#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ close(int /*<<< orphan*/ *);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ *stderr;
int main(int argc, char *argv[]) {
  FILE *fp1 = fopen("duptestfile", "w");
  if (fp1 == NULL) {
    fprintf(stderr, "Can't Open file!");
    exit(EXIT_FAILURE);
  }
  fprintf(fp1, "FP1\n");
  fflush(fp1);
  int fd2 = dup(fileno(fp1));
  FILE *fp2 = fdopen(fd2, "2");
  if (fp2 == NULL) {
    fprintf(stderr, "Dup failed!\n");
    exit(EXIT_FAILURE);
  }
  fprintf(fp2, "FP2-1\n");
  fflush(fp1);
  close(fp1);
  fprintf(fp2, "FP2-2\n");
  fflush(fp2);
  close(fp2);
  return 0;
}


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    0xe63(%rip),%rdi
lea    0xe68(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11dc <main+0x5c>
mov    0x2e21(%rip),%rax
mov    (%rax),%rdi
lea    0xe45(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe38(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
call   1030 <dup@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    0xe12(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    124a <main+0xca>
mov    0x2db3(%rip),%rax
mov    (%rax),%rdi
lea    0xdef(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%rdi
call   1040 <close@plt>
mov    -0x28(%rbp),%rdi
lea    0xdc3(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe75(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1245 <main+0xc5>
mov    %rax,%r14
lea    0xe75(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %r14,%rdi
call   1060 <fflush@plt>
mov    %r14,%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
call   1030 <dup@plt>
lea    0xe4a(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
je     125d <main+0xdd>
mov    %rax,%rbx
lea    0xe43(%rip),%rdi
mov    $0x6,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %r14,%rdi
call   1060 <fflush@plt>
mov    %r14,%rdi
call   1040 <close@plt>
lea    0xe21(%rip),%rdi
mov    $0x6,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
mov    %rbx,%rdi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d94(%rip),%rax
mov    (%rax),%rcx
lea    0xdb8(%rip),%rdi
mov    $0x10,%esi
jmp    1273 <main+0xf3>
mov    0x2d7c(%rip),%rax
mov    (%rax),%rcx
lea    0xdb8(%rip),%rdi
mov    $0xc,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"duptestfile\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [17 x i8] c"Can't Open file!\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"FP1\0A\00"
@.str.4 = [2 x i8] c"2\00"
@.str.5 = [13 x i8] c"Dup failed!\0A\00"
@.str.6 = [7 x i8] c"FP2-1\0A\00"
@.str.7 = [7 x i8] c"FP2-2\0A\00"
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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i32*, i32** @stderr
%14 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%20 = load i32*, i32** %6
%21 = call i32 @fflush(i32* %20)
%22 = load i32*, i32** %6
%23 = call i32 @fileno(i32* %22)
%24 = call i32 @dup(i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = call i32* @fdopen(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %26, i32** %8
%27 = load i32*, i32** %8
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %34
29: ; preds = %17
%30 = load i32*, i32** @stderr
%31 = call i32 @fprintf(i32* %30, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %17
%35 = load i32*, i32** %8
%36 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%37 = load i32*, i32** %6
%38 = call i32 @fflush(i32* %37)
%39 = load i32*, i32** %6
%40 = call i32 @close(i32* %39)
%41 = load i32*, i32** %8
%42 = call i32 @fprintf(i32* %41, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%43 = load i32*, i32** %8
%44 = call i32 @fflush(i32* %43)
%45 = load i32*, i32** %8
%46 = call i32 @close(i32* %45)
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"duptestfile\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32* null
@.str.2 = [17 x i8] c"Can't Open file!\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [5 x i8] c"FP1\0A\00"
@.str.4 = [2 x i8] c"2\00"
@.str.5 = [13 x i8] c"Dup failed!\0A\00"
@.str.6 = [7 x i8] c"FP2-1\0A\00"
@.str.7 = [7 x i8] c"FP2-2\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32*, i32** @stderr
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i64 16, i64 1, i32* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = tail call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 4, i64 1, i32* %3)
%12 = tail call i32 @fflush(i32* %3)
%13 = tail call i32 @fileno(i32* %3)
%14 = tail call i32 @dup(i32 %13)
%15 = tail call i32* @fdopen(i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %10
%18 = load i32*, i32** @stderr
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i64 12, i64 1, i32* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %10
%23 = tail call i64 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0), i64 6, i64 1, i32* %15)
%24 = tail call i32 @fflush(i32* %3)
%25 = tail call i32 @close(i32* %3)
%26 = tail call i64 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i64 6, i64 1, i32* %15)
%27 = tail call i32 @fflush(i32* %15)
%28 = tail call i32 @close(i32* %15)
ret i32 0
}