#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 */
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ W_OK;
int access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *);
int stat(char const *, struct stat *);
int check_dev(const char *dev_path, int *num_blocks) {
  struct stat st;
  if (stat(dev_path, &st) == -1) {
    printf("cannot find file '%s' \n", dev_path);
    return 0;
  }
  if (access(dev_path, W_OK) == -1) {
    printf("no write access to '%s'.\n", dev_path);
    return 0;
  }
  (*num_blocks) = st.st_size / 1024;
  return 1;
}


x86 assembly (clang, -O0):
<check_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1172 <check_dev+0x42>
mov    -0x10(%rbp),%rsi
lea    0xe9f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <check_dev+0x93>
mov    -0x10(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0xffffffff,%eax
jne    11a9 <check_dev+0x79>
mov    -0x10(%rbp),%rsi
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c3 <check_dev+0x93>
mov    -0x20(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_dev>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     1178 <check_dev+0x48>
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <access@plt>
cmp    $0xffffffff,%eax
je     1181 <check_dev+0x51>
mov    (%rsp),%eax
lea    0x3ff(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0xa,%ecx
mov    %ecx,(%r14)
mov    $0x1,%eax
jmp    1192 <check_dev+0x62>
lea    0xe81(%rip),%rdi
jmp    1188 <check_dev+0x58>
lea    0xe90(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [24 x i8] c"cannot find file '%s' \0A\00"
@W_OK = global i32 0
@.str.1 = [26 x i8] c"no write access to '%s'.\0A\00"
define i32 @check_dev(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i32* %1, i32** %5
%7 = load i8*, i8** %4
%8 = call i32 @stat(i8* %7, %struct.stat* %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 0, i32* %3
br label %26
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @W_OK
%16 = call i32 @access(i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 0, i32* %3
br label %26
21: ; preds = %13
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = sdiv i32 %23, 1024
%25 = load i32*, i32** %5
store i32 %24, i32* %25
store i32 1, i32* %3
br label %26
26: ; preds = %21, %18, %10
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [24 x i8] c"cannot find file '%s' \0A\00"
@W_OK = global i32 0
@.str.1 = [26 x i8] c"no write access to '%s'.\0A\00"
define i32 @check_dev(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %19
9: ; preds = %2
%10 = load i32, i32* @W_OK
%11 = tail call i32 @access(i8* %0, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %19
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = sdiv i32 %17, 1024
store i32 %18, i32* %1
br label %19
19: ; preds = %15, %13, %7
%20 = phi i32 [ 0, %7 ], [ 0, %13 ], [ 1, %15 ]
ret i32 %20
}