#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int open(char *, int, int);
int open_net_fd(char *);
int open_pipe_fd(char *);
int open_gen_fd(char *spec) {
  int fd;
  if (*spec == '|') {
    fd = open_pipe_fd(spec + 1);
  } else if (*spec == ':') {
    fd = open_net_fd(spec + 1);
  } else {
    fd = open(spec, O_WRONLY | O_CREAT | O_APPEND,
              S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_gen_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x7c,%eax
jne    1161 <open_gen_fd+0x31>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1030 <open_pipe_fd@plt>
mov    %eax,-0xc(%rbp)
jmp    11d6 <open_gen_fd+0xa6>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1186 <open_gen_fd+0x56>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1040 <open_net_fd@plt>
mov    %eax,-0xc(%rbp)
jmp    11d1 <open_gen_fd+0xa1>
mov    -0x8(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    0x2e2b(%rip),%rax
or     (%rax),%edx
mov    0x2e0a(%rip),%rax
or     (%rax),%edx
mov    0x2e09(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
jmp    11d6 <open_gen_fd+0xa6>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_gen_fd>:
mov    (%rdi),%al
cmp    $0x3a,%al
je     1143 <open_gen_fd+0x13>
cmp    $0x7c,%al
jne    114c <open_gen_fd+0x1c>
add    $0x1,%rdi
jmp    1030 <open_pipe_fd@plt>
add    $0x1,%rdi
jmp    1040 <open_net_fd@plt>
mov    0x2e5d(%rip),%rax
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e43(%rip),%rax
or     (%rax),%esi
mov    0x2e52(%rip),%rax
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e48(%rip),%rax
or     (%rax),%edx
mov    0x2e47(%rip),%rax
or     (%rax),%edx
jmp    1050 <open@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IRGRP = global i32 0
define i32 @open_gen_fd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i8, i8* %4
%6 = sext i8 %5 to i32
%7 = icmp eq i32 %6, 124
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = getelementptr inbounds i8, i8* %9, i64 1
%11 = call i32 @open_pipe_fd(i8* %10)
store i32 %11, i32* %3
br label %37
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 58
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i32 @open_net_fd(i8* %19)
store i32 %20, i32* %3
br label %36
21: ; preds = %12
%22 = load i8*, i8** %2
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = load i32, i32* @O_APPEND
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IROTH
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRGRP
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %22, i32 %27, i32 %34)
store i32 %35, i32* %3
br label %36
36: ; preds = %21, %17
br label %37
37: ; preds = %36, %8
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IROTH = global i32 0
@S_IRGRP = global i32 0
define i32 @open_gen_fd(i8* %0) {
%2 = load i8, i8* %0
switch i8 %2, label %9 [
i8 124, label %3
i8 58, label %6
]
3: ; preds = %1
%4 = getelementptr inbounds i8, i8* %0, i64 1
%5 = tail call i32 @open_pipe_fd(i8* %4)
br label %23
6: ; preds = %1
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = tail call i32 @open_net_fd(i8* %7)
br label %23
9: ; preds = %1
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %11, %10
%13 = load i32, i32* @O_APPEND
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRGRP
%21 = or i32 %19, %20
%22 = tail call i32 @open(i8* %0, i32 %14, i32 %21)
br label %23
23: ; preds = %6, %9, %3
%24 = phi i32 [ %5, %3 ], [ %8, %6 ], [ %22, %9 ]
ret i32 %24
}