#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int ioctl(int, int, 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 blksize;
  char *end;
  int fd = -1;
  if (argc != 3) {
    fprintf(stderr, "usage: %s DEV BLKSIZE\n", argv[0]);
    return 1;
  }
  errno = 0;
  blksize = strtoull(argv[2], &end, 0);
  if (errno || *end) {
    fprintf(stderr, "invalid block size\n");
    return 1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  if (ioctl(fd, 0x4C09, blksize) == -1) {
    int status = errno == EINVAL ? 2 : 1;
    perror("LOOP_SET_BLOCK_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    0x2e4a(%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    12c4 <main+0x164>
mov    0x2e18(%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    0x2df3(%rip),%rax
cmpq   $0x0,(%rax)
jne    11f7 <main+0x97>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     121a <main+0xba>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c4 <main+0x164>
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    0xde7(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c4 <main+0x164>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    $0x4c09,%esi
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    12b5 <main+0x155>
mov    0x2d5a(%rip),%rax
mov    (%rax),%rdx
mov    0x2d48(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x28(%rbp)
lea    0xd93(%rip),%rdi
call   1080 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c4 <main+0x164>
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    0x2e59(%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    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11d4 <main+0x74>
mov    0x2e17(%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     121c <main+0xbc>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4c09,%esi
mov    %rbp,%rdx
call   1030 <ioctl@plt>
cmp    $0xffffffff,%eax
je     122a <main+0xca>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebp,%ebp
jmp    11d9 <main+0x79>
lea    0xe08(%rip),%rdi
call   1080 <perror@plt>
jmp    11d4 <main+0x74>
mov    (%r14),%rax
mov    0x2d94(%rip),%rcx
xor    %ebp,%ebp
cmp    (%rcx),%rax
sete   %bpl
add    $0x1,%ebp
lea    0xde9(%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 = [23 x i8] c"usage: %s DEV BLKSIZE\0A\00"
@errno = global i64 0
@.str.1 = [20 x i8] c"invalid block size\0A\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@EINVAL = global i64 0
@.str.3 = [20 x i8] c"LOOP_SET_BLOCK_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 ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %61
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 ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %61
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 %61
43: ; preds = %33
%44 = load i32, i32* %8
%45 = load i64, i64* %6
%46 = call i32 @ioctl(i32 %44, i32 19465, i64 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %58
48: ; preds = %43
%49 = load i64, i64* @errno
%50 = load i64, i64* @EINVAL
%51 = icmp eq i64 %49, %50
%52 = zext i1 %51 to i64
%53 = select i1 %51, i32 2, i32 1
store i32 %53, i32* %9
%54 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* %8
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %9
store i32 %57, i32* %3
br label %61
58: ; preds = %43
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
store i32 0, i32* %3
br label %61
61: ; preds = %58, %48, %41, %30, %12
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [23 x i8] c"usage: %s DEV BLKSIZE\0A\00"
@errno = global i64 0
@.str.1 = [20 x i8] c"invalid block size\0A\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@EINVAL = global i64 0
@.str.3 = [20 x i8] c"LOOP_SET_BLOCK_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 ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %43
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 ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %43
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 %43
31: ; preds = %23
%32 = tail call i32 @ioctl(i32 %27, i32 19465, i64 %13)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINVAL
%37 = icmp eq i64 %35, %36
%38 = select i1 %37, i32 2, i32 1
%39 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @close(i32 %27)
br label %43
41: ; preds = %31
%42 = tail call i32 @close(i32 %27)
br label %43
43: ; preds = %41, %34, %29, %20, %6
%44 = phi i32 [ 1, %6 ], [ 1, %20 ], [ 1, %29 ], [ %38, %34 ], [ 0, %41 ]
ret i32 %44
}