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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ err(int, char *, char const *);
int /*<<< orphan*/ errx(int, char *, char const *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int block_attach(const char *path, off_t *capacity_) {
  int fd = open(path, O_RDWR);
  if (fd == -1)
    err(1, "Could not open block device: %s", path);
  off_t capacity = lseek(fd, 0, SEEK_END);
  if (capacity == -1)
    err(1, "%s: Could not determine capacity", path);
  if (capacity < 512)
    errx(1,
         "%s: Backing storage must be at least 1 block (512 bytes) "
         "in size",
         path);
  *capacity_ = capacity;
  return fd;
}


x86 assembly (clang, -O0):
<block_attach>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1184 <block_attach+0x44>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe81(%rip),%rsi
call   1050 <err@plt>
mov    -0x14(%rbp),%edi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11b9 <block_attach+0x79>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe6c(%rip),%rsi
call   1050 <err@plt>
cmpl   $0x200,-0x18(%rbp)
jge    11db <block_attach+0x9b>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe6b(%rip),%rsi
call   1030 <errx@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_attach>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1176 <block_attach+0x36>
lea    0xe97(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
call   1050 <err@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1199 <block_attach+0x59>
cmp    $0x1ff,%ebx
jle    11ad <block_attach+0x6d>
jmp    11c1 <block_attach+0x81>
lea    0xe80(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
call   1050 <err@plt>
lea    0xe8d(%rip),%rsi
mov    $0x1,%edi
mov    %r15,%rdx
call   1030 <errx@plt>
mov    %ebx,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [32 x i8] c"Could not open block device: %s\00"
@SEEK_END = global i32 0
@.str.1 = [33 x i8] c"%s: Could not determine capacity\00"
@.str.2 = [65 x i8] c"%s: Backing storage must be at least 1 block (512 bytes) in size\00"
define i32 @block_attach(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 @err(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %12, %2
%16 = load i32, i32* %5
%17 = load i32, i32* @SEEK_END
%18 = call i32 @lseek(i32 %16, i32 0, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = call i32 @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %15
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 512
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %3
%29 = call i32 @errx(i32 1, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %24
%31 = load i32, i32* %6
%32 = load i32*, i32** %4
store i32 %31, i32* %32
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str = [32 x i8] c"Could not open block device: %s\00"
@SEEK_END = global i32 0
@.str.1 = [33 x i8] c"%s: Could not determine capacity\00"
@.str.2 = [65 x i8] c"%s: Backing storage must be at least 1 block (512 bytes) in size\00"
define i32 @block_attach(i8* %0, i32* nocapture writeonly %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %2
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @lseek(i32 %4, i32 0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
14: ; preds = %8
%15 = icmp slt i32 %10, 512
br i1 %15, label %16, label %18
16: ; preds = %12, %14
%17 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %14
store i32 %10, i32* %1
ret i32 %4
}