#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 EXIT_SUCCESS;
int /*<<< orphan*/ O_CLOEXEC;
int O_CREAT;
int O_WRONLY;
int S_IROTH;
int S_IRWXG;
int S_IRWXU;
int S_IXGRP;
int S_IXOTH;
int /*<<< orphan*/ close(int);
scalar_t__ dup3(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fwrite(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd;
  char *str;
  char filename[] = "fdup.txt";
  int flags = O_WRONLY | O_CREAT;
  int mode = S_IRWXU | S_IXGRP | S_IRWXG | S_IROTH | S_IXOTH;
  fd = open(filename, flags, mode);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  if (dup3(fd, 1, O_CLOEXEC) < 0) {
    perror("dup2");
    exit(EXIT_FAILURE);
  }
  printf("The standard output goes into \'%s\'.\n", filename);
  close(fd);
  str = "Where does it go? ... \n";
  if (fwrite(str, sizeof(char), strlen(str), stdout) < 0) {
    perror("fwrite");
    exit(EXIT_FAILURE);
  }
  fflush(stdout);
  str = "Ok, Now I'm writing on stdout ... are you sure?\n";
  if (write(1, str, strlen(str)) < 0) {
    perror("write");
    exit(EXIT_FAILURE);
  }
  fprintf(stderr, "Please:\n$ cat %s\n", filename);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe3a(%rip),%rax
mov    %rax,-0x19(%rbp)
mov    0xe38(%rip),%al
mov    %al,-0x11(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    0x2e15(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    0x2dd0(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcf(%rip),%rcx
or     (%rcx),%eax
mov    0x2da6(%rip),%rcx
or     (%rcx),%eax
mov    0x2dc5(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
lea    -0x19(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    124e <main+0x9e>
lea    0xdce(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d28(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1040 <dup3@plt>
cmp    $0x0,%rax
jge    1288 <main+0xd8>
lea    0xd99(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x19(%rbp),%rsi
lea    0xd80(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    0xd91(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    0x2cd7(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
cmp    $0x0,%rax
jge    12f9 <main+0x149>
lea    0xd6a(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    0xd49(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1357 <main+0x1a7>
lea    0xd0d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c58(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
lea    -0x19(%rbp),%rdx
lea    0xd1d(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2c21(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2dca(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e10(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dee(%rip),%rax
or     (%rax),%edx
movabs $0x7478742e70756466,%rax
74 78 74
mov    %rax,0x8(%rsp)
mov    0x2db6(%rip),%rax
or     (%rax),%edx
movb   $0x0,0x10(%rsp)
mov    0x2dd0(%rip),%rax
or     (%rax),%edx
lea    0x8(%rsp),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12d8 <main+0x128>
mov    %eax,%ebx
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup3@plt>
test   %rax,%rax
js     12e1 <main+0x131>
lea    0xdcf(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xddc(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0x2d35(%rip),%r14
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   10d0 <fwrite@plt>
test   %rax,%rax
js     12ea <main+0x13a>
mov    (%r14),%edi
call   1090 <fflush@plt>
lea    0xdc6(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     12f3 <main+0x143>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
lea    0x8(%rsp),%rdx
call   1080 <fprintf@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd2a(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd26(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd5f(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd57(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"fdup.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IXGRP = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [5 x i8] c"dup2\00"
@.str.2 = [37 x i8] c"The standard output goes into '%s'.\0A\00"
@.str.3 = [24 x i8] c"Where does it go? ... \0A\00"
@stdout = global i32 0
@.str.4 = [7 x i8] c"fwrite\00"
@.str.5 = [49 x i8] c"Ok, Now I'm writing on stdout ... are you sure?\0A\00"
@.str.6 = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.7 = [18 x i8] c"Please:\0A$ cat %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [9 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = bitcast [9 x i8]* %4 to i8*
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
store i32 %10, i32* %5
%11 = load i32, i32* @S_IRWXU
%12 = load i32, i32* @S_IXGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRWXG
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IROTH
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IXOTH
%19 = or i32 %17, %18
store i32 %19, i32* %6
%20 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = call i32 @open(i8* %20, i32 %21, i32 %22)
store i32 %23, i32* %2
%24 = load i32, i32* %2
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %0
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %0
%31 = load i32, i32* %2
%32 = load i32, i32* @O_CLOEXEC
%33 = call i64 @dup3(i32 %31, i32 1, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %30
%40 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* %2
%43 = call i32 @close(i32 %42)
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8** %3
%44 = load i8*, i8** %3
%45 = load i8*, i8** %3
%46 = call i32 @strlen(i8* %45)
%47 = load i32, i32* @stdout
%48 = call i64 @fwrite(i8* %44, i32 1, i32 %46, i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %39
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %39
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
store i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i8** %3
%57 = load i8*, i8** %3
%58 = load i8*, i8** %3
%59 = call i32 @strlen(i8* %58)
%60 = call i64 @write(i32 1, i8* %57, i32 %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %54
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %54
%67 = load i32, i32* @stderr
%68 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%69 = call i32 @fprintf(i32 %67, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %68)
%70 = load i32, i32* @EXIT_SUCCESS
ret i32 %70
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"fdup.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IXGRP = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@O_CLOEXEC = global i32 0
@.str.1 = [5 x i8] c"dup2\00"
@.str.2 = [37 x i8] c"The standard output goes into '%s'.\0A\00"
@.str.3 = [24 x i8] c"Where does it go? ... \0A\00"
@stdout = global i32 0
@.str.4 = [7 x i8] c"fwrite\00"
@.str.5 = [49 x i8] c"Ok, Now I'm writing on stdout ... are you sure?\0A\00"
@.str.6 = [6 x i8] c"write\00"
@stderr = global i32 0
@.str.7 = [18 x i8] c"Please:\0A$ cat %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [9 x i8]
%2 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRWXU
%7 = load i32, i32* @S_IXGRP
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRWXG
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IXOTH
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %2, i32 %5, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %0
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %0
%22 = load i32, i32* @O_CLOEXEC
%23 = tail call i64 @dup3(i32 %15, i32 1, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%31 = call i32 @close(i32 %15)
%32 = call i32 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @stdout
%34 = call i64 @fwrite(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %29
%41 = load i32, i32* @stdout
%42 = call i32 @fflush(i32 %41)
%43 = call i32 @strlen(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
%44 = call i64 @write(i32 1, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0), i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %40
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%53 = load i32, i32* @EXIT_SUCCESS
ret i32 %53
}