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

/* Variables and functions */
int dup(int);
int dup2(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main(void) {
  FILE *alt;
  char buf[1024];
  int rv;
  printf("Hello world (1)\n");
  fflush(NULL);
  if (-1 == (rv = dup(1))) {
    fprintf(stderr, "dup(1) returned unexpected result: %d\n", rv);
  }
  alt = fdopen(rv, "w");
  fprintf(alt, "Hello world (dup)\n");
  fflush(NULL);
  fclose(alt);
  if (3 != (rv = dup2(0, 3))) {
    fprintf(stderr, "dup2(0, 3) returned unexpected result: %d\n", rv);
  }
  alt = fdopen(3, "r");
  printf("%s", fgets(buf, sizeof buf, alt));
  fclose(alt);
  if (3 != (rv = dup2(1, 3))) {
    fprintf(stderr, "dup2(1, 3) returned unexpected result: %d\n", rv);
  }
  alt = fdopen(3, "w");
  fprintf(alt, "Good bye cruel world! dup2(1, 3)\n");
  fclose(alt);
  if (20 != (rv = dup2(1, 20))) {
    fprintf(stderr, "dup2(1, 3) returned unexpected result: %d\n", rv);
  }
  alt = fdopen(20, "w");
  fprintf(alt, "Good bye cruel world! dup2(1, 20)\n");
  fclose(alt);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <fflush@plt>
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,%ecx
mov    %ecx,-0x414(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11e6 <main+0x66>
mov    0x2e11(%rip),%rax
movslq (%rax),%rdi
mov    -0x414(%rbp),%edx
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x414(%rbp),%edi
lea    0xe45(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <fflush@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
mov    $0x3,%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    %ecx,-0x414(%rbp)
mov    $0x3,%eax
cmp    %ecx,%eax
je     125f <main+0xdf>
mov    0x2d98(%rip),%rax
movslq (%rax),%rdi
mov    -0x414(%rbp),%edx
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x3,%edi
lea    0xe0d(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    %rax,%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
mov    $0x3,%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    %ecx,-0x414(%rbp)
mov    $0x3,%eax
cmp    %ecx,%eax
je     12e5 <main+0x165>
mov    0x2d12(%rip),%rax
movslq (%rax),%rdi
mov    -0x414(%rbp),%edx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x3,%edi
lea    0xd47(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
mov    $0x14,%esi
call   1040 <dup2@plt>
mov    %eax,%ecx
mov    %ecx,-0x414(%rbp)
mov    $0x14,%eax
cmp    %ecx,%eax
je     1357 <main+0x1d7>
mov    0x2ca0(%rip),%rax
movslq (%rax),%rdi
mov    -0x414(%rbp),%edx
lea    0xd2d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x14,%edi
lea    0xcd5(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd53(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x400,%rsp
lea    0xf2d(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    $0x1,%edi
call   1070 <dup@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11e9 <main+0x49>
mov    0x2e0d(%rip),%rax
movslq (%rax),%rdi
lea    0xe23(%rip),%rsi
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xe37(%rip),%rsi
mov    %ebx,%edi
call   10b0 <fdopen@plt>
mov    %rax,%rbx
lea    0xe28(%rip),%rdi
mov    $0x12,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
mov    $0x3,%esi
call   1050 <dup2@plt>
cmp    $0x3,%eax
je     124d <main+0xad>
mov    0x2da6(%rip),%rcx
movslq (%rcx),%rdi
lea    0xdf8(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xe13(%rip),%rsi
mov    $0x3,%edi
call   10b0 <fdopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
lea    0xdf1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
mov    $0x3,%esi
call   1050 <dup2@plt>
cmp    $0x3,%eax
je     12b8 <main+0x118>
mov    0x2d3b(%rip),%rcx
movslq (%rcx),%rdi
lea    0xdbd(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xd68(%rip),%rsi
mov    $0x3,%edi
call   10b0 <fdopen@plt>
mov    %rax,%rbx
lea    0xdc4(%rip),%rdi
mov    $0x21,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
mov    $0x14,%esi
call   1050 <dup2@plt>
cmp    $0x14,%eax
je     131b <main+0x17b>
mov    0x2cd8(%rip),%rcx
movslq (%rcx),%rdi
lea    0xd5a(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xd05(%rip),%rsi
mov    $0x14,%edi
call   10b0 <fdopen@plt>
mov    %rax,%rbx
lea    0xd83(%rip),%rdi
mov    $0x22,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"Hello world (1)\0A\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"dup(1) returned unexpected result: %d\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [19 x i8] c"Hello world (dup)\0A\00"
@.str.4 = [43 x i8] c"dup2(0, 3) returned unexpected result: %d\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [43 x i8] c"dup2(1, 3) returned unexpected result: %d\0A\00"
@.str.8 = [34 x i8] c"Good bye cruel world! dup2(1, 3)\0A\00"
@.str.9 = [35 x i8] c"Good bye cruel world! dup2(1, 20)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @fflush(i32* null)
%7 = call i32 @dup(i32 1)
store i32 %7, i32* %4
%8 = icmp eq i32 -1, %7
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i32*
%13 = load i32, i32* %4
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %9, %0
%16 = load i32, i32* %4
%17 = call i32* @fdopen(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %17, i32** %2
%18 = load i32*, i32** %2
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @fflush(i32* null)
%21 = load i32*, i32** %2
%22 = call i32 @fclose(i32* %21)
%23 = call i32 @dup2(i32 0, i32 3)
store i32 %23, i32* %4
%24 = icmp ne i32 3, %23
br i1 %24, label %25, label %31
25: ; preds = %15
%26 = load i32, i32* @stderr
%27 = sext i32 %26 to i64
%28 = inttoptr i64 %27 to i32*
%29 = load i32, i32* %4
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %25, %15
%32 = call i32* @fdopen(i32 3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %32, i32** %2
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%34 = load i32*, i32** %2
%35 = call i8* @fgets(i8* %33, i32 1024, i32* %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %35)
%37 = load i32*, i32** %2
%38 = call i32 @fclose(i32* %37)
%39 = call i32 @dup2(i32 1, i32 3)
store i32 %39, i32* %4
%40 = icmp ne i32 3, %39
br i1 %40, label %41, label %47
41: ; preds = %31
%42 = load i32, i32* @stderr
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i32*
%45 = load i32, i32* %4
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %41, %31
%48 = call i32* @fdopen(i32 3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %48, i32** %2
%49 = load i32*, i32** %2
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%51 = load i32*, i32** %2
%52 = call i32 @fclose(i32* %51)
%53 = call i32 @dup2(i32 1, i32 20)
store i32 %53, i32* %4
%54 = icmp ne i32 20, %53
br i1 %54, label %55, label %61
55: ; preds = %47
%56 = load i32, i32* @stderr
%57 = sext i32 %56 to i64
%58 = inttoptr i64 %57 to i32*
%59 = load i32, i32* %4
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %58, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %55, %47
%62 = call i32* @fdopen(i32 20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %62, i32** %2
%63 = load i32*, i32** %2
%64 = call i32 (i32*, i8*, ...) @fprintf(i32* %63, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
%65 = load i32*, i32** %2
%66 = call i32 @fclose(i32* %65)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [39 x i8] c"dup(1) returned unexpected result: %d\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [19 x i8] c"Hello world (dup)\0A\00"
@.str.4 = [43 x i8] c"dup2(0, 3) returned unexpected result: %d\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [43 x i8] c"dup2(1, 3) returned unexpected result: %d\0A\00"
@.str.8 = [34 x i8] c"Good bye cruel world! dup2(1, 3)\0A\00"
@.str.9 = [35 x i8] c"Good bye cruel world! dup2(1, 20)\0A\00"
@str = [16 x i8] c"Hello world (1)\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @fflush(i32* null)
%5 = tail call i32 @dup(i32 1)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i32*
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 -1)
br label %12
12: ; preds = %7, %0
%13 = tail call i32* @fdopen(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = tail call i64 @fwrite(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i64 18, i64 1, i32* %13)
%15 = tail call i32 @fflush(i32* null)
%16 = tail call i32 @fclose(i32* %13)
%17 = tail call i32 @dup2(i32 0, i32 3)
%18 = icmp eq i32 %17, 3
br i1 %18, label %24, label %19
19: ; preds = %12
%20 = load i32, i32* @stderr
%21 = sext i32 %20 to i64
%22 = inttoptr i64 %21 to i32*
%23 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i32 %17)
br label %24
24: ; preds = %19, %12
%25 = tail call i32* @fdopen(i32 3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%26 = call i8* @fgets(i8* %2, i32 1024, i32* %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %26)
%28 = call i32 @fclose(i32* %25)
%29 = call i32 @dup2(i32 1, i32 3)
%30 = icmp eq i32 %29, 3
br i1 %30, label %36, label %31
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = sext i32 %32 to i64
%34 = inttoptr i64 %33 to i32*
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %29)
br label %36
36: ; preds = %31, %24
%37 = call i32* @fdopen(i32 3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%38 = call i64 @fwrite(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0), i64 33, i64 1, i32* %37)
%39 = call i32 @fclose(i32* %37)
%40 = call i32 @dup2(i32 1, i32 20)
%41 = icmp eq i32 %40, 20
br i1 %41, label %47, label %42
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = sext i32 %43 to i64
%45 = inttoptr i64 %44 to i32*
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i32 %40)
br label %47
47: ; preds = %42, %36
%48 = call i32* @fdopen(i32 20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%49 = call i64 @fwrite(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0), i64 34, i64 1, i32* %48)
%50 = call i32 @fclose(i32* %48)
ret i32 0
}