#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 media_device_info {
  char *model;
  char *driver;
};
typedef int /*<<< orphan*/ media_device;

/* Variables and functions */
int /*<<< orphan*/ MEDIA_IOC_DEVICE_INFO;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
scalar_t__ getuid();
int ioctl(int, int /*<<< orphan*/, struct media_device_info *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(unsigned int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
scalar_t__ time(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  int opt;
  char media_device[256];
  int count;
  struct media_device_info mdi;
  int ret;
  int fd;
  if (argc < 2) {
    printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
    exit(-1);
  }
  while ((opt = getopt(argc, argv, "d:")) != -1) {
    switch (opt) {
    case 'd':
      strncpy(media_device, optarg, sizeof(media_device) - 1);
      media_device[sizeof(media_device) - 1] = '\0';
      break;
    default:
      printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
      exit(-1);
    }
  }
  if (getuid() != 0) {
    printf("Please run the test as root - Exiting.\n");
    exit(-1);
  }
  srand((unsigned int)time(NULL));
  count = rand();
  fd = open(media_device, O_RDWR);
  if (fd == -1) {
    printf("Media Device open errno %s\n", strerror(errno));
    exit(-1);
  }
  printf("\nNote:\n"
         "While test is running, remove the device and\n"
         "ensure there are no use after free errors and\n"
         "other Oops in the dmesg. Enable KaSan kernel\n"
         "config option for use-after-free error detection.\n\n");
  printf("Running test for %d iternations\n", count);
  while (count > 0) {
    ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
    if (ret < 0)
      printf("Media Device Info errno %s\n", strerror(errno));
    else
      printf("Media device model %s driver %s - count %d\n", mdi.model,
             mdi.driver, count);
    sleep(10);
    count--;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1202 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    1207 <main+0x47>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe09(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     127e <main+0xbe>
mov    -0x14(%rbp),%eax
sub    $0x64,%eax
jne    125a <main+0x9a>
jmp    1237 <main+0x77>
lea    -0x120(%rbp),%rdi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
jmp    1279 <main+0xb9>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    1207 <main+0x47>
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
je     12a7 <main+0xe7>
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0x0,%al
call   10e0 <rand@plt>
mov    %eax,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
mov    0x2d0e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x140(%rbp)
cmpl   $0xffffffff,-0x140(%rbp)
jne    1315 <main+0x155>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x124(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x124(%rbp)
jle    13d6 <main+0x216>
mov    -0x140(%rbp),%edi
mov    0x2c6f(%rip),%rax
mov    (%rax),%esi
lea    -0x138(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x13c(%rbp)
cmpl   $0x0,-0x13c(%rbp)
jge    1396 <main+0x1d6>
mov    0x2c57(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13b8 <main+0x1f8>
mov    -0x138(%rbp),%rsi
mov    -0x130(%rbp),%rdx
mov    -0x124(%rbp),%ecx
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   10d0 <sleep@plt>
mov    -0x124(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x124(%rbp)
jmp    1337 <main+0x177>
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jl     133c <main+0x16c>
mov    %edi,%ebp
lea    0xe28(%rip),%r14
mov    0x2dfb(%rip),%r12
lea    0x10(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10b0 <getopt@plt>
cmp    $0x64,%eax
jne    123d <main+0x6d>
mov    (%r12),%esi
mov    %r15,%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x10f(%rsp)
jmp    1210 <main+0x40>
cmp    $0xffffffff,%eax
jne    133c <main+0x16c>
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
jne    1357 <main+0x187>
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
xor    %eax,%eax
call   10f0 <rand@plt>
mov    %eax,%ebx
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     136d <main+0x19d>
mov    %eax,%ebp
lea    0xe14(%rip),%rdi
call   1040 <puts@plt>
lea    0xd9f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebx,%ebx
jle    1328 <main+0x158>
mov    0x2d0e(%rip),%r13
mov    %rsp,%r14
lea    0xda2(%rip),%r15
lea    0xdb7(%rip),%r12
jmp    12f9 <main+0x129>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %ebx,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
jle    1328 <main+0x158>
mov    0x0(%r13),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
test   %eax,%eax
jns    12d0 <main+0x100>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12e5 <main+0x115>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xcba(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    0x2c5c(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc9f(%rip),%rdi
mov    %rax,%rsi
jmp    1346 <main+0x176>

LLVM IR (-O0):
%struct.media_device_info = type { i8*, i8* }
@.str = [30 x i8] c"Usage: %s [-d </dev/mediaX>]\0A\00"
@.str.1 = [3 x i8] c"d:\00"
@optarg = global i32 0
@.str.2 = [40 x i8] c"Please run the test as root - Exiting.\0A\00"
@O_RDWR = global i32 0
@.str.3 = [28 x i8] c"Media Device open errno %s\0A\00"
@errno = global i32 0
@.str.4 = [195 x i8] c"\0ANote:\0AWhile test is running, remove the device and\0Aensure there are no use after free errors and\0Aother Oops in the dmesg. Enable KaSan kernel\0Aconfig option for use-after-free error detection.\0A\0A\00"
@.str.5 = [33 x i8] c"Running test for %d iternations\0A\00"
@MEDIA_IOC_DEVICE_INFO = global i32 0
@.str.6 = [28 x i8] c"Media Device Info errno %s\0A\00"
@.str.7 = [44 x i8] c"Media device model %s driver %s - count %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca %struct.media_device_info
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
br label %21
21: ; preds = %39, %20
%22 = load i32, i32* %4
%23 = load i8**, i8*** %5
%24 = call i32 @getopt(i32 %22, i8** %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 %24, i32* %6
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %40
26: ; preds = %21
%27 = load i32, i32* %6
switch i32 %27, label %33 [
i32 100, label %28
]
28: ; preds = %26
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%30 = load i32, i32* @optarg
%31 = call i32 @strncpy(i8* %29, i32 %30, i32 255)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 255
store i8 0, i8* %32
br label %39
33: ; preds = %26
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %28
br label %21
40: ; preds = %21
%41 = call i64 (...) @getuid()
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 -1)
unreachable
46: ; preds = %40
%47 = call i64 @time(i32* null)
%48 = trunc i64 %47 to i32
%49 = call i32 @srand(i32 %48)
%50 = call i32 (...) @rand()
store i32 %50, i32* %8
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%52 = load i32, i32* @O_RDWR
%53 = call i32 @open(i8* %51, i32 %52)
store i32 %53, i32* %11
%54 = load i32, i32* %11
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %61
56: ; preds = %46
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %58)
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %46
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([195 x i8], [195 x i8]* @.str.4, i64 0, i64 0))
%63 = load i32, i32* %8
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %63)
br label %65
65: ; preds = %85, %61
%66 = load i32, i32* %8
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %89
68: ; preds = %65
%69 = load i32, i32* %11
%70 = load i32, i32* @MEDIA_IOC_DEVICE_INFO
%71 = call i32 @ioctl(i32 %69, i32 %70, %struct.media_device_info* %9)
store i32 %71, i32* %10
%72 = load i32, i32* %10
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %68
%75 = load i32, i32* @errno
%76 = call i8* @strerror(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %76)
br label %85
78: ; preds = %68
%79 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %9, i32 0, i32 0
%80 = load i8*, i8** %79
%81 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %9, i32 0, i32 1
%82 = load i8*, i8** %81
%83 = load i32, i32* %8
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %80, i8* %82, i32 %83)
br label %85
85: ; preds = %78, %74
%86 = call i32 @sleep(i32 10)
%87 = load i32, i32* %8
%88 = add nsw i32 %87, -1
store i32 %88, i32* %8
br label %65
89: ; preds = %65
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.media_device_info = type { i8*, i8* }
@.str = [30 x i8] c"Usage: %s [-d </dev/mediaX>]\0A\00"
@.str.1 = [3 x i8] c"d:\00"
@optarg = global i32 0
@O_RDWR = global i32 0
@.str.3 = [28 x i8] c"Media Device open errno %s\0A\00"
@errno = global i32 0
@.str.5 = [33 x i8] c"Running test for %d iternations\0A\00"
@MEDIA_IOC_DEVICE_INFO = global i32 0
@.str.6 = [28 x i8] c"Media Device Info errno %s\0A\00"
@.str.7 = [44 x i8] c"Media device model %s driver %s - count %d\0A\00"
@str = [194 x i8] c"\0ANote:\0AWhile test is running, remove the device and\0Aensure there are no use after free errors and\0Aother Oops in the dmesg. Enable KaSan kernel\0Aconfig option for use-after-free error detection.\0A\00"
@str.8 = [39 x i8] c"Please run the test as root - Exiting.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [256 x i8]
%4 = alloca %struct.media_device_info
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.media_device_info* %4 to i8*
%7 = icmp slt i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 255
br label %14
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %8, %16
%15 = call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
switch i32 %15, label %19 [
i32 -1, label %23
i32 100, label %16
]
16: ; preds = %14
%17 = load i32, i32* @optarg
%18 = call i32 @strncpy(i8* %5, i32 %17, i32 255)
store i8 0, i8* %9
br label %14
19: ; preds = %14
%20 = load i8*, i8** %1
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %14
%24 = call i64 (...) @getuid()
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.8, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %23
%30 = call i64 @time(i32* null)
%31 = trunc i64 %30 to i32
%32 = call i32 @srand(i32 %31)
%33 = call i32 (...) @rand()
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* %5, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %29
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %29
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([194 x i8], [194 x i8]* @str, i64 0, i64 0))
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%45 = icmp sgt i32 %33, 0
br i1 %45, label %46, label %66
46: ; preds = %42
%47 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %4, i64 0, i32 0
%48 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %4, i64 0, i32 1
br label %49
49: ; preds = %46, %62
%50 = phi i32 [ %33, %46 ], [ %64, %62 ]
%51 = load i32, i32* @MEDIA_IOC_DEVICE_INFO
%52 = call i32 @ioctl(i32 %35, i32 %51, %struct.media_device_info* %4)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %49
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %56)
br label %62
58: ; preds = %49
%59 = load i8*, i8** %47
%60 = load i8*, i8** %48
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %59, i8* %60, i32 %50)
br label %62
62: ; preds = %58, %54
%63 = call i32 @sleep(i32 10)
%64 = add nsw i32 %50, -1
%65 = icmp sgt i32 %50, 1
br i1 %65, label %49, label %66
66: ; preds = %62, %42
ret i32 0
}