#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 (0 == (long)fp || -1 == (long)fp) {
    if (-1 == (long)fp)
      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),%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1250 <flush_cache_ext+0xb0>
mov    -0x250(%rbp),%rcx
mov    $0xffffffffffffffff,%rax
cmp    %rcx,%rax
jne    1282 <flush_cache_ext+0xe2>
mov    -0x250(%rbp),%rcx
mov    $0xffffffffffffffff,%rax
cmp    %rcx,%rax
jne    127d <flush_cache_ext+0xdd>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    143f <flush_cache_ext+0x29f>
jmp    1414 <flush_cache_ext+0x274>
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    12c4 <flush_cache_ext+0x124>
jmp    1414 <flush_cache_ext+0x274>
mov    -0x260(%rbp),%rdi
call   10a0 <strstrip@plt>
mov    %rax,-0x240(%rbp)
mov    -0x240(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    12ef <flush_cache_ext+0x14f>
jmp    1414 <flush_cache_ext+0x274>
mov    -0x260(%rbp),%rdi
call   1090 <atoll@plt>
cmp    $0x10000000,%rax
jae    130c <flush_cache_ext+0x16c>
jmp    1414 <flush_cache_ext+0x274>
mov    -0x268(%rbp),%rax
mov    -0x260(%rbp),%rdi
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xcec(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x268(%rbp),%rcx
mov    %eax,-0x248(%rbp)
mov    -0x248(%rbp),%eax
cmp    %ecx,%eax
jge    1354 <flush_cache_ext+0x1b4>
cmpl   $0x0,-0x248(%rbp)
jge    136a <flush_cache_ext+0x1ca>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x254(%rbp)
jmp    143f <flush_cache_ext+0x29f>
mov    -0x260(%rbp),%rdi
mov    0x2c68(%rip),%rax
mov    (%rax),%esi
mov    0x2c67(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x244(%rbp)
cmp    $0x0,%eax
jge    139c <flush_cache_ext+0x1fc>
jmp    1414 <flush_cache_ext+0x274>
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    0x2c0e(%rip),%rax
mov    (%rax),%esi
lea    -0x220(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
je     13e7 <flush_cache_ext+0x247>
jmp    1414 <flush_cache_ext+0x274>
mov    -0x228(%rbp),%rax
movzwl 0xa6(%rax),%eax
and    $0xe000,%eax
cmp    $0x6000,%eax
jne    140f <flush_cache_ext+0x26f>
movl   $0x1,-0x248(%rbp)
jmp    1414 <flush_cache_ext+0x274>
cmpl   $0x0,-0x244(%rbp)
jl     142c <flush_cache_ext+0x28c>
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    $0x228,%rsp
mov    %rdi,%rsi
mov    %rsp,-0x40(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%r12d
lea    0x1(%r12),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe1e(%rip),%rdi
mov    %rsi,-0x38(%rbp)
call   10c0 <hdopen@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffffffffffff,%rax
je     130f <flush_cache_ext+0x16f>
mov    %rax,%r14
xor    %eax,%eax
test   %r14,%r14
je     130c <flush_cache_ext+0x16c>
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %r14,%rdx
call   1080 <fgets@plt>
mov    %rax,-0x30(%rbp)
mov    %r14,%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
je     130a <flush_cache_ext+0x16a>
mov    %rbx,%rdi
call   10a0 <strstrip@plt>
cmpb   $0x0,(%rax)
je     130a <flush_cache_ext+0x16a>
mov    %rbx,%rdi
call   1090 <atoll@plt>
cmp    $0x10000000,%rax
mov    $0x0,%eax
jb     130c <flush_cache_ext+0x16c>
lea    0xdb4(%rip),%rdx
mov    %rbx,%rdi
mov    %r13d,%esi
mov    -0x38(%rbp),%rcx
call   1040 <snprintf@plt>
cmp    %r12d,%eax
jg     130f <flush_cache_ext+0x16f>
test   %eax,%eax
js     130f <flush_cache_ext+0x16f>
mov    %eax,%r12d
mov    0x2d57(%rip),%rax
mov    0x2d58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1325 <flush_cache_ext+0x185>
mov    %eax,%ebx
lea    -0x250(%rbp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x204,%edx
call   1050 <memset@plt>
movb   $0xec,-0x250(%rbp)
movb   $0x1,-0x24d(%rbp)
mov    0x2d07(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1060 <ioctl@plt>
movzwl -0x1a6(%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    130f <flush_cache_ext+0x16f>
xor    %eax,%eax
mov    %eax,%r15d
mov    -0x40(%rbp),%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    130c <flush_cache_ext+0x16c>

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 eq i64 0, %23
br i1 %24, label %29, label %25
25: ; preds = %1
%26 = load i32*, i32** %11
%27 = ptrtoint i32* %26 to i64
%28 = icmp eq i64 -1, %27
br i1 %28, label %29, label %35
29: ; preds = %25, %1
%30 = load i32*, i32** %11
%31 = ptrtoint i32* %30 to i64
%32 = icmp eq i64 -1, %31
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -1, i32* %2
store i32 1, i32* %12
br label %101
34: ; preds = %29
br label %93
35: ; preds = %25
%36 = trunc i64 %17 to i32
%37 = load i32*, i32** %11
%38 = call i8* @fgets(i8* %19, i32 %36, i32* %37)
store i8* %38, i8** %8
%39 = load i32*, i32** %11
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %8
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %44
43: ; preds = %35
br label %93
44: ; preds = %35
%45 = call i8* @strstrip(i8* %19)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %44
br label %93
51: ; preds = %44
%52 = call i64 @atoll(i8* %19)
%53 = icmp ult i64 %52, 268435456
br i1 %53, label %54, label %55
54: ; preds = %51
br label %93
55: ; preds = %51
%56 = trunc i64 %17 to i32
%57 = load i8*, i8** %3
%58 = call i32 @snprintf(i8* %19, i32 %56, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %57)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = trunc i64 %17 to i32
%61 = icmp sge i32 %59, %60
br i1 %61, label %65, label %62
62: ; preds = %55
%63 = load i32, i32* %10
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %62, %55
store i32 -1, i32* %2
store i32 1, i32* %12
br label %101
66: ; preds = %62
%67 = load i32, i32* @O_RDONLY
%68 = load i32, i32* @O_NONBLOCK
%69 = or i32 %67, %68
%70 = call i32 @open(i8* %19, i32 %69)
store i32 %70, i32* %9
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %66
br label %93
73: ; preds = %66
%74 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 0
%75 = call i32 @memset(i8* %74, i32 0, i32 516)
%76 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 0
store i8 -20, i8* %76
%77 = getelementptr inbounds [516 x i8], [516 x i8]* %4, i64 0, i64 3
store i8 1, i8* %77
%78 = load i32, i32* %9
%79 = load i32, i32* @HDIO_DRIVE_CMD
%80 = bitcast [516 x i8]* %4 to i8**
%81 = call i64 @ioctl(i32 %78, i32 %79, i8** %80)
%82 = icmp ne i64 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %73
br label %93
84: ; preds = %73
%85 = load i16*, i16** %5
%86 = getelementptr inbounds i16, i16* %85, i64 83
%87 = load i16, i16* %86
%88 = zext i16 %87 to i32
%89 = and i32 %88, 57344
%90 = icmp eq i32 %89, 24576
br i1 %90, label %91, label %92
91: ; preds = %84
store i32 1, i32* %10
br label %92
92: ; preds = %91, %84
br label %93
93: ; preds = %92, %83, %72, %54, %50, %43, %34
%94 = load i32, i32* %9
%95 = icmp sge i32 %94, 0
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = load i32, i32* %9
%98 = call i32 @close(i32 %97)
br label %99
99: ; preds = %96, %93
%100 = load i32, i32* %10
store i32 %100, i32* %2
store i32 1, i32* %12
br label %101
101: ; preds = %99, %65, %33
%102 = load i8*, i8** %6
%103 = load i32, i32* %2
ret i32 %103
}

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 = ptrtoint i32* %9 to i64
switch i64 %10, label %11 [
i64 -1, label %50
i64 0, label %48
]
11: ; preds = %1
%12 = call i8* @fgets(i8* %8, i32 %5, i32* %9)
%13 = call i32 @fclose(i32* %9)
%14 = icmp eq i8* %12, null
br i1 %14, label %48, label %15
15: ; preds = %11
%16 = call i8* @strstrip(i8* %8)
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %48, label %19
19: ; preds = %15
%20 = call i64 @atoll(i8* %8)
%21 = icmp ult i64 %20, 268435456
br i1 %21, label %48, label %22
22: ; preds = %19
%23 = call i32 @snprintf(i8* %8, i32 %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%24 = icmp sgt i32 %23, %4
%25 = icmp slt i32 %23, 0
%26 = or i1 %24, %25
br i1 %26, label %50, label %27
27: ; preds = %22
%28 = load i32, i32* @O_RDONLY
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %29, %28
%31 = call i32 @open(i8* %8, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %48, label %33
33: ; preds = %27
%34 = call i32 @memset(i8* %3, i32 0, i32 516)
store i8 -20, i8* %3
%35 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 3
store i8 1, i8* %35
%36 = load i32, i32* @HDIO_DRIVE_CMD
%37 = bitcast [516 x i8]* %2 to i8**
%38 = call i64 @ioctl(i32 %31, i32 %36, i8** %37)
%39 = icmp eq i64 %38, 0
%40 = getelementptr inbounds [516 x i8], [516 x i8]* %2, i64 0, i64 170
%41 = bitcast i8* %40 to i16*
%42 = load i16, i16* %41
%43 = and i16 %42, -8192
%44 = icmp eq i16 %43, 24576
%45 = select i1 %39, i1 %44, i1 false
%46 = select i1 %45, i32 1, i32 %23
%47 = call i32 @close(i32 %31)
br label %50
48: ; preds = %11, %15, %19, %27, %1
%49 = phi i32 [ 0, %1 ], [ %23, %27 ], [ 0, %19 ], [ 0, %15 ], [ 0, %11 ]
br label %50
50: ; preds = %48, %33, %22, %1
%51 = phi i32 [ -1, %1 ], [ -1, %22 ], [ %46, %33 ], [ %49, %48 ]
ret i32 %51
}