#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 EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int main() {
  int oldfd, newfd;
  oldfd = open("./test.c", O_RDWR);
  if (oldfd < 0) {
    fprintf(stderr, "open failed");
    exit(EXIT_FAILURE);
  }
  newfd = 101;
  if (dup2(oldfd, newfd) < 0) {
    fprintf(stderr, "dup2 failed");
    perror("dup2");
    exit(EXIT_FAILURE);
  }
  close(oldfd);
  printf("%d,%d\n", newfd, oldfd);
  char buf[5] = {0};
  if (read(newfd, buf, 4) < 0) {
    fprintf(stderr, "read failed");
    exit(1);
  }
  printf("%s\n", buf);
  close(oldfd);
  close(newfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e4 <main+0x54>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe38(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x65,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    122f <main+0x9f>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe05(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xe05(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x11(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jge    1295 <main+0x105>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x11(%rbp),%rsi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1226 <main+0x96>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x65,%esi
call   1040 <dup2@plt>
test   %rax,%rax
js     123d <main+0xad>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe57(%rip),%rdi
mov    $0x65,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
movb   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x65,%edi
mov    $0x4,%edx
call   1070 <read@plt>
test   %rax,%rax
js     126c <main+0xdc>
lea    0x8(%rsp),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x65,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xdd3(%rip),%rsi
call   1080 <fprintf@plt>
jmp    125e <main+0xce>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xdc8(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"./test.c\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [12 x i8] c"dup2 failed\00"
@.str.3 = [5 x i8] c"dup2\00"
@.str.4 = [7 x i8] c"%d,%d\0A\00"
@.str.5 = [12 x i8] c"read failed\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [5 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
store i32 101, i32* %3
%15 = load i32, i32* %2
%16 = load i32, i32* %3
%17 = call i64 @dup2(i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %14
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %3
%29 = load i32, i32* %2
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 %29)
%31 = bitcast [5 x i8]* %4 to i8*
%32 = load i32, i32* %3
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%34 = call i64 @read(i32 %32, i8* %33, i32 4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %25
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %25
%41 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"./test.c\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [12 x i8] c"dup2 failed\00"
@.str.3 = [5 x i8] c"dup2\00"
@.str.4 = [7 x i8] c"%d,%d\0A\00"
@.str.5 = [12 x i8] c"read failed\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = tail call i64 @dup2(i32 %3, i32 101)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %10
%20 = tail call i32 @close(i32 %3)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 101, i32 %3)
%22 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%23 = call i64 @read(i32 101, i8* %22, i32 4)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = call i32 @puts(i8* %22)
%31 = tail call i32 @close(i32 %3)
%32 = tail call i32 @close(i32 101)
ret i32 0
}