#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 O_CREAT;
int O_EXCL;
int O_WRONLY;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main() {
  int fd = open("1.txt", O_WRONLY | O_CREAT | O_EXCL, 0666);
  if (fd < 0) {
    perror("fd");
    goto err;
  }
  fprintf(stderr, "1.This is from stderr\n");
  dup2(fd, STDERR_FILENO);
  fprintf(stderr, "2.This is from stderr\n");
  close(fd);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e89(%rip),%rax
or     (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
lea    0xe7f(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a9 <main+0x59>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
jmp    11f8 <main+0xa8>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ff <main+0xaf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e5d(%rip),%rax
mov    0x2e96(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e63(%rip),%rax
or     (%rax),%esi
lea    0xe8a(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     11cc <main+0x7c>
mov    %eax,%ebx
mov    0x2e4b(%rip),%r14
mov    (%r14),%edi
lea    0xe72(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    (%r14),%edi
lea    0xe6a(%rip),%rsi
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11c4 <main+0x74>

LLVM IR (-O0):
@.str = [6 x i8] c"1.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [3 x i8] c"fd\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"1.This is from stderr\0A\00"
@STDERR_FILENO = global i32 0
@.str.3 = [23 x i8] c"2.This is from stderr\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @O_EXCL
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %23
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* %2
%17 = load i32, i32* @STDERR_FILENO
%18 = call i32 @dup2(i32 %16, i32 %17)
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
store i32 0, i32* %1
br label %24
23: ; preds = %11
store i32 -1, i32* %1
br label %24
24: ; preds = %23, %13
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [6 x i8] c"1.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.1 = [3 x i8] c"fd\00"
@stderr = global i32 0
@.str.2 = [23 x i8] c"1.This is from stderr\0A\00"
@STDERR_FILENO = global i32 0
@.str.3 = [23 x i8] c"2.This is from stderr\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_EXCL
%5 = or i32 %3, %4
%6 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %5, i32 438)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %18
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @STDERR_FILENO
%14 = tail call i32 @dup2(i32 %6, i32 %13)
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @close(i32 %6)
br label %18
18: ; preds = %8, %10
%19 = phi i32 [ -1, %8 ], [ 0, %10 ]
ret i32 %19
}