#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 v4l2_capability {
  int dummy;
};
typedef int /*<<< orphan*/ deviceCapabilities;

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ VIDIOC_QUERYCAP;
int /*<<< orphan*/ memset(struct v4l2_capability *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int xioctl(int, int /*<<< orphan*/, struct v4l2_capability *);
int openDevice(char videoDevice[]) {
  int fileDescriptor = -1;
  struct v4l2_capability deviceCapabilities;
  memset(&(deviceCapabilities), 0, sizeof(deviceCapabilities));
  fileDescriptor = open(videoDevice, O_RDWR | O_NONBLOCK);
  if (fileDescriptor == -1) {
    perror("Failed to open the video device");
    return -1;
  }
  if (xioctl(fileDescriptor, VIDIOC_QUERYCAP, &deviceCapabilities) == -1) {
    perror("Failed to retrieve device parameters");
    return -1;
  }
  return fileDescriptor;
}


x86 assembly (clang, -O0):
<openDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a3 <openDevice+0x63>
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <openDevice+0x9f>
mov    -0x14(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <xioctl@plt>
cmp    $0xffffffff,%eax
jne    11d9 <openDevice+0x99>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <openDevice+0x9f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openDevice>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e88(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1040 <open@plt>
cmp    $0xffffffff,%eax
je     119c <openDevice+0x5c>
mov    %eax,%ebx
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <xioctl@plt>
cmp    $0xffffffff,%eax
je     11a5 <openDevice+0x65>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe5d(%rip),%rdi
jmp    11ac <openDevice+0x6c>
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1194 <openDevice+0x54>

LLVM IR (-O0):
%struct.v4l2_capability = type { i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str = [32 x i8] c"Failed to open the video device\00"
@VIDIOC_QUERYCAP = global i32 0
@.str.1 = [37 x i8] c"Failed to retrieve device parameters\00"
define i32 @openDevice(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.v4l2_capability
store i8* %0, i8** %3
store i32 -1, i32* %4
%6 = call i32 @memset(%struct.v4l2_capability* %5, i32 0, i32 4)
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %25
16: ; preds = %1
%17 = load i32, i32* %4
%18 = load i32, i32* @VIDIOC_QUERYCAP
%19 = call i32 @xioctl(i32 %17, i32 %18, %struct.v4l2_capability* %5)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %25
23: ; preds = %16
%24 = load i32, i32* %4
store i32 %24, i32* %2
br label %25
25: ; preds = %23, %21, %14
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.v4l2_capability = type { i32 }
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str = [32 x i8] c"Failed to open the video device\00"
@VIDIOC_QUERYCAP = global i32 0
@.str.1 = [37 x i8] c"Failed to retrieve device parameters\00"
define i32 @openDevice(i8* nocapture %0) {
%2 = alloca %struct.v4l2_capability
%3 = bitcast %struct.v4l2_capability* %2 to i8*
%4 = call i32 @memset(%struct.v4l2_capability* %2, i32 0, i32 4)
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i32, i32* @VIDIOC_QUERYCAP
%12 = call i32 @xioctl(i32 %8, i32 %11, %struct.v4l2_capability* %2)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10, %1
%15 = phi i8* [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), %10 ]
%16 = call i32 @perror(i8* %15)
br label %17
17: ; preds = %14, %10
%18 = phi i32 [ %8, %10 ], [ -1, %14 ]
ret i32 %18
}