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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, unsigned long long *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *strrchr(char *, char);
unsigned long long getsize(char *device) {
  int k = strlen(device);
  char path[k + 20];
  strcpy(path, "/sys/block/");
  char *devpos = strrchr(device, '/');
  if (!devpos || strlen(devpos) < 5) {
    printf("'%s' is not a device path\n", device);
    return 0;
  }
  devpos++;
  strncat(path, devpos, 3);
  strcat(path, "/");
  strcat(path, devpos);
  strcat(path, "/");
  strcat(path, "size");
  FILE *f = fopen(path, "r");
  if (!f) {
    printf("cannot open '%s'\n", path);
    return 0;
  }
  unsigned long long rez;
  int sw = fscanf(f, "%llu", &rez);
  fclose(f);
  if (sw != 1) {
    printf("unknown file format for '%s'\n", path);
    return 0;
  }
  return rez;
}


x86 assembly (clang, -O0):
<getsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x14,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
lea    0xe28(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120c <getsize+0x7c>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x5,%eax
jge    1230 <getsize+0xa0>
mov    -0x10(%rbp),%rsi
lea    0xdf5(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    132b <getsize+0x19b>
mov    -0x58(%rbp),%rdi
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    $0x3,%edx
call   1090 <strncat@plt>
mov    -0x58(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x58(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x58(%rbp),%rdi
lea    0xd94(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x58(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x58(%rbp),%rdi
lea    0xd96(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12ce <getsize+0x13e>
mov    -0x58(%rbp),%rsi
lea    0xd79(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    132b <getsize+0x19b>
mov    -0x40(%rbp),%rdi
lea    0xd67(%rip),%rsi
lea    -0x48(%rbp),%rdx
call   1080 <fscanf@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x1,-0x4c(%rbp)
je     131c <getsize+0x18c>
mov    -0x58(%rbp),%rsi
lea    0xd42(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    132b <getsize+0x19b>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsize>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1050 <strlen@plt>
add    $0x14,%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe43(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     11e7 <getsize+0x57>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x4,%eax
jg     1203 <getsize+0x73>
lea    0xe1e(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x1,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1090 <strncat@plt>
lea    0xdec(%rip),%r14
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <strcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <strcat@plt>
lea    0xde1(%rip),%rsi
mov    %r15,%rdi
call   10b0 <strcat@plt>
lea    0xdd7(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1292 <getsize+0x102>
mov    %rax,%rbx
lea    0xdd4(%rip),%rsi
lea    -0x20(%rbp),%rdx
mov    %rax,%rdi
call   1080 <fscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
cmp    $0x1,%r14d
jne    12a1 <getsize+0x111>
mov    -0x20(%rbp),%rax
jmp    11f8 <getsize+0x68>
lea    0xd95(%rip),%rdi
mov    %r15,%rsi
jmp    11f1 <getsize+0x61>
lea    0xd9d(%rip),%rdi
mov    %r15,%rsi
jmp    11f1 <getsize+0x61>

LLVM IR (-O0):
@.str = [12 x i8] c"/sys/block/\00"
@.str.1 = [27 x i8] c"'%s' is not a device path\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [5 x i8] c"size\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [18 x i8] c"cannot open '%s'\0A\00"
@.str.6 = [5 x i8] c"%llu\00"
@.str.7 = [30 x i8] c"unknown file format for '%s'\0A\00"
define i64 @getsize(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 20
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %5
%18 = alloca i8, i64 %16
store i64 %16, i64* %6
%19 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %3
%21 = call i8* @strrchr(i8* %20, i8 signext 47)
store i8* %21, i8** %7
%22 = load i8*, i8** %7
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %28
24: ; preds = %1
%25 = load i8*, i8** %7
%26 = call i32 @strlen(i8* %25)
%27 = icmp slt i32 %26, 5
br i1 %27, label %28, label %31
28: ; preds = %24, %1
%29 = load i8*, i8** %3
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %29)
store i64 0, i64* %2
store i32 1, i32* %8
br label %57
31: ; preds = %24
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = call i32 @strncat(i8* %18, i8* %34, i32 3)
%36 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = load i8*, i8** %7
%38 = call i32 @strcat(i8* %18, i8* %37)
%39 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @strcat(i8* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%41 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %41, i32** %9
%42 = load i32*, i32** %9
%43 = icmp ne i32* %42, null
br i1 %43, label %46, label %44
44: ; preds = %31
%45 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %18)
store i64 0, i64* %2
store i32 1, i32* %8
br label %57
46: ; preds = %31
%47 = load i32*, i32** %9
%48 = call i32 @fscanf(i32* %47, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64* %10)
store i32 %48, i32* %11
%49 = load i32*, i32** %9
%50 = call i32 @fclose(i32* %49)
%51 = load i32, i32* %11
%52 = icmp ne i32 %51, 1
br i1 %52, label %53, label %55
53: ; preds = %46
%54 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %18)
store i64 0, i64* %2
store i32 1, i32* %8
br label %57
55: ; preds = %46
%56 = load i64, i64* %10
store i64 %56, i64* %2
store i32 1, i32* %8
br label %57
57: ; preds = %55, %53, %44, %28
%58 = load i8*, i8** %5
%59 = load i64, i64* %2
ret i64 %59
}

LLVM IR (-O2):
@.str = [12 x i8] c"/sys/block/\00"
@.str.1 = [27 x i8] c"'%s' is not a device path\0A\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [5 x i8] c"size\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [18 x i8] c"cannot open '%s'\0A\00"
@.str.6 = [5 x i8] c"%llu\00"
@.str.7 = [30 x i8] c"unknown file format for '%s'\0A\00"
define i64 @getsize(i8* %0) {
%2 = alloca i64
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 20
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 @strlen(i8* %8)
%12 = icmp slt i32 %11, 5
br i1 %12, label %13, label %15
13: ; preds = %10, %1
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %37
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %8, i64 1
%17 = call i32 @strncat(i8* %6, i8* %16, i32 3)
%18 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @strcat(i8* %6, i8* %16)
%20 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %6)
br label %37
26: ; preds = %15
%27 = bitcast i64* %2 to i8*
%28 = call i32 @fscanf(i32* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64* %2)
%29 = call i32 @fclose(i32* %22)
%30 = icmp eq i32 %28, 1
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.7, i64 0, i64 0), i8* %6)
br label %35
33: ; preds = %26
%34 = load i64, i64* %2
br label %35
35: ; preds = %33, %31
%36 = phi i64 [ 0, %31 ], [ %34, %33 ]
br label %37
37: ; preds = %24, %35, %13
%38 = phi i64 [ 0, %13 ], [ %36, %35 ], [ 0, %24 ]
ret i64 %38
}