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

/* Variables and functions */
scalar_t__ EINVAL;
int /*<<< orphan*/ NBD_SET_SIZE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int /*<<< orphan*/, unsigned long long);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
unsigned long long strtoull(char *, char **, int /*<<< orphan*/);
int main(int argc, char **argv) {
  unsigned long long size;
  char *end;
  int fd = -1;
  if (argc != 3) {
    fprintf(stderr, "usage: %s DEV SIZE\n", argv[0]);
    return 1;
  }
  errno = 0;
  size = strtoull(argv[2], &end, 0);
  if (errno || *end) {
    fprintf(stderr, "invalid size\n");
    return 1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  if (ioctl(fd, NBD_SET_SIZE, size) == -1) {
    int status = errno == EINVAL ? 2 : 1;
    perror("NBD_SET_SIZE");
    close(fd);
    return status;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11b1 <main+0x51>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe62(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x168>
mov    0x2e10(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <strtoull@plt>
mov    %rax,-0x18(%rbp)
mov    0x2deb(%rip),%rax
cmpq   $0x0,(%rax)
jne    11f7 <main+0x97>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     121a <main+0xba>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x168>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1255 <main+0xf5>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c8 <main+0x168>
mov    -0x24(%rbp),%edi
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12b9 <main+0x159>
mov    0x2d4e(%rip),%rax
mov    (%rax),%rdx
mov    0x2d3c(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x28(%rbp)
lea    0xd86(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c8 <main+0x168>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    11ba <main+0x5a>
mov    0x2e51(%rip),%r14
movq   $0x0,(%r14)
mov    0x10(%rbx),%rdi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1050 <strtoull@plt>
cmpq   $0x0,(%r14)
jne    11a1 <main+0x41>
mov    %rax,%rbp
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     11e4 <main+0x84>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11d4 <main+0x74>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe33(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%rbx),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1220 <main+0xc0>
mov    %eax,%ebx
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     122e <main+0xce>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
jmp    11d9 <main+0x79>
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
jmp    11d4 <main+0x74>
mov    (%r14),%rax
mov    0x2d88(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
sete   %bpl
add    $0x1,%ebp
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11d9 <main+0x79>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s DEV SIZE\0A\00"
@errno = global i64 0
@.str.1 = [14 x i8] c"invalid size\0A\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@NBD_SET_SIZE = global i32 0
@EINVAL = global i64 0
@.str.3 = [13 x i8] c"NBD_SET_SIZE\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %8
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %62
18: ; preds = %2
store i64 0, i64* @errno
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i64 @strtoull(i8* %21, i8** %7, i32 0)
store i64 %22, i64* %6
%23 = load i64, i64* @errno
%24 = icmp ne i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %18
%26 = load i8*, i8** %7
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25, %18
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %62
33: ; preds = %25
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %62
43: ; preds = %33
%44 = load i32, i32* %8
%45 = load i32, i32* @NBD_SET_SIZE
%46 = load i64, i64* %6
%47 = call i32 @ioctl(i32 %44, i32 %45, i64 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %59
49: ; preds = %43
%50 = load i64, i64* @errno
%51 = load i64, i64* @EINVAL
%52 = icmp eq i64 %50, %51
%53 = zext i1 %52 to i64
%54 = select i1 %52, i32 2, i32 1
store i32 %54, i32* %9
%55 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %9
store i32 %58, i32* %3
br label %62
59: ; preds = %43
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
br label %62
62: ; preds = %59, %49, %41, %30, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"usage: %s DEV SIZE\0A\00"
@errno = global i64 0
@.str.1 = [14 x i8] c"invalid size\0A\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@NBD_SET_SIZE = global i32 0
@EINVAL = global i64 0
@.str.3 = [13 x i8] c"NBD_SET_SIZE\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %44
10: ; preds = %2
store i64 0, i64* @errno
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = call i64 @strtoull(i8* %12, i8** %3, i32 0)
%14 = load i64, i64* @errno
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %10
%17 = load i8*, i8** %3
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16, %10
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %44
23: ; preds = %16
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %44
31: ; preds = %23
%32 = load i32, i32* @NBD_SET_SIZE
%33 = tail call i32 @ioctl(i32 %27, i32 %32, i64 %13)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EINVAL
%38 = icmp eq i64 %36, %37
%39 = select i1 %38, i32 2, i32 1
%40 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 @close(i32 %27)
br label %44
42: ; preds = %31
%43 = tail call i32 @close(i32 %27)
br label %44
44: ; preds = %42, %35, %29, %20, %6
%45 = phi i32 [ 1, %6 ], [ 1, %20 ], [ 1, %29 ], [ %39, %35 ], [ 0, %42 ]
ret i32 %45
}