#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 scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
off_t mmc_device_size(const char *devpath) {
  int fd = open(devpath, O_RDONLY);
  if (fd < 0)
    return 0;
  off_t len = lseek(fd, 0, SEEK_END);
  close(fd);
  return len < 0 ? 0 : len;
}


x86 assembly (clang, -O0):
<mmc_device_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1168 <mmc_device_size+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11ad <mmc_device_size+0x7d>
mov    -0x14(%rbp),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x20(%rbp)
jge    119d <mmc_device_size+0x6d>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11a5 <mmc_device_size+0x75>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmc_device_size>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     116f <mmc_device_size+0x3f>
mov    %eax,%ebp
mov    0x2e99(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1040 <close@plt>
test   %r14,%r14
cmovns %r14,%rbx
jmp    1171 <mmc_device_size+0x41>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
define i64 @mmc_device_size(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i64
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %1
store i64 0, i64* %2
br label %25
12: ; preds = %1
%13 = load i32, i32* %4
%14 = load i32, i32* @SEEK_END
%15 = call i64 @lseek(i32 %13, i32 0, i32 %14)
store i64 %15, i64* %5
%16 = load i32, i32* %4
%17 = call i32 @close(i32 %16)
%18 = load i64, i64* %5
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %12
br label %23
21: ; preds = %12
%22 = load i64, i64* %5
br label %23
23: ; preds = %21, %20
%24 = phi i64 [ 0, %20 ], [ %22, %21 ]
store i64 %24, i64* %2
br label %25
25: ; preds = %23, %11
%26 = load i64, i64* %2
ret i64 %26
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
define i64 @mmc_device_size(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = load i32, i32* @SEEK_END
%7 = tail call i64 @lseek(i32 %3, i32 0, i32 %6)
%8 = tail call i32 @close(i32 %3)
%9 = icmp sgt i64 %7, 0
%10 = select i1 %9, i64 %7, i64 0
br label %11
11: ; preds = %1, %5
%12 = phi i64 [ %10, %5 ], [ 0, %1 ]
ret i64 %12
}