#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  fprintf(stderr, "this is from stderr\n");
  int fd = dup(STDERR_FILENO);
  if (fd < 0) {
    perror("dup");
    goto err;
  }
  char buf[100] = {0};
  snprintf(buf, sizeof(buf) - 1, "this is dup file descriptor: %d\n", fd);
  write(fd, buf, strlen(buf) + 1);
  close(fd);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d3 <main+0x53>
lea    0xe4c(%rip),%rdi
call   10a0 <perror@plt>
jmp    1239 <main+0xb9>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x63,%esi
lea    0xe23(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x7c(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1240 <main+0xc0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1060 <dup@plt>
test   %eax,%eax
js     1208 <main+0x98>
mov    %eax,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
lea    0xe4a(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x63,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe06(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1200 <main+0x90>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"this is from stderr\0A\00"
@STDERR_FILENO = global i32 0
@.str.1 = [4 x i8] c"dup\00"
@.str.2 = [33 x i8] c"this is dup file descriptor: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
store i32 0, i32* %1
%4 = load i32, i32* @stderr
%5 = call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @STDERR_FILENO
%7 = call i32 @dup(i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %25
12: ; preds = %0
%13 = bitcast [100 x i8]* %3 to i8*
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* %2
%16 = call i32 @snprintf(i8* %14, i32 99, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %2
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%20 = call i64 @strlen(i8* %19)
%21 = add nsw i64 %20, 1
%22 = call i32 @write(i32 %17, i8* %18, i64 %21)
%23 = load i32, i32* %2
%24 = call i32 @close(i32 %23)
store i32 0, i32* %1
br label %26
25: ; preds = %10
store i32 -1, i32* %1
br label %26
26: ; preds = %25, %12
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"this is from stderr\0A\00"
@STDERR_FILENO = global i32 0
@.str.1 = [4 x i8] c"dup\00"
@.str.2 = [33 x i8] c"this is dup file descriptor: %d\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = load i32, i32* @stderr
%3 = tail call i32 @fprintf(i32 %2, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @STDERR_FILENO
%5 = tail call i32 @dup(i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %16
9: ; preds = %0
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 99, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%12 = call i64 @strlen(i8* %10)
%13 = add nsw i64 %12, 1
%14 = call i32 @write(i32 %5, i8* %10, i64 %13)
%15 = call i32 @close(i32 %5)
br label %16
16: ; preds = %7, %9
%17 = phi i32 [ -1, %7 ], [ 0, %9 ]
ret i32 %17
}