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

/* Variables and functions */
int /*<<< orphan*/ ENOENT;
unsigned char *__map_file(char const *, size_t *);
int be32toh(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ munmap(void *, size_t);
nl_catd do_catopen(const char *name) {
  size_t size;
  const unsigned char *map = __map_file(name, &size);
  if (!map || be32toh(*(uint32_t *)(map)) != 0xff88ff89 ||
      20 + be32toh(*(uint32_t *)(map + 8)) != size) {
    if (map)
      munmap((void *)map, size);
    errno = ENOENT;
    return (nl_catd)-1;
  }
  return (nl_catd)map;
}


x86 assembly (clang, -O0):
<do_catopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <__map_file@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1189 <do_catopen+0x59>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <be32toh@plt>
cmp    $0xff88ff89,%eax
jne    1189 <do_catopen+0x59>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edi
call   1040 <be32toh@plt>
add    $0x14,%eax
cltq
cmp    -0x18(%rbp),%rax
je     11bf <do_catopen+0x8f>
cmpq   $0x0,-0x20(%rbp)
je     11a1 <do_catopen+0x71>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <munmap@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11c6 <do_catopen+0x96>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_catopen>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1030 <__map_file@plt>
test   %rax,%rax
je     117b <do_catopen+0x4b>
mov    %rax,%rbx
mov    (%rax),%edi
call   1040 <be32toh@plt>
cmp    $0xff88ff89,%eax
jne    116e <do_catopen+0x3e>
mov    0x8(%rbx),%edi
call   1040 <be32toh@plt>
add    $0x14,%eax
cltq
mov    0x8(%rsp),%rsi
cmp    %rax,%rsi
jne    1173 <do_catopen+0x43>
jmp    1192 <do_catopen+0x62>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <munmap@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e55(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@ENOENT = global i32 0
@errno = global i32 0
define i32 @do_catopen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i8* @__map_file(i8* %6, i64* %4)
store i8* %7, i8** %5
%8 = load i8*, i8** %5
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %26
10: ; preds = %1
%11 = load i8*, i8** %5
%12 = bitcast i8* %11 to i32*
%13 = load i32, i32* %12
%14 = call i32 @be32toh(i32 %13)
%15 = icmp ne i32 %14, -7798903
br i1 %15, label %26, label %16
16: ; preds = %10
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 8
%19 = bitcast i8* %18 to i32*
%20 = load i32, i32* %19
%21 = call i32 @be32toh(i32 %20)
%22 = add nsw i32 20, %21
%23 = sext i32 %22 to i64
%24 = load i64, i64* %4
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %16, %10, %1
%27 = load i8*, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = load i64, i64* %4
%32 = call i32 @munmap(i8* %30, i64 %31)
br label %33
33: ; preds = %29, %26
%34 = load i32, i32* @ENOENT
store i32 %34, i32* @errno
store i32 -1, i32* %2
br label %38
35: ; preds = %16
%36 = load i8*, i8** %5
%37 = ptrtoint i8* %36 to i32
store i32 %37, i32* %2
br label %38
38: ; preds = %35, %33
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
@ENOENT = global i32 0
@errno = global i32 0
define i32 @do_catopen(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = call i8* @__map_file(i8* %0, i64* %2)
%5 = icmp eq i8* %4, null
br i1 %5, label %25, label %6
6: ; preds = %1
%7 = bitcast i8* %4 to i32*
%8 = load i32, i32* %7
%9 = call i32 @be32toh(i32 %8)
%10 = icmp eq i32 %9, -7798903
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = load i64, i64* %2
br label %22
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %4, i64 8
%15 = bitcast i8* %14 to i32*
%16 = load i32, i32* %15
%17 = call i32 @be32toh(i32 %16)
%18 = add nsw i32 %17, 20
%19 = sext i32 %18 to i64
%20 = load i64, i64* %2
%21 = icmp eq i64 %20, %19
br i1 %21, label %27, label %22
22: ; preds = %11, %13
%23 = phi i64 [ %12, %11 ], [ %20, %13 ]
%24 = call i32 @munmap(i8* %4, i64 %23)
br label %25
25: ; preds = %1, %22
%26 = load i32, i32* @ENOENT
store i32 %26, i32* @errno
br label %30
27: ; preds = %13
%28 = ptrtoint i8* %4 to i64
%29 = trunc i64 %28 to i32
br label %30
30: ; preds = %27, %25
%31 = phi i32 [ -1, %25 ], [ %29, %27 ]
ret i32 %31
}