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

/* Variables and functions */
scalar_t__ dinfo_extract_dev(int /*<<< orphan*/ *, char *, int);
char *dinfo_malloc(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
char *realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int dinfo_get_dev_from_blockdev(char *blockdev, dev_t *dev) {
  FILE *dasddev;
  int offset = 0;
  char *devfile = NULL;
  char *readbuf = NULL;
  int readbuflen = 80;
  if ((devfile = dinfo_malloc(readbuflen)) == NULL)
    return -1;
  sprintf(devfile, "/sys/block/%s/dev", blockdev);
  if ((readbuf = dinfo_malloc(readbuflen)) == NULL) {
    printf("Error: Not enough memory to allocate readbuffer\n");
    return -1;
  }
  if ((dasddev = fopen(devfile, "r")) == NULL)
    return -1;
  while (fgets(readbuf + offset, 80, dasddev) &&
         readbuf[strlen(readbuf) - 1] != '\n') {
    offset += 80 - 1;
    if (offset + 80 - 1 >= readbuflen) {
      readbuf = realloc(readbuf, readbuflen + 80 - 1);
      readbuflen += 80 - 1;
    }
  }
  fclose(dasddev);
  if (dinfo_extract_dev(dev, readbuf, readbuflen) != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<dinfo_get_dev_from_blockdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x50,-0x3c(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <dinfo_malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11e0 <dinfo_get_dev_from_blockdev+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <dinfo_get_dev_from_blockdev+0x19b>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x3c(%rbp),%edi
call   1030 <dinfo_malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1222 <dinfo_get_dev_from_blockdev+0x92>
lea    0xe01(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <dinfo_get_dev_from_blockdev+0x19b>
mov    -0x30(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    124c <dinfo_get_dev_from_blockdev+0xbc>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <dinfo_get_dev_from_blockdev+0x19b>
jmp    1251 <dinfo_get_dev_from_blockdev+0xc1>
mov    -0x38(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rdx
mov    $0x50,%esi
call   1070 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x3d(%rbp)
je     12a6 <dinfo_get_dev_from_blockdev+0x116>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
test   $0x1,%al
jne    12b6 <dinfo_get_dev_from_blockdev+0x126>
jmp    12f5 <dinfo_get_dev_from_blockdev+0x165>
mov    -0x24(%rbp),%eax
add    $0x4f,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x50,%eax
sub    $0x1,%eax
cmp    -0x3c(%rbp),%eax
jl     12f0 <dinfo_get_dev_from_blockdev+0x160>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
add    $0x50,%esi
sub    $0x1,%esi
call   1080 <realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x4f,%eax
mov    %eax,-0x3c(%rbp)
jmp    1251 <dinfo_get_dev_from_blockdev+0xc1>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   10b0 <dinfo_extract_dev@plt>
cmp    $0x0,%rax
je     1324 <dinfo_get_dev_from_blockdev+0x194>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <dinfo_get_dev_from_blockdev+0x19b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dinfo_get_dev_from_blockdev>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x50,%edi
call   1030 <dinfo_malloc@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     12a1 <dinfo_get_dev_from_blockdev+0x111>
mov    %rax,%rbp
lea    0xe3f(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   10a0 <sprintf@plt>
mov    $0x50,%edi
call   1030 <dinfo_malloc@plt>
test   %rax,%rax
je     1277 <dinfo_get_dev_from_blockdev+0xe7>
mov    %rax,%rbx
lea    0xe57(%rip),%rsi
mov    %rbp,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12a1 <dinfo_get_dev_from_blockdev+0x111>
mov    %rax,%r15
mov    $0x50,%r12d
mov    %rbx,%rdi
mov    $0x50,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1285 <dinfo_get_dev_from_blockdev+0xf5>
mov    $0x50,%r12d
mov    $0x4f,%ebp
jmp    124a <dinfo_get_dev_from_blockdev+0xba>
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%rbx,%rbp,1),%rdi
mov    $0x50,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
add    $0x4f,%rbp
test   %rax,%rax
je     1285 <dinfo_get_dev_from_blockdev+0xf5>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
je     1285 <dinfo_get_dev_from_blockdev+0xf5>
lea    0x4f(%rbp),%eax
cmp    %r12d,%eax
jl     1230 <dinfo_get_dev_from_blockdev+0xa0>
add    $0x4f,%r12d
mov    %rbx,%rdi
mov    %r12d,%esi
call   1080 <realloc@plt>
mov    %rax,%rbx
jmp    1230 <dinfo_get_dev_from_blockdev+0xa0>
lea    0xd94(%rip),%rdi
call   1040 <puts@plt>
jmp    12a1 <dinfo_get_dev_from_blockdev+0x111>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   10b0 <dinfo_extract_dev@plt>
neg    %rax
sbb    %r12d,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/sys/block/%s/dev\00"
@.str.1 = [49 x i8] c"Error: Not enough memory to allocate readbuffer\0A\00"
@.str.2 = [2 x i8] c"r\00"
define i32 @dinfo_get_dev_from_blockdev(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %7
store i8* null, i8** %8
store i8* null, i8** %9
store i32 80, i32* %10
%11 = load i32, i32* %10
%12 = call i8* @dinfo_malloc(i32 %11)
store i8* %12, i8** %8
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %77
15: ; preds = %2
%16 = load i8*, i8** %8
%17 = load i8*, i8** %4
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %10
%20 = call i8* @dinfo_malloc(i32 %19)
store i8* %20, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %77
24: ; preds = %15
%25 = load i8*, i8** %8
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %6
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %3
br label %77
29: ; preds = %24
br label %30
30: ; preds = %66, %29
%31 = load i8*, i8** %9
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32*, i32** %6
%36 = call i64 @fgets(i8* %34, i32 80, i32* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %30
%39 = load i8*, i8** %9
%40 = load i8*, i8** %9
%41 = call i32 @strlen(i8* %40)
%42 = sub nsw i32 %41, 1
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %39, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 10
br label %48
48: ; preds = %38, %30
%49 = phi i1 [ false, %30 ], [ %47, %38 ]
br i1 %49, label %50, label %67
50: ; preds = %48
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 79
store i32 %52, i32* %7
%53 = load i32, i32* %7
%54 = add nsw i32 %53, 80
%55 = sub nsw i32 %54, 1
%56 = load i32, i32* %10
%57 = icmp sge i32 %55, %56
br i1 %57, label %58, label %66
58: ; preds = %50
%59 = load i8*, i8** %9
%60 = load i32, i32* %10
%61 = add nsw i32 %60, 80
%62 = sub nsw i32 %61, 1
%63 = call i8* @realloc(i8* %59, i32 %62)
store i8* %63, i8** %9
%64 = load i32, i32* %10
%65 = add nsw i32 %64, 79
store i32 %65, i32* %10
br label %66
66: ; preds = %58, %50
br label %30
67: ; preds = %48
%68 = load i32*, i32** %6
%69 = call i32 @fclose(i32* %68)
%70 = load i32*, i32** %5
%71 = load i8*, i8** %9
%72 = load i32, i32* %10
%73 = call i64 @dinfo_extract_dev(i32* %70, i8* %71, i32 %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %67
store i32 -1, i32* %3
br label %77
76: ; preds = %67
store i32 0, i32* %3
br label %77
77: ; preds = %76, %75, %28, %22, %14
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
@.str = [18 x i8] c"/sys/block/%s/dev\00"
@.str.2 = [2 x i8] c"r\00"
@str = [48 x i8] c"Error: Not enough memory to allocate readbuffer\00"
define i32 @dinfo_get_dev_from_blockdev(i8* %0, i32* %1) {
%3 = tail call i8* @dinfo_malloc(i32 80)
%4 = icmp eq i8* %3, null
br i1 %4, label %48, label %5
5: ; preds = %2
%6 = tail call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = tail call i8* @dinfo_malloc(i32 80)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %48
11: ; preds = %5
%12 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %48, label %14
14: ; preds = %11
%15 = tail call i64 @fgets(i8* %7, i32 80, i32* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %41, label %17
17: ; preds = %14, %35
%18 = phi i64 [ %28, %35 ], [ 0, %14 ]
%19 = phi i32 [ %37, %35 ], [ 80, %14 ]
%20 = phi i8* [ %36, %35 ], [ %7, %14 ]
%21 = tail call i32 @strlen(i8* %20)
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 10
br i1 %26, label %41, label %27
27: ; preds = %17
%28 = add nuw i64 %18, 79
%29 = trunc i64 %18 to i32
%30 = add i32 %29, 158
%31 = icmp slt i32 %30, %19
br i1 %31, label %35, label %32
32: ; preds = %27
%33 = add nsw i32 %19, 79
%34 = tail call i8* @realloc(i8* %20, i32 %33)
br label %35
35: ; preds = %32, %27
%36 = phi i8* [ %34, %32 ], [ %20, %27 ]
%37 = phi i32 [ %33, %32 ], [ %19, %27 ]
%38 = getelementptr inbounds i8, i8* %36, i64 %28
%39 = tail call i64 @fgets(i8* %38, i32 80, i32* %12)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %17
41: ; preds = %17, %35, %14
%42 = phi i8* [ %7, %14 ], [ %36, %35 ], [ %20, %17 ]
%43 = phi i32 [ 80, %14 ], [ %37, %35 ], [ %19, %17 ]
%44 = tail call i32 @fclose(i32* %12)
%45 = tail call i64 @dinfo_extract_dev(i32* %1, i8* %42, i32 %43)
%46 = icmp ne i64 %45, 0
%47 = sext i1 %46 to i32
br label %48
48: ; preds = %41, %11, %2, %9
%49 = phi i32 [ -1, %9 ], [ -1, %2 ], [ -1, %11 ], [ %47, %41 ]
ret i32 %49
}