#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*/ off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int errno;
scalar_t__ ftruncate(int, int /*<<< orphan*/);
int memcmp(void *, char *, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int fdcmp(int fd, char *buf, off_t len) {
  void *mem;
  int r;
  if (ftruncate(fd, len) < 0) {
    perror("ftruncate");
    return -errno;
  }
  mem = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
  if (mem == MAP_FAILED) {
    perror("mmap");
    return -errno;
  }
  r = memcmp(mem, buf, len);
  munmap(mem, len);
  return r;
}


x86 assembly (clang, -O0):
<fdcmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <ftruncate@plt>
cmp    $0x0,%rax
jge    1196 <fdcmp+0x46>
lea    0xe82(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
mov    0x2e4c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <fdcmp+0xc7>
mov    -0x14(%rbp),%esi
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
jne    11f2 <fdcmp+0xa2>
lea    0xe30(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
mov    0x2df0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <fdcmp+0xc7>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <memcmp@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <munmap@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdcmp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%esi
call   1040 <ftruncate@plt>
test   %rax,%rax
js     11c5 <fdcmp+0x75>
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2e30(%rip),%rax
cmp    (%rax),%rbp
je     11dd <fdcmp+0x8d>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <memcmp@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
mov    %ebx,%esi
call   1060 <munmap@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e00(%rip),%rax
xor    %r15d,%r15d
jmp    11f0 <fdcmp+0xa0>
lea    0xe26(%rip),%rdi
call   1070 <perror@plt>
mov    0x2de8(%rip),%rax
sub    (%rax),%r15d
mov    %r15d,%eax
jmp    11ba <fdcmp+0x6a>

LLVM IR (-O0):
@.str = [10 x i8] c"ftruncate\00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define i32 @fdcmp(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i32, i32* %7
%12 = call i64 @ftruncate(i32 %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @errno
%17 = sub nsw i32 0, %16
store i32 %17, i32* %4
br label %40
18: ; preds = %3
%19 = load i32, i32* %7
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_PRIVATE
%22 = load i32, i32* %5
%23 = call i8* @mmap(i32* null, i32 %19, i32 %20, i32 %21, i32 %22, i32 0)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %31
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @errno
%30 = sub nsw i32 0, %29
store i32 %30, i32* %4
br label %40
31: ; preds = %18
%32 = load i8*, i8** %8
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = call i32 @memcmp(i8* %32, i8* %33, i32 %34)
store i32 %35, i32* %9
%36 = load i8*, i8** %8
%37 = load i32, i32* %7
%38 = call i32 @munmap(i8* %36, i32 %37)
%39 = load i32, i32* %9
store i32 %39, i32* %4
br label %40
40: ; preds = %31, %27, %14
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
@.str = [10 x i8] c"ftruncate\00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define i32 @fdcmp(i32 %0, i8* %1, i32 %2) {
%4 = tail call i64 @ftruncate(i32 %0, i32 %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %23
10: ; preds = %3
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @MAP_PRIVATE
%13 = tail call i8* @mmap(i32* null, i32 %2, i32 %11, i32 %12, i32 %0, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %23
20: ; preds = %10
%21 = tail call i32 @memcmp(i8* %13, i8* %1, i32 %2)
%22 = tail call i32 @munmap(i8* %13, i32 %2)
br label %23
23: ; preds = %20, %16, %6
%24 = phi i32 [ %9, %6 ], [ %19, %16 ], [ %21, %20 ]
ret i32 %24
}