#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 /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ dup2(int, int);
int open(char *, int, ...);
scalar_t__ read(int /*<<< orphan*/, char **, int);
int /*<<< orphan*/ write(int, char **, int);
int main(int argc, char **argv) {
  int passwd = open("/etc/passwd", O_RDONLY);
  int saida = open("saida.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  int erros = open("erros.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  char buf[100];
  dup2(passwd, 0);
  dup2(saida, 1);
  dup2(erros, 2);
  while (read(0, &buf, 100)) {
    write(1, &buf, 100);
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e75(%rip),%rax
or     (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
lea    0xe77(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
lea    0xe50(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
lea    -0x80(%rbp),%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
je     122b <main+0xeb>
lea    -0x80(%rbp),%rsi
mov    $0x1,%edi
mov    $0x64,%edx
call   1030 <write@plt>
jmp    11f9 <main+0xb9>
mov    $0x1,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xea4(%rip),%rdi
xor    %eax,%eax
call   1060 <open@plt>
mov    0x2e56(%rip),%r15
mov    0x2e87(%rip),%rbx
mov    (%rbx),%esi
or     (%r15),%esi
mov    %eax,%r14d
mov    0x2e70(%rip),%r12
or     (%r12),%esi
lea    0xe81(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
or     (%r15),%esi
or     (%r12),%esi
lea    0xe6d(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
mov    %eax,%ebx
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x64,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1216 <main+0xd6>
mov    %rsp,%rbx
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1030 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1050 <read@plt>
test   %rax,%rax
jne    11f0 <main+0xb0>
mov    $0x1,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"saida.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [10 x i8] c"erros.txt\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDONLY
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 438)
store i32 %17, i32* %7
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 438)
store i32 %23, i32* %8
%24 = load i32, i32* %6
%25 = call i32 @dup2(i32 %24, i32 0)
%26 = load i32, i32* %7
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = load i32, i32* %8
%29 = call i32 @dup2(i32 %28, i32 2)
br label %30
30: ; preds = %34, %2
%31 = bitcast [100 x i8]* %9 to i8**
%32 = call i64 @read(i32 0, i8** %31, i32 100)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = bitcast [100 x i8]* %9 to i8**
%36 = call i32 @write(i32 1, i8** %35, i32 100)
br label %30
37: ; preds = %30
ret i32 1
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"saida.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [10 x i8] c"erros.txt\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 438)
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %16, i32 438)
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%19 = tail call i32 @dup2(i32 %5, i32 0)
%20 = tail call i32 @dup2(i32 %11, i32 1)
%21 = tail call i32 @dup2(i32 %17, i32 2)
%22 = bitcast [100 x i8]* %3 to i8**
%23 = call i64 @read(i32 0, i8** %22, i32 100)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %2, %25
%26 = call i32 @write(i32 1, i8** %22, i32 100)
%27 = call i64 @read(i32 0, i8** %22, i32 100)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %25
29: ; preds = %25, %2
ret i32 1
}