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

/* Variables and functions */
int /*<<< orphan*/ BLKBSZGET;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, size_t *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t reqbufalign(char *devpath) {
  size_t align;
  int fd = open(devpath, 0);
  if (fd == -1) {
    perror("unable to open device");
    return 0;
  }
  if (ioctl(fd, BLKBSZGET, &align) != 0) {
    perror("unable to get correct alignment");
  }
  close(fd);
  return align;
}


x86 assembly (clang, -O0):
<reqbufalign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    117d <reqbufalign+0x3d>
lea    0xe95(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b8 <reqbufalign+0x78>
mov    -0x1c(%rbp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11a8 <reqbufalign+0x68>
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reqbufalign>:
push   %rbx
sub    $0x10,%rsp
xor    %esi,%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     117f <reqbufalign+0x3f>
mov    %eax,%ebx
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    118f <reqbufalign+0x4f>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe7a(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
jmp    1179 <reqbufalign+0x39>
lea    0xe80(%rip),%rdi
call   1060 <perror@plt>
jmp    116d <reqbufalign+0x2d>

LLVM IR (-O0):
@.str = [22 x i8] c"unable to open device\00"
@BLKBSZGET = global i32 0
@.str.1 = [32 x i8] c"unable to get correct alignment\00"
define i64 @reqbufalign(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @open(i8* %6, i32 0)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i64 0, i64* %2
br label %23
12: ; preds = %1
%13 = load i32, i32* %5
%14 = load i32, i32* @BLKBSZGET
%15 = call i64 @ioctl(i32 %13, i32 %14, i64* %4)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
%22 = load i64, i64* %4
store i64 %22, i64* %2
br label %23
23: ; preds = %19, %10
%24 = load i64, i64* %2
ret i64 %24
}

LLVM IR (-O2):
@.str = [22 x i8] c"unable to open device\00"
@BLKBSZGET = global i32 0
@.str.1 = [32 x i8] c"unable to get correct alignment\00"
define i64 @reqbufalign(i8* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i32 @open(i8* %0, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %1
%9 = load i32, i32* @BLKBSZGET
%10 = call i64 @ioctl(i32 %4, i32 %9, i64* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = call i32 @close(i32 %4)
%16 = load i64, i64* %2
br label %17
17: ; preds = %14, %6
%18 = phi i64 [ 0, %6 ], [ %16, %14 ]
ret i64 %18
}