#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 *, ...);
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);
  }
  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);
}


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    11cc <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    11d1 <main+0x51>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe3f(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1248 <main+0xc8>
mov    -0x14(%rbp),%eax
sub    $0x64,%eax
jne    1224 <main+0xa4>
jmp    1201 <main+0x81>
lea    -0x120(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
jmp    1243 <main+0xc3>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    11d1 <main+0x51>
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
je     1271 <main+0xf1>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x140(%rbp)
cmpl   $0xffffffff,-0x140(%rbp)
jne    12c2 <main+0x142>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x140(%rbp),%edi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
lea    -0x138(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x13c(%rbp)
cmpl   $0x0,-0x13c(%rbp)
jge    1314 <main+0x194>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1330 <main+0x1b0>
mov    -0x138(%rbp),%rsi
mov    -0x130(%rbp),%rdx
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jl     1283 <main+0xf3>
mov    %edi,%ebp
lea    0xe6a(%rip),%r14
mov    0x2e3d(%rip),%r12
lea    0x10(%rsp),%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getopt@plt>
cmp    $0x64,%eax
jne    11ed <main+0x5d>
mov    (%r12),%esi
mov    %r15,%rdi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x10f(%rsp)
jmp    11c0 <main+0x30>
cmp    $0xffffffff,%eax
jne    1283 <main+0xf3>
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
jne    129e <main+0x10e>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x124>
mov    0x2d97(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1070 <ioctl@plt>
test   %eax,%eax
js     1252 <main+0xc2>
mov    (%rsp),%rsi
mov    0x8(%rsp),%rdx
lea    0xe10(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1271 <main+0xe1>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdd6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd73(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xdd5(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd58(%rip),%rdi
mov    %rax,%rsi
jmp    128d <main+0xfd>

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
@MEDIA_IOC_DEVICE_INFO = global i32 0
@.str.4 = [28 x i8] c"Media Device Info errno %s\0A\00"
@.str.5 = [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 = load i32, i32* %11
%59 = load i32, i32* @MEDIA_IOC_DEVICE_INFO
%60 = call i32 @ioctl(i32 %58, i32 %59, %struct.media_device_info* %9)
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %57
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %65)
br label %73
67: ; preds = %57
%68 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %9, i32 0, i32 0
%69 = load i8*, i8** %68
%70 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %9, i32 0, i32 1
%71 = load i8*, i8** %70
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %69, i8* %71)
br label %73
73: ; preds = %67, %63
%74 = load i32, i32* %3
ret i32 %74
}

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.4 = [28 x i8] c"Media Device Info errno %s\0A\00"
@.str.5 = [33 x i8] c"Media device model %s driver %s\0A\00"
@str = [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, 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 = load i32, i32* @MEDIA_IOC_DEVICE_INFO
%40 = call i32 @ioctl(i32 %31, i32 %39, %struct.media_device_info* %4)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %52
46: ; preds = %38
%47 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %4, i64 0, i32 0
%48 = load i8*, i8** %47
%49 = getelementptr inbounds %struct.media_device_info, %struct.media_device_info* %4, i64 0, i32 1
%50 = load i8*, i8** %49
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %48, i8* %50)
br label %52
52: ; preds = %46, %42
ret i32 0
}