#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 STDOUT_FILENO;
int *malloc(int);
int open(char *, int, int);
int optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcmp(char *, char *);
int *fill_fd(int argc, char **argv, int oflag, int bflag) {
  int *ret = malloc(sizeof(int) * (argc + 1));
  int i;
  for (i = optind; i < argc; i++) {
    if (!strcmp(argv[i], "-"))
      ret[i] = STDOUT_FILENO;
    else
      ret[i] = open(argv[i], oflag, bflag);
    if (ret[i] < 0)
      perror("tee");
  }
  ret[argc] = -2;
  return ret;
}


x86 assembly (clang, -O0):
<fill_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1213 <fill_fd+0xd3>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe69(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11be <fill_fd+0x7e>
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    11e2 <fill_fd+0xa2>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <open@plt>
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jge    1200 <fill_fd+0xc0>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
jmp    1205 <fill_fd+0xc5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1178 <fill_fd+0x38>
mov    -0x20(%rbp),%rax
movslq -0x4(%rbp),%rcx
movl   $0xfffffffe,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x4(%rsp)
mov    %edx,(%rsp)
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x4(,%rbp,4),%edi
call   1040 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    0x2e6e(%rip),%rax
movslq (%rax),%rax
mov    %rbp,0x10(%rsp)
cmp    %ebp,%eax
jge    11ec <fill_fd+0xac>
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,4),%r15
lea    (%rbx,%rax,8),%r14
mov    0x10(%rsp),%r12
sub    %eax,%r12d
xor    %ebp,%ebp
lea    0xe64(%rip),%rbx
jmp    11c0 <fill_fd+0x80>
xchg   %ax,%ax
mov    %r13,%rdi
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
call   1050 <open@plt>
mov    %eax,(%r15,%rbp,4)
test   %eax,%eax
js     11de <fill_fd+0x9e>
add    $0x1,%rbp
cmp    %ebp,%r12d
je     11ec <fill_fd+0xac>
mov    (%r14,%rbp,8),%r13
mov    %r13,%rdi
mov    %rbx,%rsi
call   1030 <strcmp@plt>
test   %eax,%eax
jne    11a0 <fill_fd+0x60>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
jmp    11af <fill_fd+0x6f>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
jmp    11b7 <fill_fd+0x77>
movslq 0x10(%rsp),%rcx
mov    0x8(%rsp),%rax
movl   $0xfffffffe,(%rax,%rcx,4)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@optind = global i32 0
@.str = [2 x i8] c"-\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [4 x i8] c"tee\00"
define i32* @fill_fd(i32 %0, i8** %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = mul i64 4, %13
%15 = trunc i64 %14 to i32
%16 = call i32* @malloc(i32 %15)
store i32* %16, i32** %9
%17 = load i32, i32* @optind
store i32 %17, i32* %10
br label %18
18: ; preds = %59, %4
%19 = load i32, i32* %10
%20 = load i32, i32* %5
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %62
22: ; preds = %18
%23 = load i8**, i8*** %6
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8*, i8** %23, i64 %25
%27 = load i8*, i8** %26
%28 = call i32 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%29 = icmp ne i32 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %22
%31 = load i32, i32* @STDOUT_FILENO
%32 = load i32*, i32** %9
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
store i32 %31, i32* %35
br label %49
36: ; preds = %22
%37 = load i8**, i8*** %6
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %37, i64 %39
%41 = load i8*, i8** %40
%42 = load i32, i32* %7
%43 = load i32, i32* %8
%44 = call i32 @open(i8* %41, i32 %42, i32 %43)
%45 = load i32*, i32** %9
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %44, i32* %48
br label %49
49: ; preds = %36, %30
%50 = load i32*, i32** %9
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i32, i32* %50, i64 %52
%54 = load i32, i32* %53
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %49
%57 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %58
58: ; preds = %56, %49
br label %59
59: ; preds = %58
%60 = load i32, i32* %10
%61 = add nsw i32 %60, 1
store i32 %61, i32* %10
br label %18
62: ; preds = %18
%63 = load i32*, i32** %9
%64 = load i32, i32* %5
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %63, i64 %65
store i32 -2, i32* %66
%67 = load i32*, i32** %9
ret i32* %67
}

LLVM IR (-O2):
@optind = global i32 0
@.str = [2 x i8] c"-\00"
@STDOUT_FILENO = global i32 0
@.str.1 = [4 x i8] c"tee\00"
define noalias i32* @fill_fd(i32 %0, i8** nocapture %1, i32 %2, i32 %3) {
%5 = shl i32 %0, 2
%6 = add i32 %5, 4
%7 = tail call i32* @malloc(i32 %6)
%8 = load i32, i32* @optind
%9 = icmp slt i32 %8, %0
br i1 %9, label %10, label %32
10: ; preds = %4
%11 = sext i32 %8 to i64
br label %12
12: ; preds = %10, %28
%13 = phi i64 [ %11, %10 ], [ %29, %28 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 %13
%15 = load i8*, i8** %14
%16 = tail call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = load i32, i32* @STDOUT_FILENO
br label %22
20: ; preds = %12
%21 = tail call i32 @open(i8* %15, i32 %2, i32 %3)
br label %22
22: ; preds = %20, %18
%23 = phi i32 [ %19, %18 ], [ %21, %20 ]
%24 = getelementptr inbounds i32, i32* %7, i64 %13
store i32 %23, i32* %24
%25 = icmp slt i32 %23, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %22, %26
%29 = add nsw i64 %13, 1
%30 = trunc i64 %29 to i32
%31 = icmp eq i32 %30, %0
br i1 %31, label %32, label %12
32: ; preds = %28, %4
%33 = sext i32 %0 to i64
%34 = getelementptr inbounds i32, i32* %7, i64 %33
store i32 -2, i32* %34
ret i32* %7
}