#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  int fd, newfd;
  char line[100];
  int i;
  for (i = 0; i < 3; ++i) {
    fgets(line, 100, stdin);
    printf("%s", line);
  }
  fd = open("/etc/passwd", O_RDONLY);
  if (fd == -1) {
    perror("open() failed");
    exit(1);
  }
  close(0);
  newfd = dup(fd);
  if (newfd != 0) {
    fprintf(stderr, "Could not duplicate fd to 0\n");
    exit(1);
  }
  close(fd);
  for (i = 0; i < 3; ++i) {
    fgets(line, 100, stdin);
    printf("%s", line);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x84(%rbp)
cmpl   $0x3,-0x84(%rbp)
jge    11eb <main+0x6b>
lea    -0x80(%rbp),%rdi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    -0x80(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    11a3 <main+0x23>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe08(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1223 <main+0xa3>
lea    0xdfb(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     125e <main+0xde>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdce(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x84(%rbp)
cmpl   $0x3,-0x84(%rbp)
jge    12b8 <main+0x138>
lea    -0x80(%rbp),%rdi
mov    0x2d58(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    -0x80(%rbp),%rsi
lea    0xd61(%rip),%rdi
call   1030 <printf@plt>
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    1270 <main+0xf0>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e50(%rip),%r15
mov    (%r15),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    0xe56(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
lea    0xe08(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     128f <main+0x10f>
mov    %eax,%ebx
xor    %edi,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1040 <dup@plt>
test   %eax,%eax
jne    12a5 <main+0x125>
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%r15),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    0xdbe(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd79(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
lea    0xd68(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"open() failed\00"
@stderr = global i32 0
@.str.3 = [29 x i8] c"Could not duplicate fd to 0\0A\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 [100 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
br label %10
10: ; preds = %19, %2
%11 = load i32, i32* %9
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %22
13: ; preds = %10
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %14, i32 100, i32 %15)
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %13
%20 = load i32, i32* %9
%21 = add nsw i32 %20, 1
store i32 %21, i32* %9
br label %10
22: ; preds = %10
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %22
%31 = call i32 @close(i32 0)
%32 = load i32, i32* %6
%33 = call i32 @dup(i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
%41 = load i32, i32* %6
%42 = call i32 @close(i32 %41)
store i32 0, i32* %9
br label %43
43: ; preds = %52, %40
%44 = load i32, i32* %9
%45 = icmp slt i32 %44, 3
br i1 %45, label %46, label %55
46: ; preds = %43
%47 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%48 = load i32, i32* @stdin
%49 = call i32 @fgets(i8* %47, i32 100, i32 %48)
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%51 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %46
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
store i32 %54, i32* %9
br label %43
55: ; preds = %43
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@.str.2 = [14 x i8] c"open() failed\00"
@stderr = global i32 0
@.str.3 = [29 x i8] c"Could not duplicate fd to 0\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %4, i32 100, i32 %5)
%7 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %4, i32 100, i32 %8)
%10 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %4, i32 100, i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = call i32 @close(i32 0)
%22 = call i32 @dup(i32 %15)
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = call i32 @close(i32 %15)
%30 = load i32, i32* @stdin
%31 = call i32 @fgets(i8* %4, i32 100, i32 %30)
%32 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%33 = load i32, i32* @stdin
%34 = call i32 @fgets(i8* %4, i32 100, i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%36 = load i32, i32* @stdin
%37 = call i32 @fgets(i8* %4, i32 100, i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
ret i32 0
}