#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_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SIGABRT;
int /*<<< orphan*/ SIGINT;
int /*<<< orphan*/ SIGQUIT;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ STDIN_FILENO;
int STDOUT_FILENO;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ bake_args(int, char **, int *);
int *fill_fd(int, char **, int, int);
int /*<<< orphan*/ free(int *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int opt, oflag = (O_WRONLY | O_CREAT),
           bflag = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  bake_args(argc, argv, &opt);
  if (opt & (1 << 1))
    oflag |= O_APPEND;
  if (opt & (1 << 0)) {
    signal(SIGINT, SIG_IGN);
    signal(SIGQUIT, SIG_IGN);
    signal(SIGABRT, SIG_IGN);
    signal(SIGTERM, SIG_IGN);
  }
  int *fd_list = fill_fd(argc, argv, oflag, bflag);
  char buffer[128 + 1];
  int n_read = read(STDIN_FILENO, buffer, 128);
  while (n_read > 0) {
    int i;
    for (i = 0; fd_list[i] != -2; i++)
      if (fd_list[i] > 0)
        write(fd_list[i], buffer, n_read);
    write(STDOUT_FILENO, buffer, n_read);
    n_read = read(STDIN_FILENO, buffer, 128);
  }
  free(fd_list);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    0x2e6f(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    0x2e22(%rip),%rcx
or     (%rcx),%eax
mov    0x2e01(%rip),%rcx
or     (%rcx),%eax
mov    0x2df0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <bake_args@plt>
mov    -0x14(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11e3 <main+0x83>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     124e <main+0xee>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
call   1050 <fill_fd@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jle    133e <main+0x1de>
movl   $0x0,-0xb8(%rbp)
mov    -0x28(%rbp),%rax
movslq -0xb8(%rbp),%rcx
cmpl   $0xfffffffe,(%rax,%rcx,4)
je     12fe <main+0x19e>
mov    -0x28(%rbp),%rax
movslq -0xb8(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jle    12e5 <main+0x185>
mov    -0x28(%rbp),%rax
movslq -0xb8(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0xb0(%rbp),%rsi
mov    -0xb4(%rbp),%edx
call   1040 <write@plt>
jmp    12ea <main+0x18a>
mov    -0xb8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb8(%rbp)
jmp    129b <main+0x13b>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    -0xb0(%rbp),%rsi
mov    -0xb4(%rbp),%edx
call   1040 <write@plt>
mov    0x2c68(%rip),%rax
mov    (%rax),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,-0xb4(%rbp)
jmp    1284 <main+0x124>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2e00(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%r12d
or     (%rax),%r12d
mov    0x2e0c(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e0a(%rip),%rax
or     (%rax),%ebx
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2df3(%rip),%rax
or     (%rax),%ebx
lea    0xc(%rsp),%rdx
call   1080 <bake_args@plt>
mov    0xc(%rsp),%eax
mov    %eax,%ebp
shl    $0x1e,%ebp
sar    $0x1f,%ebp
mov    0x2da4(%rip),%rcx
and    (%rcx),%ebp
or     %r12d,%ebp
test   $0x1,%al
je     1224 <main+0xc4>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2dd3(%rip),%r12
mov    (%r12),%esi
call   1070 <signal@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%esi
call   1070 <signal@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%esi
call   1070 <signal@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%esi
call   1070 <signal@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1050 <fill_fd@plt>
mov    %rax,%r14
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    12cd <main+0x16d>
mov    %eax,%ebx
mov    %r14,%r15
add    $0x4,%r15
mov    0x2d86(%rip),%r13
lea    0x10(%rsp),%rbp
jmp    129a <main+0x13a>
nopl   0x0(%rax)
mov    0x0(%r13),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    12cd <main+0x16d>
mov    (%r14),%edi
cmp    $0xfffffffe,%edi
je     1270 <main+0x110>
mov    %r15,%r12
jmp    12bd <main+0x15d>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
add    $0x4,%r12
cmp    $0xfffffffe,%edi
je     1270 <main+0x110>
test   %edi,%edi
jle    12b0 <main+0x150>
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
jmp    12b0 <main+0x150>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@O_APPEND = global i32 0
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
@SIGABRT = global i32 0
@SIGTERM = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
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 i32*
%10 = alloca [129 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
store i32 %15, i32* %7
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IROTH
%22 = or i32 %20, %21
store i32 %22, i32* %8
%23 = load i32, i32* %4
%24 = load i8**, i8*** %5
%25 = call i32 @bake_args(i32 %23, i8** %24, i32* %6)
%26 = load i32, i32* %6
%27 = and i32 %26, 2
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %2
%30 = load i32, i32* @O_APPEND
%31 = load i32, i32* %7
%32 = or i32 %31, %30
store i32 %32, i32* %7
br label %33
33: ; preds = %29, %2
%34 = load i32, i32* %6
%35 = and i32 %34, 1
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %50
37: ; preds = %33
%38 = load i32, i32* @SIGINT
%39 = load i32, i32* @SIG_IGN
%40 = call i32 @signal(i32 %38, i32 %39)
%41 = load i32, i32* @SIGQUIT
%42 = load i32, i32* @SIG_IGN
%43 = call i32 @signal(i32 %41, i32 %42)
%44 = load i32, i32* @SIGABRT
%45 = load i32, i32* @SIG_IGN
%46 = call i32 @signal(i32 %44, i32 %45)
%47 = load i32, i32* @SIGTERM
%48 = load i32, i32* @SIG_IGN
%49 = call i32 @signal(i32 %47, i32 %48)
br label %50
50: ; preds = %37, %33
%51 = load i32, i32* %4
%52 = load i8**, i8*** %5
%53 = load i32, i32* %7
%54 = load i32, i32* %8
%55 = call i32* @fill_fd(i32 %51, i8** %52, i32 %53, i32 %54)
store i32* %55, i32** %9
%56 = load i32, i32* @STDIN_FILENO
%57 = getelementptr inbounds [129 x i8], [129 x i8]* %10, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 128)
store i32 %58, i32* %11
br label %59
59: ; preds = %90, %50
%60 = load i32, i32* %11
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %98
62: ; preds = %59
store i32 0, i32* %12
br label %63
63: ; preds = %87, %62
%64 = load i32*, i32** %9
%65 = load i32, i32* %12
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i32, i32* %64, i64 %66
%68 = load i32, i32* %67
%69 = icmp ne i32 %68, -2
br i1 %69, label %70, label %90
70: ; preds = %63
%71 = load i32*, i32** %9
%72 = load i32, i32* %12
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %74
%76 = icmp sgt i32 %75, 0
br i1 %76, label %77, label %86
77: ; preds = %70
%78 = load i32*, i32** %9
%79 = load i32, i32* %12
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %78, i64 %80
%82 = load i32, i32* %81
%83 = getelementptr inbounds [129 x i8], [129 x i8]* %10, i64 0, i64 0
%84 = load i32, i32* %11
%85 = call i32 @write(i32 %82, i8* %83, i32 %84)
br label %86
86: ; preds = %77, %70
br label %87
87: ; preds = %86
%88 = load i32, i32* %12
%89 = add nsw i32 %88, 1
store i32 %89, i32* %12
br label %63
90: ; preds = %63
%91 = load i32, i32* @STDOUT_FILENO
%92 = getelementptr inbounds [129 x i8], [129 x i8]* %10, i64 0, i64 0
%93 = load i32, i32* %11
%94 = call i32 @write(i32 %91, i8* %92, i32 %93)
%95 = load i32, i32* @STDIN_FILENO
%96 = getelementptr inbounds [129 x i8], [129 x i8]* %10, i64 0, i64 0
%97 = call i32 @read(i32 %95, i8* %96, i32 128)
store i32 %97, i32* %11
br label %59
98: ; preds = %59
%99 = load i32*, i32** %9
%100 = call i32 @free(i32* %99)
ret i32 0
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@O_APPEND = global i32 0
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGQUIT = global i32 0
@SIGABRT = global i32 0
@SIGTERM = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca [129 x i8]
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = call i32 @bake_args(i32 %0, i8** %1, i32* %3)
%17 = load i32, i32* %3
%18 = and i32 %17, 2
%19 = icmp eq i32 %18, 0
%20 = load i32, i32* @O_APPEND
%21 = select i1 %19, i32 0, i32 %20
%22 = or i32 %8, %21
%23 = and i32 %17, 1
%24 = icmp eq i32 %23, 0
br i1 %24, label %38, label %25
25: ; preds = %2
%26 = load i32, i32* @SIGINT
%27 = load i32, i32* @SIG_IGN
%28 = call i32 @signal(i32 %26, i32 %27)
%29 = load i32, i32* @SIGQUIT
%30 = load i32, i32* @SIG_IGN
%31 = call i32 @signal(i32 %29, i32 %30)
%32 = load i32, i32* @SIGABRT
%33 = load i32, i32* @SIG_IGN
%34 = call i32 @signal(i32 %32, i32 %33)
%35 = load i32, i32* @SIGTERM
%36 = load i32, i32* @SIG_IGN
%37 = call i32 @signal(i32 %35, i32 %36)
br label %38
38: ; preds = %25, %2
%39 = call i32* @fill_fd(i32 %0, i8** %1, i32 %22, i32 %15)
%40 = getelementptr inbounds [129 x i8], [129 x i8]* %4, i64 0, i64 0
%41 = load i32, i32* @STDIN_FILENO
%42 = call i32 @read(i32 %41, i8* %40, i32 128)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %65
44: ; preds = %38, %59
%45 = phi i32 [ %63, %59 ], [ %42, %38 ]
%46 = load i32, i32* %39
%47 = icmp eq i32 %46, -2
br i1 %47, label %59, label %48
48: ; preds = %44, %54
%49 = phi i64 [ %55, %54 ], [ 0, %44 ]
%50 = phi i32 [ %57, %54 ], [ %46, %44 ]
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @write(i32 %50, i8* %40, i32 %45)
br label %54
54: ; preds = %48, %52
%55 = add nuw i64 %49, 1
%56 = getelementptr inbounds i32, i32* %39, i64 %55
%57 = load i32, i32* %56
%58 = icmp eq i32 %57, -2
br i1 %58, label %59, label %48
59: ; preds = %54, %44
%60 = load i32, i32* @STDOUT_FILENO
%61 = call i32 @write(i32 %60, i8* %40, i32 %45)
%62 = load i32, i32* @STDIN_FILENO
%63 = call i32 @read(i32 %62, i8* %40, i32 128)
%64 = icmp sgt i32 %63, 0
br i1 %64, label %44, label %65
65: ; preds = %59, %38
%66 = call i32 @free(i32* %39)
ret i32 0
}