#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ USBDEVFS_GET_CAPABILITIES;
int /*<<< orphan*/ claim_some_intf(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ drop_privileges(int, int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ reset_device(int);
int scanf(char *, int *);
int main(int argc, char *argv[]) {
  uint32_t mask, caps;
  int c, fd;
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    printf("Failed to open file\n");
    goto err_fd;
  }
  ioctl(fd, USBDEVFS_GET_CAPABILITIES, &caps);
  if (!(caps & 0x40)) {
    printf("DROP_PRIVILEGES not supported\n");
    goto err;
  }
  drop_privileges(fd, -1U);
  printf("Available options:\n"
         "[0] Exit now\n"
         "[1] Reset device. Should fail if device is in use\n"
         "[2] Claim 4 interfaces. Should succeed where not in use\n"
         "[3] Narrow interface permission mask\n"
         "Which option shall I run?: ");
  while (scanf("%d", &c) == 1) {
    switch (c) {
    case 0:
      goto exit;
    case 1:
      reset_device(fd);
      break;
    case 2:
      claim_some_intf(fd);
      break;
    case 3:
      printf("Insert new mask: ");
      scanf("%x", &mask);
      drop_privileges(fd, mask);
      break;
    default:
      printf("I don't recognize that\n");
    }
    printf("Which test shall I run next?: ");
  }
exit:
  close(fd);
  return 0;
err:
  close(fd);
err_fd:
  return 1;
}


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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ca <main+0x4a>
lea    0xe50(%rip),%rdi
call   1050 <printf@plt>
jmp    12df <main+0x15f>
mov    -0x20(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x18(%rbp),%eax
and    $0x40,%eax
cmp    $0x0,%eax
jne    11ff <main+0x7f>
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
jmp    12d7 <main+0x157>
mov    -0x20(%rbp),%edi
mov    $0xffffffff,%esi
call   1030 <drop_privileges@plt>
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
lea    0xef0(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   1090 <scanf@plt>
cmp    $0x1,%eax
jne    12be <main+0x13e>
mov    -0x1c(%rbp),%eax
mov    %rax,-0x28(%rbp)
sub    $0x3,%rax
ja     12a1 <main+0x121>
mov    -0x28(%rbp),%rax
lea    0xdb3(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
jmp    12c3 <main+0x143>
mov    -0x20(%rbp),%edi
call   1040 <reset_device@plt>
jmp    12ad <main+0x12d>
mov    -0x20(%rbp),%edi
call   1080 <claim_some_intf@plt>
jmp    12ad <main+0x12d>
lea    0xe96(%rip),%rdi
call   1050 <printf@plt>
lea    0xe9c(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1090 <scanf@plt>
mov    -0x20(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <drop_privileges@plt>
jmp    12ad <main+0x12d>
lea    0xe7f(%rip),%rdi
call   1050 <printf@plt>
lea    0xe8b(%rip),%rdi
call   1050 <printf@plt>
jmp    1218 <main+0x98>
jmp    12c3 <main+0x143>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <main+0x166>
mov    -0x20(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
test   %eax,%eax
js     11ef <main+0x5f>
mov    %eax,%ebx
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
call   1070 <ioctl@plt>
testb  $0x40,0x10(%rsp)
jne    1205 <main+0x75>
lea    0xf39(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0x1,%eax
jmp    12e1 <main+0x151>
lea    0xf51(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12e1 <main+0x151>
mov    %ebx,%edi
mov    $0xffffffff,%esi
call   1030 <drop_privileges@plt>
lea    0xdf8(%rip),%rdi
call   1060 <printf@plt>
lea    0xeb7(%rip),%rdi
lea    0xc(%rsp),%rsi
call   10a0 <scanf@plt>
cmp    $0x1,%eax
jne    12d8 <main+0x148>
lea    0xeb5(%rip),%r15
lea    0xe96(%rip),%r12
lea    0xc(%rsp),%r13
lea    0xdaf(%rip),%r14
lea    0x14(%rsp),%rbp
jmp    1284 <main+0xf4>
nopl   0x0(%rax,%rax,1)
lea    0xec9(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1060 <printf@plt>
mov    %r12,%rdi
mov    %r13,%rsi
call   10a0 <scanf@plt>
cmp    $0x1,%eax
jne    12d8 <main+0x148>
mov    0xc(%rsp),%eax
cmp    $0x3,%rax
ja     1260 <main+0xd0>
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
mov    %ebx,%edi
call   1050 <reset_device@plt>
jmp    126c <main+0xdc>
mov    %ebx,%edi
call   1090 <claim_some_intf@plt>
jmp    126c <main+0xdc>
nopl   0x0(%rax)
lea    0xe27(%rip),%rdi
call   1060 <printf@plt>
lea    0xe2d(%rip),%rdi
mov    %rbp,%rsi
call   10a0 <scanf@plt>
mov    0x14(%rsp),%esi
mov    %ebx,%edi
call   1030 <drop_privileges@plt>
jmp    126c <main+0xdc>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@.str = [21 x i8] c"Failed to open file\0A\00"
@USBDEVFS_GET_CAPABILITIES = global i32 0
@.str.1 = [31 x i8] c"DROP_PRIVILEGES not supported\0A\00"
@.str.2 = [203 x i8] c"Available options:\0A[0] Exit now\0A[1] Reset device. Should fail if device is in use\0A[2] Claim 4 interfaces. Should succeed where not in use\0A[3] Narrow interface permission mask\0AWhich option shall I run?: \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"Insert new mask: \00"
@.str.5 = [3 x i8] c"%x\00"
@.str.6 = [24 x i8] c"I don't recognize that\0A\00"
@.str.7 = [31 x i8] c"Which test shall I run next?: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %61
19: ; preds = %2
%20 = load i32, i32* %9
%21 = load i32, i32* @USBDEVFS_GET_CAPABILITIES
%22 = call i32 @ioctl(i32 %20, i32 %21, i32* %7)
%23 = load i32, i32* %7
%24 = and i32 %23, 64
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %58
28: ; preds = %19
%29 = load i32, i32* %9
%30 = call i32 @drop_privileges(i32 %29, i32 -1)
%31 = call i32 @printf(i8* getelementptr inbounds ([203 x i8], [203 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %52, %28
%33 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %8)
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %54
35: ; preds = %32
%36 = load i32, i32* %8
switch i32 %36, label %50 [
i32 0, label %37
i32 1, label %38
i32 2, label %41
i32 3, label %44
]
37: ; preds = %35
br label %55
38: ; preds = %35
%39 = load i32, i32* %9
%40 = call i32 @reset_device(i32 %39)
br label %52
41: ; preds = %35
%42 = load i32, i32* %9
%43 = call i32 @claim_some_intf(i32 %42)
br label %52
44: ; preds = %35
%45 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%46 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %6)
%47 = load i32, i32* %9
%48 = load i32, i32* %6
%49 = call i32 @drop_privileges(i32 %47, i32 %48)
br label %52
50: ; preds = %35
%51 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %52
52: ; preds = %50, %44, %41, %38
%53 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0))
br label %32
54: ; preds = %32
br label %55
55: ; preds = %54, %37
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
store i32 0, i32* %3
br label %62
58: ; preds = %26
%59 = load i32, i32* %9
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %58, %17
store i32 1, i32* %3
br label %62
62: ; preds = %61, %55
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@USBDEVFS_GET_CAPABILITIES = global i32 0
@.str.2 = [203 x i8] c"Available options:\0A[0] Exit now\0A[1] Reset device. Should fail if device is in use\0A[2] Claim 4 interfaces. Should succeed where not in use\0A[3] Narrow interface permission mask\0AWhich option shall I run?: \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [18 x i8] c"Insert new mask: \00"
@.str.5 = [3 x i8] c"%x\00"
@.str.7 = [31 x i8] c"Which test shall I run next?: \00"
@str = [30 x i8] c"DROP_PRIVILEGES not supported\00"
@str.8 = [23 x i8] c"I don't recognize that\00"
@str.9 = [20 x i8] c"Failed to open file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
br label %49
16: ; preds = %2
%17 = load i32, i32* @USBDEVFS_GET_CAPABILITIES
%18 = call i32 @ioctl(i32 %12, i32 %17, i32* %4)
%19 = load i32, i32* %4
%20 = and i32 %19, 64
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%24 = call i32 @close(i32 %12)
br label %49
25: ; preds = %16
%26 = call i32 @drop_privileges(i32 %12, i32 -1)
%27 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([203 x i8], [203 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %47
30: ; preds = %25, %43
%31 = load i32, i32* %5
switch i32 %31, label %41 [
i32 0, label %47
i32 1, label %32
i32 2, label %34
i32 3, label %36
]
32: ; preds = %30
%33 = call i32 @reset_device(i32 %12)
br label %43
34: ; preds = %30
%35 = call i32 @claim_some_intf(i32 %12)
br label %43
36: ; preds = %30
%37 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%39 = load i32, i32* %3
%40 = call i32 @drop_privileges(i32 %12, i32 %39)
br label %43
41: ; preds = %30
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
br label %43
43: ; preds = %41, %36, %34, %32
%44 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0))
%45 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%46 = icmp eq i32 %45, 1
br i1 %46, label %30, label %47
47: ; preds = %30, %43, %25
%48 = call i32 @close(i32 %12)
br label %49
49: ; preds = %14, %22, %47
%50 = phi i32 [ 0, %47 ], [ 1, %22 ], [ 1, %14 ]
ret i32 %50
}