#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
FILE *myopen(char *name, char *mode, int *flag) {
  static char cmdline[128];
  if (!strcmp(name + strlen(name) - 3, ".gz")) {
    *flag = 1;
    sprintf(cmdline, "zcat %s", name);
    return popen(cmdline, mode);
  }
  *flag = 0;
  return fopen(name, mode);
}


x86 assembly (clang, -O0):
<myopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x28(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe77(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11d1 <myopen+0x81>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x10(%rbp),%rdx
lea    0x2eb4(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rsi
lea    0x2e9d(%rip),%rdi
call   1050 <popen@plt>
mov    %rax,-0x8(%rbp)
jmp    11ec <myopen+0x9c>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myopen>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
cltq
lea    (%rbx,%rax,1),%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe8c(%rip),%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     1194 <myopen+0x44>
movl   $0x0,(%r15)
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1060 <fopen@plt>
movl   $0x1,(%r15)
lea    0x2ebe(%rip),%r15
lea    0xe5b(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <popen@plt>

LLVM IR (-O0):
@myopen.cmdline = internal global [128 x i8] zeroinitializer
@.str = [4 x i8] c".gz\00"
@.str.1 = [8 x i8] c"zcat %s\00"
define i32* @myopen(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%8 = load i8*, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
%13 = getelementptr inbounds i8, i8* %12, i64 -3
%14 = call i32 @strcmp(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %3
%17 = load i32*, i32** %7
store i32 1, i32* %17
%18 = load i8*, i8** %5
%19 = call i32 @sprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @myopen.cmdline, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %6
%21 = call i32* @popen(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @myopen.cmdline, i64 0, i64 0), i8* %20)
store i32* %21, i32** %4
br label %27
22: ; preds = %3
%23 = load i32*, i32** %7
store i32 0, i32* %23
%24 = load i8*, i8** %5
%25 = load i8*, i8** %6
%26 = call i32* @fopen(i8* %24, i8* %25)
store i32* %26, i32** %4
br label %27
27: ; preds = %22, %16
%28 = load i32*, i32** %4
ret i32* %28
}

LLVM IR (-O2):
@myopen.cmdline = internal global [128 x i8] zeroinitializer
@.str = [4 x i8] c".gz\00"
@.str.1 = [8 x i8] c"zcat %s\00"
define noalias i32* @myopen(i8* %0, i8* nocapture %1, i32* nocapture writeonly %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = getelementptr inbounds i8, i8* %6, i64 -3
%8 = tail call i32 @strcmp(i8* %7, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %3
store i32 1, i32* %2
%11 = tail call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([128 x i8], [128 x i8]* @myopen.cmdline, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = tail call i32* @popen(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @myopen.cmdline, i64 0, i64 0), i8* %1)
br label %15
13: ; preds = %3
store i32 0, i32* %2
%14 = tail call i32* @fopen(i8* %0, i8* %1)
br label %15
15: ; preds = %13, %10
%16 = phi i32* [ %14, %13 ], [ %12, %10 ]
ret i32* %16
}