#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 fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
unsigned long strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int get_disk_size(unsigned long *disk_size) {
  char buffer[512];
  FILE *read_fp;
  int chars_read;
  int ret = 0;
  memset(buffer, 0, 512);
  read_fp =
      popen("fdisk -l | grep \"Disk /dev/mmcblk0\" | awk '{print $7}'", "r");
  if (read_fp != NULL) {
    chars_read = fread(buffer, sizeof(char), 512 - 1, read_fp);
    if (chars_read > 0) {
      printf("fdisk result= %s\n", buffer);
      *disk_size = strtoul(buffer, NULL, 0);
      printf("%lu\n", *disk_size);
      printf("disk_size=%lu sectors,%d MB\n", *disk_size,
             (*disk_size) / (1024 * 2));
      *disk_size = *disk_size - (70 * 1024 * 2);
      printf("p4_size=%08x(%lu) sectors,%d MB\n", *disk_size, *disk_size,
             (*disk_size) / (1024 * 2));
      ret = 1;
    } else {
      ret = 0;
    }
    pclose(read_fp);
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_disk_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x220(%rbp)
lea    -0x210(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    0xe6d(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x218(%rbp)
cmpq   $0x0,-0x218(%rbp)
je     12ab <get_disk_size+0x14b>
lea    -0x210(%rbp),%rdi
mov    -0x218(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1ff,%edx
call   1030 <fread@plt>
mov    %eax,-0x21c(%rbp)
cmpl   $0x0,-0x21c(%rbp)
jle    1295 <get_disk_size+0x135>
lea    -0x210(%rbp),%rsi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x210(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
shr    $0xb,%rdx
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
sub    $0x23000,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
shr    $0xb,%rcx
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x220(%rbp)
jmp    129f <get_disk_size+0x13f>
movl   $0x0,-0x220(%rbp)
mov    -0x218(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x220(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_disk_size>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%rbp
mov    %rsp,%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
lea    0xe77(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     1242 <get_disk_size+0xe2>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%r14d
mov    $0x1,%esi
mov    $0x1ff,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1237 <get_disk_size+0xd7>
lea    0xe72(%rip),%rdi
mov    %rsp,%r15
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoul@plt>
mov    %rax,0x0(%rbp)
lea    0xe60(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x0(%rbp),%rsi
mov    %rsi,%rdx
shr    $0xb,%rdx
lea    0xe49(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xfffffffffffdd000,%rdx
add    0x0(%rbp),%rdx
mov    %rdx,0x0(%rbp)
mov    %rdx,%rcx
shr    $0xb,%rcx
lea    0xe42(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    123a <get_disk_size+0xda>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1050 <pclose@plt>
mov    %r14d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [55 x i8] c"fdisk -l | grep \22Disk /dev/mmcblk0\22 | awk '{print $7}'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"fdisk result= %s\0A\00"
@.str.3 = [5 x i8] c"%lu\0A\00"
@.str.4 = [29 x i8] c"disk_size=%lu sectors,%d MB\0A\00"
@.str.5 = [33 x i8] c"p4_size=%08x(%lu) sectors,%d MB\0A\00"
define i32 @get_disk_size(i64* %0) {
%2 = alloca i64*
%3 = alloca [512 x i8]
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i64* %0, i64** %2
store i32 0, i32* %6
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 512)
%9 = call i32* @popen(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %49
12: ; preds = %1
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32 @fread(i8* %13, i32 1, i32 511, i32* %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %45
18: ; preds = %12
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%22 = call i64 @strtoul(i8* %21, i32* null, i32 0)
%23 = load i64*, i64** %2
store i64 %22, i64* %23
%24 = load i64*, i64** %2
%25 = load i64, i64* %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %25)
%27 = load i64*, i64** %2
%28 = load i64, i64* %27
%29 = load i64*, i64** %2
%30 = load i64, i64* %29
%31 = udiv i64 %30, 2048
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %28, i64 %31)
%33 = load i64*, i64** %2
%34 = load i64, i64* %33
%35 = sub i64 %34, 143360
%36 = load i64*, i64** %2
store i64 %35, i64* %36
%37 = load i64*, i64** %2
%38 = load i64, i64* %37
%39 = load i64*, i64** %2
%40 = load i64, i64* %39
%41 = load i64*, i64** %2
%42 = load i64, i64* %41
%43 = udiv i64 %42, 2048
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %38, i64 %40, i64 %43)
store i32 1, i32* %6
br label %46
45: ; preds = %12
store i32 0, i32* %6
br label %46
46: ; preds = %45, %18
%47 = load i32*, i32** %4
%48 = call i32 @pclose(i32* %47)
br label %49
49: ; preds = %46, %1
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
@.str = [55 x i8] c"fdisk -l | grep \22Disk /dev/mmcblk0\22 | awk '{print $7}'\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [18 x i8] c"fdisk result= %s\0A\00"
@.str.3 = [5 x i8] c"%lu\0A\00"
@.str.4 = [29 x i8] c"disk_size=%lu sectors,%d MB\0A\00"
@.str.5 = [33 x i8] c"p4_size=%08x(%lu) sectors,%d MB\0A\00"
define i32 @get_disk_size(i64* nocapture %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 512)
%5 = call i32* @popen(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %24, label %7
7: ; preds = %1
%8 = call i32 @fread(i8* %3, i32 1, i32 511, i32* %5)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %7
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%12 = call i64 @strtoul(i8* nocapture %3, i32* null, i32 0)
store i64 %12, i64* %0
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %12)
%14 = load i64, i64* %0
%15 = lshr i64 %14, 11
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i64 %14, i64 %15)
%17 = load i64, i64* %0
%18 = add i64 %17, -143360
store i64 %18, i64* %0
%19 = lshr i64 %18, 11
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i64 %18, i64 %18, i64 %19)
br label %21
21: ; preds = %7, %10
%22 = phi i32 [ 1, %10 ], [ 0, %7 ]
%23 = call i32 @pclose(i32* %5)
br label %24
24: ; preds = %21, %1
%25 = phi i32 [ %22, %21 ], [ 0, %1 ]
ret i32 %25
}