#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 /*<<< orphan*/ sleep(int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int main(int argc, char **argv) {
  int opt;
  char media_device[256];
  int count = 0;
  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);
  }
  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");
  while (count < 100) {
    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\n", mdi.model, mdi.driver);
    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)
movl   $0x0,-0x124(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11dc <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    11e1 <main+0x51>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe2f(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1258 <main+0xc8>
mov    -0x14(%rbp),%eax
sub    $0x64,%eax
jne    1234 <main+0xa4>
jmp    1211 <main+0x81>
lea    -0x120(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
jmp    1253 <main+0xc3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    11e1 <main+0x51>
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
je     1281 <main+0xf1>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x140(%rbp)
cmpl   $0xffffffff,-0x140(%rbp)
jne    12d2 <main+0x142>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x64,-0x124(%rbp)
jge    1379 <main+0x1e9>
mov    -0x140(%rbp),%edi
mov    0x2cc6(%rip),%rax
mov    (%rax),%esi
lea    -0x138(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x13c(%rbp)
cmpl   $0x0,-0x13c(%rbp)
jge    133f <main+0x1af>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    135b <main+0x1cb>
mov    -0x138(%rbp),%rsi
mov    -0x130(%rbp),%rdx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   10b0 <sleep@plt>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    12e0 <main+0x150>
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     12d5 <main+0x135>
mov    %edi,%ebp
lea    0xe58(%rip),%r14
mov    0x2e2b(%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   1090 <getopt@plt>
cmp    $0x64,%eax
jne    120d <main+0x6d>
mov    (%r12),%esi
mov    %r15,%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x10f(%rsp)
jmp    11e0 <main+0x40>
cmp    $0xffffffff,%eax
jne    12d5 <main+0x135>
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
jne    12f0 <main+0x150>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1306 <main+0x166>
mov    %eax,%ebx
lea    0xe2f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x64,%ebp
mov    0x2d64(%rip),%r13
mov    %rsp,%r14
lea    0xdd7(%rip),%r15
lea    0xdec(%rip),%r12
jmp    1292 <main+0xf2>
nop
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
add    $0xffffffff,%ebp
je     12c1 <main+0x121>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
test   %eax,%eax
jns    1270 <main+0xd0>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1283 <main+0xe3>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd21(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xe45(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd06(%rip),%rdi
mov    %rax,%rsi
jmp    12df <main+0x13f>

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"
@MEDIA_IOC_DEVICE_INFO = global i32 0
@.str.5 = [28 x i8] c"Media Device Info errno %s\0A\00"
@.str.6 = [33 x i8] c"Media device model %s driver %s\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
store i32 0, i32* %8
%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 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%48 = load i32, i32* @O_RDWR
%49 = call i32 @open(i8* %47, i32 %48)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %57
52: ; preds = %46
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %54)
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %46
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([195 x i8], [195 x i8]* @.str.4, i64 0, i64 0))
br label %59
59: ; preds = %78, %57
%60 = load i32, i32* %8
%61 = icmp slt i32 %60, 100
br i1 %61, label %62, label %82
62: ; preds = %59
%63 = load i32, i32* %11
%64 = load i32, i32* @MEDIA_IOC_DEVICE_INFO
%65 = call i32 @ioctl(i32 %63, i32 %64, %struct.media_device_info* %9)
store i32 %65, i32* %10
%66 = load i32, i32* %10
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %62
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %70)
br label %78
72: ; preds = %62
%73 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %9, i32 0, i32 0
%74 = load i8*, i8** %73
%75 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %9, i32 0, i32 1
%76 = load i8*, i8** %75
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %74, i8* %76)
br label %78
78: ; preds = %72, %68
%79 = call i32 @sleep(i32 10)
%80 = load i32, i32* %8
%81 = add nsw i32 %80, 1
store i32 %81, i32* %8
br label %59
82: ; preds = %59
%83 = load i32, i32* %3
ret i32 %83
}

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
@MEDIA_IOC_DEVICE_INFO = global i32 0
@.str.5 = [28 x i8] c"Media Device Info errno %s\0A\00"
@.str.6 = [33 x i8] c"Media device model %s driver %s\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.7 = [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.7, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %23
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %5, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %29
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([194 x i8], [194 x i8]* @str, i64 0, i64 0))
%40 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %4, i64 0, i32 0
%41 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %4, i64 0, i32 1
br label %42
42: ; preds = %38, %55
%43 = phi i32 [ 0, %38 ], [ %57, %55 ]
%44 = load i32, i32* @MEDIA_IOC_DEVICE_INFO
%45 = call i32 @ioctl(i32 %31, i32 %44, %struct.media_device_info* %4)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %49)
br label %55
51: ; preds = %42
%52 = load i8*, i8** %40
%53 = load i8*, i8** %41
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i8* %52, i8* %53)
br label %55
55: ; preds = %51, %47
%56 = call i32 @sleep(i32 10)
%57 = add nuw nsw i32 %43, 1
%58 = icmp eq i32 %57, 100
br i1 %58, label %59, label %42
59: ; preds = %55
ret i32 0
}