#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int, char *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int fd;
  int v;
  fd = open("toto.txt", O_CREAT | O_EXCL | O_RDWR);
  if (fd <= 0) {
    perror("overture de toto.txt");
    exit(1);
  }
  printf("> ");
  scanf("%d", &v);
  printf("un printf : v=%d\n", v);
  dprintf(fd, "direct : v=%d\n", v);
  dup2(fd, 1);
  printf("après dup : v=%d\n", v);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
lea    0xe62(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jg     11d2 <main+0x52>
lea    0xe3d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe31(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x8(%rbp),%esi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%edx
lea    0xe1f(%rip),%rsi
call   1060 <dprintf@plt>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e6c(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e42(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    1222 <main+0xa2>
mov    %eax,%ebx
lea    0xe5c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe51(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1070 <scanf@plt>
mov    0xc(%rsp),%esi
lea    0xe3f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edx
lea    0xe3f(%rip),%rsi
mov    %ebx,%edi
call   1060 <dprintf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%esi
lea    0xe30(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdd7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"toto.txt\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@.str.1 = [21 x i8] c"overture de toto.txt\00"
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"un printf : v=%d\0A\00"
@.str.5 = [15 x i8] c"direct : v=%d\0A\00"
@.str.6 = [19 x i8] c"apr\C3\A8s dup : v=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_EXCL
%5 = or i32 %3, %4
%6 = load i32, i32* @O_RDWR
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp sle i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %2)
%17 = load i32, i32* %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %1
%20 = load i32, i32* %2
%21 = call i32 @dprintf(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %1
%23 = call i32 @dup2(i32 %22, i32 1)
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %1
%27 = call i32 @close(i32 %26)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"toto.txt\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@.str.1 = [21 x i8] c"overture de toto.txt\00"
@.str.2 = [3 x i8] c"> \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"un printf : v=%d\0A\00"
@.str.5 = [15 x i8] c"direct : v=%d\0A\00"
@.str.6 = [19 x i8] c"apr\C3\A8s dup : v=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_EXCL
%5 = or i32 %4, %3
%6 = load i32, i32* @O_RDWR
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %1)
%16 = load i32, i32* %1
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %1
%19 = call i32 @dprintf(i32 %8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %18)
%20 = call i32 @dup2(i32 %8, i32 1)
%21 = load i32, i32* %1
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%23 = call i32 @close(i32 %8)
ret i32 0
}