#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 *);
scalar_t__ strcmp(char *, char *);
int strlen(char *);
FILE *openfile(char *filename, char *type) {
  FILE *handle;
  char command[1000];
  if (strcmp(type, "r") == 0) {
    if (strcmp(&filename[strlen(filename) - 2], ".Z") == 0) {
      sprintf(command, "zcat %s", filename);
      handle = popen(command, "r");
    } else if (strcmp(&filename[strlen(filename) - 3], ".gz") == 0) {
      sprintf(command, "gunzip -c %s", filename);
      handle = popen(command, "r");
    } else {
      handle = fopen(filename, "r");
    }
  } else {
    handle = fopen(filename, "w");
  }
  return handle;
}


x86 assembly (clang, -O0):
<openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1273 <openfile+0x123>
mov    -0x8(%rbp),%rax
mov    %rax,-0x408(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x408(%rbp),%rdi
sub    $0x2,%eax
cltq
add    %rax,%rdi
lea    0xe5b(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11e9 <openfile+0x99>
lea    -0x400(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe3d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x400(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x18(%rbp)
jmp    126e <openfile+0x11e>
mov    -0x8(%rbp),%rax
mov    %rax,-0x410(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x410(%rbp),%rdi
sub    $0x3,%eax
cltq
add    %rax,%rdi
lea    0xdfa(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1255 <openfile+0x105>
lea    -0x400(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xddd(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x400(%rbp),%rdi
lea    0xdb9(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x18(%rbp)
jmp    1269 <openfile+0x119>
mov    -0x8(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    126e <openfile+0x11e>
jmp    1287 <openfile+0x137>
mov    -0x8(%rbp),%rdi
lea    0xda0(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfile>:
push   %r14
push   %rbx
sub    $0x3e8,%rsp
mov    %rsi,%rax
mov    %rdi,%rbx
lea    0xe99(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     118d <openfile+0x3d>
lea    0xea3(%rip),%rsi
mov    %rbx,%rdi
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <fopen@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rax,%rbx,1),%rdi
add    $0xfffffffffffffffe,%rdi
lea    0xe5c(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11dc <openfile+0x8c>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
lea    (%rax,%rbx,1),%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe44(%rip),%rsi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11e5 <openfile+0x95>
lea    0xe26(%rip),%rsi
jmp    117b <openfile+0x2b>
lea    0xe22(%rip),%rsi
jmp    11ec <openfile+0x9c>
lea    0xe25(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
lea    0xdff(%rip),%rsi
mov    %r14,%rdi
call   1050 <popen@plt>
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c".Z\00"
@.str.2 = [8 x i8] c"zcat %s\00"
@.str.3 = [4 x i8] c".gz\00"
@.str.4 = [13 x i8] c"gunzip -c %s\00"
@.str.5 = [2 x i8] c"w\00"
define i32* @openfile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [1000 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i64 @strcmp(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %45
10: ; preds = %2
%11 = load i8*, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %11, i64 %15
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %10
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%21 = load i8*, i8** %3
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%24 = call i32* @popen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %5
br label %44
25: ; preds = %10
%26 = load i8*, i8** %3
%27 = load i8*, i8** %3
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %28, 3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
%32 = call i64 @strcmp(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %25
%35 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%36 = load i8*, i8** %3
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%39 = call i32* @popen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %39, i32** %5
br label %43
40: ; preds = %25
%41 = load i8*, i8** %3
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %42, i32** %5
br label %43
43: ; preds = %40, %34
br label %44
44: ; preds = %43, %19
br label %48
45: ; preds = %2
%46 = load i8*, i8** %3
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %47, i32** %5
br label %48
48: ; preds = %45, %44
%49 = load i32*, i32** %5
ret i32* %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c".Z\00"
@.str.2 = [8 x i8] c"zcat %s\00"
@.str.3 = [4 x i8] c".gz\00"
@.str.4 = [13 x i8] c"gunzip -c %s\00"
@.str.5 = [2 x i8] c"w\00"
define noalias i32* @openfile(i8* %0, i8* %1) {
%3 = alloca [1000 x i8]
%4 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %29
7: ; preds = %2
%8 = tail call i32 @strlen(i8* %0)
%9 = add nsw i32 %8, -2
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i64 @strcmp(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %7
%15 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%16 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %31
17: ; preds = %7
%18 = tail call i32 @strlen(i8* %0)
%19 = add nsw i32 %18, -3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = tail call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%26 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %31
27: ; preds = %17
%28 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %31
29: ; preds = %2
%30 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %14, %27, %24, %29
%32 = phi i32* [ %16, %14 ], [ %26, %24 ], [ %28, %27 ], [ %30, %29 ]
ret i32* %32
}