#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*/ F_DUPFD;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char *path = argv[1];
  printf("This message should appear on the screen%s\n", path);
  int out = open(path, O_CREAT | O_RDWR, 0666);
  close(1);
  fcntl(out, F_DUPFD, 1);
  close(2);
  fcntl(out, F_DUPFD, 2);
  printf("This message should not appear on the screen\n");
  fprintf(stderr, "This error should not appear on the screen\n");
  close(out);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   1030 <fcntl@plt>
mov    $0x2,%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    $0x2,%edx
call   1030 <fcntl@plt>
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe5c(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%rbx
lea    0xe91(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e78(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,%ebx
mov    $0x1,%edi
call   1060 <close@plt>
mov    0x2e2d(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <fcntl@plt>
mov    $0x2,%edi
call   1060 <close@plt>
mov    (%r14),%esi
mov    %ebx,%edi
mov    $0x2,%edx
call   1040 <fcntl@plt>
lea    0xe7e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [44 x i8] c"This message should appear on the screen%s\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@F_DUPFD = global i32 0
@.str.1 = [46 x i8] c"This message should not appear on the screen\0A\00"
@stderr = global i32 0
@.str.2 = [44 x i8] c"This error should not appear on the screen\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i8**, i8*** %4
%8 = getelementptr inbounds i8*, i8** %7, i64 1
%9 = load i8*, i8** %8
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_RDWR
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %12, i32 %15, i32 438)
store i32 %16, i32* %6
%17 = call i32 @close(i32 1)
%18 = load i32, i32* %6
%19 = load i32, i32* @F_DUPFD
%20 = call i32 @fcntl(i32 %18, i32 %19, i32 1)
%21 = call i32 @close(i32 2)
%22 = load i32, i32* %6
%23 = load i32, i32* @F_DUPFD
%24 = call i32 @fcntl(i32 %22, i32 %23, i32 2)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
ret i32 0
}

LLVM IR (-O2):
@.str = [44 x i8] c"This message should appear on the screen%s\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@F_DUPFD = global i32 0
@stderr = global i32 0
@.str.2 = [44 x i8] c"This error should not appear on the screen\0A\00"
@str = [45 x i8] c"This message should not appear on the screen\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* %4, i32 %8, i32 438)
%10 = tail call i32 @close(i32 1)
%11 = load i32, i32* @F_DUPFD
%12 = tail call i32 @fcntl(i32 %9, i32 %11, i32 1)
%13 = tail call i32 @close(i32 2)
%14 = load i32, i32* @F_DUPFD
%15 = tail call i32 @fcntl(i32 %9, i32 %14, i32 2)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @close(i32 %9)
ret i32 0
}