#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*/ buf;
typedef int /*<<< orphan*/ args;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ HDIO_DRIVE_CMD;
int /*<<< orphan*/ NAME_MAX;
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ atoll(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *hdopen(char *, char const *);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char **);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int open(char *, int);
int snprintf(char *, int, char *, char const *);
char *strstrip(char *);
int flush_cache_ext(const char *device) {
  unsigned char args[4 + 512];
  unsigned short *id = (unsigned short *)(&args[4]);
  char buf[NAME_MAX + 1], *ptr;
  int fd = -1, ret = 0;
  FILE *fp;
  fp = hdopen("/sys/block"
              "/%s/size",
              device);
  if ((long)fp <= 0) {
    if ((long)fp < 0)
      return -1;
    goto out;
  }
  ptr = fgets(buf, sizeof(buf), fp);
  fclose(fp);
  if (ptr == (char *)0)
    goto out;
  ptr = strstrip(buf);
  if (*ptr == '\0')
    goto out;
  if ((size_t)atoll(buf) < (1 << 28))
    goto out;
  ret = snprintf(buf, sizeof(buf),
                 "/dev"
                 "/%s",
                 device);
  if ((ret >= (int)sizeof(buf)) || (ret < 0))
    return -1;
  if ((fd = open(buf, O_RDONLY | O_NONBLOCK)) < 0)
    goto out;
  memset(&args[0], 0, sizeof(args));
  args[0] = 0xEC;
  args[3] = 1;
  if (ioctl(fd, HDIO_DRIVE_CMD, &args))
    goto out;
  if ((id[83] & 0xE000) == 0x6000)
    ret = 1;
out:
  if (fd >= 0)
    close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<flush_cache_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x21c(%rbp),%rax
mov    %rax,-0x228(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x268(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x230(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x260(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x238(%rbp)
movl   $0xffffffff,-0x244(%rbp)
ff ff ff
movl   $0x0,-0x248(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xde5(%rip),%rdi
call   10c0 <hdopen@plt>
mov    %rax,-0x250(%rbp)
mov    -0x250(%rbp),%rax
cmp    $0x0,%rax
jg     1264 <flush_cache_ext+0xc4>
mov    -0x250(%rbp),%rax
cmp    $0x0,%rax
jge    125f <flush_cache_ext+0xbf>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    1421 <flush_cache_ext+0x281>
jmp    13f6 <flush_cache_ext+0x256>
mov    -0x260(%rbp),%rdi
mov    -0x268(%rbp),%rax
mov    %eax,%esi
mov    -0x250(%rbp),%rdx
call   1080 <fgets@plt>
mov    %rax,-0x240(%rbp)
mov    -0x250(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x240(%rbp)
jne    12a6 <flush_cache_ext+0x106>
jmp    13f6 <flush_cache_ext+0x256>
mov    -0x260(%rbp),%rdi
call   10a0 <strstrip@plt>
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12d1 <flush_cache_ext+0x131>
jmp    13f6 <flush_cache_ext+0x256>
mov    -0x260(%rbp),%rdi
call   1090 <atoll@plt>
cmp    $0x10000000,%rax
jae    12ee <flush_cache_ext+0x14e>
jmp    13f6 <flush_cache_ext+0x256>
mov    -0x268(%rbp),%rax
mov    -0x260(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xd0a(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x268(%rbp),%rcx
mov    %eax,-0x248(%rbp)
mov    -0x248(%rbp),%eax
cmp    %ecx,%eax
jge    1336 <flush_cache_ext+0x196>
cmpl   $0x0,-0x248(%rbp)
jge    134c <flush_cache_ext+0x1ac>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    1421 <flush_cache_ext+0x281>
mov    -0x260(%rbp),%rdi
mov    0x2c86(%rip),%rax
mov    (%rax),%esi
mov    0x2c85(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x244(%rbp)
cmp    $0x0,%eax
jge    137e <flush_cache_ext+0x1de>
jmp    13f6 <flush_cache_ext+0x256>
lea    -0x220(%rbp),%rdi
xor    %esi,%esi
mov    $0x204,%edx
call   1050 <memset@plt>
movb   $0xec,-0x220(%rbp)
movb   $0x1,-0x21d(%rbp)
mov    -0x244(%rbp),%edi
mov    0x2c2c(%rip),%rax
mov    (%rax),%esi
lea    -0x220(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     13c9 <flush_cache_ext+0x229>
jmp    13f6 <flush_cache_ext+0x256>
mov    -0x228(%rbp),%rax
movzwl 0xa6(%rax),%eax
and    $0xe000,%eax
cmp    $0x6000,%eax
jne    13f1 <flush_cache_ext+0x251>
movl   $0x1,-0x248(%rbp)
jmp    13f6 <flush_cache_ext+0x256>
cmpl   $0x0,-0x244(%rbp)
jl     140e <flush_cache_ext+0x26e>
mov    -0x244(%rbp),%edi
call   1070 <close@plt>
mov    -0x248(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
mov    -0x230(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flush_cache_ext>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,%r15
mov    %rsp,%r14
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
lea    0x1(%rax),%r12d
mov    %rsp,%r13
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe1c(%rip),%rdi
mov    %r15,%rsi
call   10c0 <hdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jg     120a <flush_cache_ext+0x6a>
xor    %eax,%eax
mov    $0xffffffff,%r15d
test   %rbx,%rbx
jns    1327 <flush_cache_ext+0x187>
jmp    132a <flush_cache_ext+0x18a>
mov    %r14,-0x30(%rbp)
mov    %r13,%rdi
mov    %r12d,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
test   %r14,%r14
je     131b <flush_cache_ext+0x17b>
mov    %r13,%rdi
call   10a0 <strstrip@plt>
cmpb   $0x0,(%rax)
je     1321 <flush_cache_ext+0x181>
mov    %r13,%rdi
call   1090 <atoll@plt>
cmp    $0x10000000,%rax
mov    -0x30(%rbp),%r14
mov    $0x0,%eax
jb     1327 <flush_cache_ext+0x187>
lea    0xdac(%rip),%rdx
mov    %r13,%rdi
mov    %r12d,%esi
mov    %r15,%rcx
call   1040 <snprintf@plt>
mov    $0xffffffff,%r15d
cmp    -0x38(%rbp),%eax
jg     132a <flush_cache_ext+0x18a>
test   %eax,%eax
js     132a <flush_cache_ext+0x18a>
mov    %eax,%r12d
mov    0x2d4a(%rip),%rax
mov    0x2d4b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r13,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     133f <flush_cache_ext+0x19f>
mov    %eax,%ebx
lea    -0x240(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x204,%edx
call   1050 <memset@plt>
movb   $0xec,-0x240(%rbp)
movb   $0x1,-0x23d(%rbp)
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
mov    -0x30(%rbp),%r14
call   1060 <ioctl@plt>
movzwl -0x196(%rbp),%ecx
and    $0xe000,%ecx
cmp    $0x6000,%ecx
mov    $0x1,%r15d
cmovne %r12d,%r15d
test   %rax,%rax
cmovne %r12d,%r15d
mov    %ebx,%edi
call   1070 <close@plt>
jmp    132a <flush_cache_ext+0x18a>
mov    -0x30(%rbp),%r14
jmp    1327 <flush_cache_ext+0x187>
mov    -0x30(%rbp),%r14
xor    %eax,%eax
mov    %eax,%r15d
mov    %r14,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12d,%eax
jmp    1327 <flush_cache_ext+0x187>

LLVM IR (-O0):
@NAME_MAX = global i32 0
@.str = [19 x i8] c"/sys/block/%s/size\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@HDIO_DRIVE_CMD = global i32 0
define i32 @flush_cache_ext(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [516 x i8]
%5 = alloca i16*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %3
%13 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 4
%14 = bitcast i8* %13 to i16*
store i16* %14, i16** %5
%15 = load i32, i32* @NAME_MAX
%16 = add nsw i32 %15, 1
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %6
%19 = alloca i8, i64 %17
store i64 %17, i64* %7
store i32 -1, i32* %9
store i32 0, i32* %10
%20 = load i8*, i8** %3
%21 = call i32* @hdopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32* %21, i32** %11
%22 = load i32*, i32** %11
%23 = ptrtoint i32* %22 to i64
%24 = icmp sle i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %1
%26 = load i32*, i32** %11
%27 = ptrtoint i32* %26 to i64
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %2
store i32 1, i32* %12
br label %97
30: ; preds = %25
br label %89
31: ; preds = %1
%32 = trunc i64 %17 to i32
%33 = load i32*, i32** %11
%34 = call i8* @fgets(i8* %19, i32 %32, i32* %33)
store i8* %34, i8** %8
%35 = load i32*, i32** %11
%36 = call i32 @fclose(i32* %35)
%37 = load i8*, i8** %8
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %40
39: ; preds = %31
br label %89
40: ; preds = %31
%41 = call i8* @strstrip(i8* %19)
store i8* %41, i8** %8
%42 = load i8*, i8** %8
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
br label %89
47: ; preds = %40
%48 = call i64 @atoll(i8* %19)
%49 = icmp ult i64 %48, 268435456
br i1 %49, label %50, label %51
50: ; preds = %47
br label %89
51: ; preds = %47
%52 = trunc i64 %17 to i32
%53 = load i8*, i8** %3
%54 = call i32 @snprintf(i8* %19, i32 %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %53)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = trunc i64 %17 to i32
%57 = icmp sge i32 %55, %56
br i1 %57, label %61, label %58
58: ; preds = %51
%59 = load i32, i32* %10
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %58, %51
store i32 -1, i32* %2
store i32 1, i32* %12
br label %97
62: ; preds = %58
%63 = load i32, i32* @O_RDONLY
%64 = load i32, i32* @O_NONBLOCK
%65 = or i32 %63, %64
%66 = call i32 @open(i8* %19, i32 %65)
store i32 %66, i32* %9
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %62
br label %89
69: ; preds = %62
%70 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 0
%71 = call i32 @memset(i8* %70, i32 0, i32 516)
%72 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 0
store i8 -20, i8* %72
%73 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 3
store i8 1, i8* %73
%74 = load i32, i32* %9
%75 = load i32, i32* @HDIO_DRIVE_CMD
%76 = bitcast [516 x i8]* %4 to i8**
%77 = call i64 @ioctl(i32 %74, i32 %75, i8** %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %69
br label %89
80: ; preds = %69
%81 = load i16*, i16** %5
%82 = getelementptr inbounds i16, i16* %81, i64 83
%83 = load i16, i16* %82
%84 = zext i16 %83 to i32
%85 = and i32 %84, 57344
%86 = icmp eq i32 %85, 24576
br i1 %86, label %87, label %88
87: ; preds = %80
store i32 1, i32* %10
br label %88
88: ; preds = %87, %80
br label %89
89: ; preds = %88, %79, %68, %50, %46, %39, %30
%90 = load i32, i32* %9
%91 = icmp sge i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %89
%93 = load i32, i32* %9
%94 = call i32 @close(i32 %93)
br label %95
95: ; preds = %92, %89
%96 = load i32, i32* %10
store i32 %96, i32* %2
store i32 1, i32* %12
br label %97
97: ; preds = %95, %61, %29
%98 = load i8*, i8** %6
%99 = load i32, i32* %2
ret i32 %99
}

LLVM IR (-O2):
@NAME_MAX = global i32 0
@.str = [19 x i8] c"/sys/block/%s/size\00"
@.str.1 = [8 x i8] c"/dev/%s\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@HDIO_DRIVE_CMD = global i32 0
define i32 @flush_cache_ext(i8* %0) {
%2 = alloca [516 x i8]
%3 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @NAME_MAX
%5 = add nsw i32 %4, 1
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = tail call i32* @hdopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = icmp slt i32* %9, inttoptr (i64 1 to i32*)
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = icmp slt i32* %9, null
br i1 %12, label %52, label %50
13: ; preds = %1
%14 = call i8* @fgets(i8* %8, i32 %5, i32* %9)
%15 = call i32 @fclose(i32* %9)
%16 = icmp eq i8* %14, null
br i1 %16, label %50, label %17
17: ; preds = %13
%18 = call i8* @strstrip(i8* %8)
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %50, label %21
21: ; preds = %17
%22 = call i64 @atoll(i8* %8)
%23 = icmp ult i64 %22, 268435456
br i1 %23, label %50, label %24
24: ; preds = %21
%25 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%26 = icmp sgt i32 %25, %4
%27 = icmp slt i32 %25, 0
%28 = or i1 %26, %27
br i1 %28, label %52, label %29
29: ; preds = %24
%30 = load i32, i32* @O_RDONLY
%31 = load i32, i32* @O_NONBLOCK
%32 = or i32 %31, %30
%33 = call i32 @open(i8* %8, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %50, label %35
35: ; preds = %29
%36 = call i32 @memset(i8* %3, i32 0, i32 516)
store i8 -20, i8* %3
%37 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 3
store i8 1, i8* %37
%38 = load i32, i32* @HDIO_DRIVE_CMD
%39 = bitcast [516 x i8]* %2 to i8**
%40 = call i64 @ioctl(i32 %33, i32 %38, i8** %39)
%41 = icmp eq i64 %40, 0
%42 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 170
%43 = bitcast i8* %42 to i16*
%44 = load i16, i16* %43
%45 = and i16 %44, -8192
%46 = icmp eq i16 %45, 24576
%47 = select i1 %41, i1 %46, i1 false
%48 = select i1 %47, i32 1, i32 %25
%49 = call i32 @close(i32 %33)
br label %52
50: ; preds = %11, %13, %17, %21, %29
%51 = phi i32 [ %25, %29 ], [ 0, %21 ], [ 0, %17 ], [ 0, %13 ], [ 0, %11 ]
br label %52
52: ; preds = %50, %35, %24, %11
%53 = phi i32 [ -1, %11 ], [ -1, %24 ], [ %48, %35 ], [ %51, %50 ]
ret i32 %53
}