#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*/ free(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
char *xmalloc(int);
FILE *myopen(char *name, char *mode, int *flag) {
  int len = strlen(name);
  if (!strcmp(name + len - 3, ".gz")) {
    FILE *res;
    char *cmdline = xmalloc(len + 6);
    sprintf(cmdline, "zcat %s", name);
    res = popen(cmdline, mode);
    free(cmdline);
    *flag = 1;
    return res;
  }
  *flag = 0;
  return fopen(name, mode);
}


x86 assembly (clang, -O0):
<myopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe5a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1208 <myopen+0x98>
mov    -0x24(%rbp),%edi
add    $0x6,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe32(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <popen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
movl   $0x1,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1223 <myopen+0xb3>
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
cltq
lea    (%rbx,%rax,1),%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe68(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11bd <myopen+0x4d>
movl   $0x0,(%r14)
mov    %rbx,%rdi
mov    %r15,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <fopen@plt>
add    $0x6,%ebp
mov    %ebp,%edi
call   1050 <xmalloc@plt>
mov    %rax,%rbp
lea    0xe33(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <popen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1030 <free@plt>
movl   $0x1,(%r14)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.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*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %8
%13 = load i8*, i8** %5
%14 = load i32, i32* %8
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -3
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %34, label %20
20: ; preds = %3
%21 = load i32, i32* %8
%22 = add nsw i32 %21, 6
%23 = call i8* @xmalloc(i32 %22)
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = load i8*, i8** %5
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %10
%28 = load i8*, i8** %6
%29 = call i32* @popen(i8* %27, i8* %28)
store i32* %29, i32** %9
%30 = load i8*, i8** %10
%31 = call i32 @free(i8* %30)
%32 = load i32*, i32** %7
store i32 1, i32* %32
%33 = load i32*, i32** %9
store i32* %33, i32** %4
br label %39
34: ; preds = %3
%35 = load i32*, i32** %7
store i32 0, i32* %35
%36 = load i8*, i8** %5
%37 = load i8*, i8** %6
%38 = call i32* @fopen(i8* %36, i8* %37)
store i32* %38, i32** %4
br label %39
39: ; preds = %34, %20
%40 = load i32*, i32** %4
ret i32* %40
}

LLVM IR (-O2):
@.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 %16
10: ; preds = %3
%11 = add nsw i32 %4, 6
%12 = tail call i8* @xmalloc(i32 %11)
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = tail call i32* @popen(i8* %12, i8* %1)
%15 = tail call i32 @free(i8* %12)
store i32 1, i32* %2
br label %18
16: ; preds = %3
store i32 0, i32* %2
%17 = tail call i32* @fopen(i8* %0, i8* %1)
br label %18
18: ; preds = %16, %10
%19 = phi i32* [ %17, %16 ], [ %14, %10 ]
ret i32* %19
}