#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 size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
int main() {
  int fd = open("./log", O_CREAT | O_WRONLY, 0644);
  if (fd < 0) {
    perror("open");
    return 1;
  }
  int new_fd = dup2(fd, 1);
  if (new_fd < 0) {
    perror("dup2");
    return 3;
  }
  printf("new_fd :[%d], old_fd:[%d] \n", new_fd, fd);
  char buf[1024];
  int done = 0;
  while (!done) {
    memset(buf, '\0', sizeof(buf));
    ssize_t size = read(0, buf, sizeof(buf) - 1);
    if (size > 0) {
      buf[size] = '\0';
      printf("%s", buf);
      fflush(stdout);
    }
  }
  close(fd);
  close(new_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
lea    0xe55(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11da <main+0x5a>
lea    0xe3d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12cc <main+0x14c>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    120c <main+0x8c>
lea    0xe10(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12cc <main+0x14c>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1243 <main+0xc3>
jmp    12b5 <main+0x135>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x410(%rbp),%rsi
xor    %edi,%edi
mov    $0x3ff,%edx
call   1070 <read@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jbe    12b0 <main+0x130>
mov    -0x420(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
jmp    122a <main+0xaa>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e75(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6b(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     121e <main+0xae>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
test   %eax,%eax
js     122c <main+0xbc>
lea    0xe54(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%rbx
lea    0xe5b(%rip),%r14
mov    0x2e00(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
test   %rax,%rax
je     11e0 <main+0x70>
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%edi
call   1070 <fflush@plt>
jmp    11e0 <main+0x70>
mov    $0x1,%ebx
lea    0xddc(%rip),%rdi
jmp    1238 <main+0xc8>
mov    $0x3,%ebx
lea    0xdd3(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"./log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"dup2\00"
@.str.3 = [28 x i8] c"new_fd :[%d], old_fd:[%d] \0A\00"
@.str.4 = [3 x i8] c"%s\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i64
store i32 0, i32* %1
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %9, i32 420)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %50
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i32 @dup2(i32 %16, i32 1)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %1
br label %50
22: ; preds = %15
%23 = load i32, i32* %3
%24 = load i32, i32* %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %24)
store i32 0, i32* %5
br label %26
26: ; preds = %44, %22
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = call i32 @memset(i8* %31, i8 signext 0, i32 1024)
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = call i64 @read(i32 0, i8* %33, i32 1023)
store i64 %34, i64* %6
%35 = load i64, i64* %6
%36 = icmp ugt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %30
%38 = load i64, i64* %6
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @stdout
%43 = call i32 @fflush(i32 %42)
br label %44
44: ; preds = %37, %30
br label %26
45: ; preds = %26
%46 = load i32, i32* %2
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %3
%49 = call i32 @close(i32 %48)
store i32 0, i32* %1
br label %50
50: ; preds = %45, %20, %13
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [6 x i8] c"./log\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [5 x i8] c"dup2\00"
@.str.3 = [28 x i8] c"new_fd :[%d], old_fd:[%d] \0A\00"
@.str.4 = [3 x i8] c"%s\00"
@stdout = global i32 0
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_CREAT
%3 = load i32, i32* @O_WRONLY
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %4, i32 420)
%6 = icmp slt i32 %5, 0
br i1 %6, label %23, label %7
7: ; preds = %0
%8 = tail call i32 @dup2(i32 %5, i32 1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %8, i32 %5)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
br label %13
13: ; preds = %22, %10
%14 = call i32 @memset(i8* %12, i8 signext 0, i32 1024)
%15 = call i64 @read(i32 0, i8* %12, i32 1023)
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %15
store i8 0, i8* %18
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%20 = load i32, i32* @stdout
%21 = call i32 @fflush(i32 %20)
br label %22
22: ; preds = %17, %13
br label %13
23: ; preds = %7, %0
%24 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %7 ]
%25 = phi i32 [ 1, %0 ], [ 3, %7 ]
%26 = tail call i32 @perror(i8* %24)
ret i32 %25
}