#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 */
char *alloca(size_t);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ *fopen_uncompressed(char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
char *stpcpy(char *, char const *);
size_t strlen(char const *);
FILE *charmap_open(const char *directory, const char *name) {
  size_t dlen = strlen(directory);
  int add_slash = (dlen == 0 || directory[dlen - 1] != '/');
  size_t nlen = strlen(name);
  char *pathname;
  char *p;
  FILE *stream;
  pathname = alloca(dlen + add_slash + nlen + 5);
  p = stpcpy(pathname, directory);
  if (add_slash)
    *p++ = '/';
  p = stpcpy(p, name);
  stream = fopen(pathname, "rm");
  if (stream != NULL)
    return stream;
  memcpy(p, ".gz", 4);
  stream = fopen_uncompressed(pathname, "gzip");
  if (stream != NULL)
    return stream;
  memcpy(p, ".bz2", 5);
  stream = fopen_uncompressed(pathname, "bzip2");
  if (stream != NULL)
    return stream;
  return NULL;
}


x86 assembly (clang, -O0):
<charmap_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    $0x1,%al
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x49(%rbp)
je     11a6 <charmap_open+0x46>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
add    -0x30(%rbp),%rdi
add    $0x5,%rdi
call   1060 <alloca@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <stpcpy@plt>
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1207 <charmap_open+0xa7>
mov    -0x40(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x40(%rbp)
movb   $0x2f,(%rax)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <stpcpy@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1244 <charmap_open+0xe4>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ce <charmap_open+0x16e>
mov    -0x40(%rbp),%rdi
lea    0xdb4(%rip),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x38(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   1030 <fopen_uncompressed@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1285 <charmap_open+0x125>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ce <charmap_open+0x16e>
mov    -0x40(%rbp),%rdi
lea    0xd7c(%rip),%rsi
mov    $0x5,%edx
call   1070 <memcpy@plt>
mov    -0x38(%rbp),%rdi
lea    0xd6c(%rip),%rsi
call   1030 <fopen_uncompressed@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12c6 <charmap_open+0x166>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12ce <charmap_open+0x16e>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<charmap_open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1187 <charmap_open+0x27>
cmpb   $0x2f,-0x1(%rbx,%r12,1)
setne  %bpl
jmp    118a <charmap_open+0x2a>
mov    $0x1,%bpl
mov    %r14,%rdi
call   1050 <strlen@plt>
movzbl %bpl,%ebp
add    %rbp,%rbx
lea    (%rax,%rbx,1),%rdi
add    $0x5,%rdi
call   1060 <alloca@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <stpcpy@plt>
test   %bpl,%bpl
je     11c0 <charmap_open+0x60>
movb   $0x2f,(%rax)
add    $0x1,%rax
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <stpcpy@plt>
mov    %rax,%r14
lea    0xe2b(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
jne    120a <charmap_open+0xaa>
lea    0xe1a(%rip),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    0xe0a(%rip),%rsi
mov    %r15,%rdi
call   1030 <fopen_uncompressed@plt>
test   %rax,%rax
je     1213 <charmap_open+0xb3>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf2(%rip),%rsi
mov    %r14,%rdi
mov    $0x5,%edx
call   1070 <memcpy@plt>
lea    0xde3(%rip),%rsi
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fopen_uncompressed@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rm\00"
@.str.1 = [4 x i8] c".gz\00"
@.str.2 = [5 x i8] c"gzip\00"
@.str.3 = [5 x i8] c".bz2\00"
@.str.4 = [6 x i8] c"bzip2\00"
define i32* @charmap_open(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = icmp eq i64 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i64, i64* %6
%19 = sub i64 %18, 1
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 47
br label %24
24: ; preds = %16, %2
%25 = phi i1 [ true, %2 ], [ %23, %16 ]
%26 = zext i1 %25 to i32
store i32 %26, i32* %7
%27 = load i8*, i8** %5
%28 = call i64 @strlen(i8* %27)
store i64 %28, i64* %8
%29 = load i64, i64* %6
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = add i64 %29, %31
%33 = load i64, i64* %8
%34 = add i64 %32, %33
%35 = add i64 %34, 5
%36 = call i8* @alloca(i64 %35)
store i8* %36, i8** %9
%37 = load i8*, i8** %9
%38 = load i8*, i8** %4
%39 = call i8* @stpcpy(i8* %37, i8* %38)
store i8* %39, i8** %10
%40 = load i32, i32* %7
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %24
%43 = load i8*, i8** %10
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %10
store i8 47, i8* %43
br label %45
45: ; preds = %42, %24
%46 = load i8*, i8** %10
%47 = load i8*, i8** %5
%48 = call i8* @stpcpy(i8* %46, i8* %47)
store i8* %48, i8** %10
%49 = load i8*, i8** %9
%50 = call i32* @fopen(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %50, i32** %11
%51 = load i32*, i32** %11
%52 = icmp ne i32* %51, null
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = load i32*, i32** %11
store i32* %54, i32** %3
br label %74
55: ; preds = %45
%56 = load i8*, i8** %10
%57 = call i32 @memcpy(i8* %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%58 = load i8*, i8** %9
%59 = call i32* @fopen_uncompressed(i8* %58, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32* %59, i32** %11
%60 = load i32*, i32** %11
%61 = icmp ne i32* %60, null
br i1 %61, label %62, label %64
62: ; preds = %55
%63 = load i32*, i32** %11
store i32* %63, i32** %3
br label %74
64: ; preds = %55
%65 = load i8*, i8** %10
%66 = call i32 @memcpy(i8* %65, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 5)
%67 = load i8*, i8** %9
%68 = call i32* @fopen_uncompressed(i8* %67, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32* %68, i32** %11
%69 = load i32*, i32** %11
%70 = icmp ne i32* %69, null
br i1 %70, label %71, label %73
71: ; preds = %64
%72 = load i32*, i32** %11
store i32* %72, i32** %3
br label %74
73: ; preds = %64
store i32* null, i32** %3
br label %74
74: ; preds = %73, %71, %62, %53
%75 = load i32*, i32** %3
ret i32* %75
}

LLVM IR (-O2):
@.str = [3 x i8] c"rm\00"
@.str.1 = [4 x i8] c".gz\00"
@.str.2 = [5 x i8] c"gzip\00"
@.str.3 = [5 x i8] c".bz2\00"
@.str.4 = [6 x i8] c"bzip2\00"
define i32* @charmap_open(i8* nocapture %0, i8* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = add i64 %3, -1
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp ne i8 %8, 47
br label %10
10: ; preds = %5, %2
%11 = phi i1 [ true, %2 ], [ %9, %5 ]
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%13 = zext i1 %11 to i64
%14 = add i64 %3, 5
%15 = add i64 %14, %13
%16 = add i64 %15, %12
%17 = tail call i8* @alloca(i64 %16)
%18 = tail call i8* @stpcpy(i8* %17, i8* %0)
br i1 %11, label %19, label %21
19: ; preds = %10
%20 = getelementptr inbounds i8, i8* %18, i64 1
store i8 47, i8* %18
br label %21
21: ; preds = %19, %10
%22 = phi i8* [ %20, %19 ], [ %18, %10 ]
%23 = tail call i8* @stpcpy(i8* %22, i8* %1)
%24 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %33
26: ; preds = %21
%27 = tail call i32 @memcpy(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%28 = tail call i32* @fopen_uncompressed(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = tail call i32 @memcpy(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 5)
%32 = tail call i32* @fopen_uncompressed(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %30, %26, %21
%34 = phi i32* [ %24, %21 ], [ %28, %26 ], [ %32, %30 ]
ret i32* %34
}