#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_WRONLY;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int i = 0;
  for (i = 0; i < 10; ++i) {
    write(1, "EXAMPLE\n", 8);
  }
  int stdout_copy = dup(1);
  close(1);
  int fd = open("fff", O_WRONLY | O_CREAT, 0777);
  for (i = 0; i < 10; ++i) {
    printf("%d\n", i);
  }
  fflush(stdout);
  dup2(stdout_copy, 1);
  for (i = 0; i < 10; ++i) {
    write(1, "HELLO\n", 6);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    11bb <main+0x4b>
mov    $0x1,%edi
lea    0xe5d(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    118d <main+0x1d>
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,-0xc(%rbp)
mov    $0x1,%edi
call   1070 <close@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
or     (%rax),%esi
lea    0xe1e(%rip),%rdi
mov    $0x1ff,%edx
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1226 <main+0xb6>
mov    -0x8(%rbp),%esi
lea    0xdfa(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11ff <main+0x8f>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0xa,-0x8(%rbp)
jge    1276 <main+0x106>
mov    $0x1,%edi
lea    0xdb3(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1248 <main+0xd8>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe86(%rip),%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,%ebp
mov    $0x1,%edi
call   1070 <close@plt>
mov    0x2d7d(%rip),%rax
mov    0x2da6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdac(%rip),%rdi
mov    $0x1ff,%edx
call   1090 <open@plt>
lea    0xd9f(%rip),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x3,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x4,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x5,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x6,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x7,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x8,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1050 <printf@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
lea    0xd03(%rip),%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"EXAMPLE\0A\00"
@.str.1 = [4 x i8] c"fff\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@stdout = global i32 0
@.str.3 = [7 x i8] c"HELLO\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %2
br label %5
5: ; preds = %10, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 10
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
br label %10
10: ; preds = %8
%11 = load i32, i32* %2
%12 = add nsw i32 %11, 1
store i32 %12, i32* %2
br label %5
13: ; preds = %5
%14 = call i32 @dup(i32 1)
store i32 %14, i32* %3
%15 = call i32 @close(i32 1)
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 511)
store i32 %19, i32* %4
store i32 0, i32* %2
br label %20
20: ; preds = %26, %13
%21 = load i32, i32* %2
%22 = icmp slt i32 %21, 10
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = load i32, i32* %2
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23
%27 = load i32, i32* %2
%28 = add nsw i32 %27, 1
store i32 %28, i32* %2
br label %20
29: ; preds = %20
%30 = load i32, i32* @stdout
%31 = call i32 @fflush(i32 %30)
%32 = load i32, i32* %3
%33 = call i32 @dup2(i32 %32, i32 1)
store i32 0, i32* %2
br label %34
34: ; preds = %39, %29
%35 = load i32, i32* %2
%36 = icmp slt i32 %35, 10
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
br label %39
39: ; preds = %37
%40 = load i32, i32* %2
%41 = add nsw i32 %40, 1
store i32 %41, i32* %2
br label %34
42: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"EXAMPLE\0A\00"
@.str.1 = [4 x i8] c"fff\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@stdout = global i32 0
@.str.3 = [7 x i8] c"HELLO\0A\00"
define i32 @main() {
%1 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%2 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%3 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%4 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%5 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%6 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%7 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%8 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%9 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%10 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 8)
%11 = tail call i32 @dup(i32 1)
%12 = tail call i32 @close(i32 1)
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 511)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 0)
%18 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 1)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 2)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 3)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 4)
%22 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 5)
%23 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 6)
%24 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 7)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 8)
%26 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 9)
%27 = load i32, i32* @stdout
%28 = tail call i32 @fflush(i32 %27)
%29 = tail call i32 @dup2(i32 %11, i32 1)
%30 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%31 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%32 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%33 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%34 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%35 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%36 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%37 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%38 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
%39 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 6)
ret i32 0
}