#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ vid_format;
struct TYPE_3__ {
  int width;
  int height;
  int bytesperline;
  int sizeimage;
  int /*<<< orphan*/ colorspace;
  scalar_t__ priv;
  int /*<<< orphan*/ field;
  int /*<<< orphan*/ pixelformat;
};
struct TYPE_4__ {
  TYPE_1__ pix;
};
struct v4l2_format {
  TYPE_2__ fmt;
  int /*<<< orphan*/ type;
};
struct v4l2_capability {
  TYPE_2__ fmt;
  int /*<<< orphan*/ type;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ V4L2_BUF_TYPE_VIDEO_OUTPUT;
int /*<<< orphan*/ V4L2_COLORSPACE_JPEG;
int /*<<< orphan*/ V4L2_FIELD_NONE;
int /*<<< orphan*/ V4L2_PIX_FMT_BGR24;
int /*<<< orphan*/ V4L2_PIX_FMT_RGB24;
int /*<<< orphan*/ V4L2_PIX_FMT_UYVY;
int /*<<< orphan*/ VIDIOC_QUERYCAP;
int /*<<< orphan*/ VIDIOC_S_FMT;
int ioctl(int, int /*<<< orphan*/, struct v4l2_format *);
int /*<<< orphan*/ memset(struct v4l2_format *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int open_device(char *dev, int width, int height, int pixFormat) {
  struct v4l2_capability vid_caps;
  struct v4l2_format vid_format;
  memset(&vid_format, 0, sizeof(vid_format));
  vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  vid_format.fmt.pix.width = width;
  vid_format.fmt.pix.height = height;
  switch (pixFormat) {
  case 1:
    vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
    vid_format.fmt.pix.bytesperline = (width * 3);
    vid_format.fmt.pix.sizeimage = (width * height * 3);
    break;
  case 2:
    vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
    vid_format.fmt.pix.bytesperline = (width * 2);
    vid_format.fmt.pix.sizeimage = (width * height * 2);
    break;
  case 3:
    vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
    vid_format.fmt.pix.bytesperline = (width * 3);
    vid_format.fmt.pix.sizeimage = (width * height * 3);
    break;
  default:
    return -1;
  }
  vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  vid_format.fmt.pix.priv = 0;
  vid_format.fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  int fdwr = open(dev, O_RDWR);
  int ret_code = ioctl(fdwr, VIDIOC_QUERYCAP, &vid_caps);
  ret_code = ioctl(fdwr, VIDIOC_S_FMT, &vid_format);
  return fdwr;
}


x86 assembly (clang, -O0):
<open_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x8c(%rbp)
sub    $0x1,%eax
je     11af <open_device+0x7f>
jmp    1187 <open_device+0x57>
mov    -0x8c(%rbp),%eax
sub    $0x2,%eax
je     11d4 <open_device+0xa4>
jmp    119b <open_device+0x6b>
mov    -0x8c(%rbp),%eax
sub    $0x3,%eax
je     11fb <open_device+0xcb>
jmp    1220 <open_device+0xf0>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
imul   $0x3,-0x14(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x74(%rbp)
jmp    122c <open_device+0xfc>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x78(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    122c <open_device+0xfc>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
imul   $0x3,-0x14(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x74(%rbp)
jmp    122c <open_device+0xfc>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a9 <open_device+0x179>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x84(%rbp)
mov    -0x84(%rbp),%edi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x88(%rbp)
mov    -0x84(%rbp),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x88(%rbp)
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_device>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    %ecx,%ebp
mov    %edx,%r12d
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %r15d,(%rsp)
mov    %r12d,0x4(%rsp)
cmp    $0x3,%ebp
je     118d <open_device+0x5d>
cmp    $0x2,%ebp
je     11a8 <open_device+0x78>
mov    $0xffffffff,%ebx
cmp    $0x1,%ebp
jne    1227 <open_device+0xf7>
mov    0x2e3d(%rip),%rax
jmp    1194 <open_device+0x64>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    (%r15,%r15,2),%eax
mov    %eax,0x8(%rsp)
imul   %eax,%r12d
jmp    11c4 <open_device+0x94>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    (%r15,%r15,1),%eax
mov    %eax,0x8(%rsp)
imul   %r15d,%r12d
add    %r12d,%r12d
mov    %r12d,0xc(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
lea    0x30(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %ebx,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.v4l2_capability = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32, i32, i32, i32, i32, i64, i32, i32 }
%struct.v4l2_format = type { %struct.TYPE_4__, i32 }
@V4L2_BUF_TYPE_VIDEO_OUTPUT = global i32 0
@V4L2_PIX_FMT_RGB24 = global i32 0
@V4L2_PIX_FMT_UYVY = global i32 0
@V4L2_PIX_FMT_BGR24 = global i32 0
@V4L2_FIELD_NONE = global i32 0
@V4L2_COLORSPACE_JPEG = global i32 0
@O_RDWR = global i32 0
@VIDIOC_QUERYCAP = global i32 0
@VIDIOC_S_FMT = global i32 0
define i32 @open_device(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.v4l2_capability
%11 = alloca %struct.v4l2_format
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = call i32 @memset(%struct.v4l2_format* %11, i32 0, i32 48)
%15 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_OUTPUT
%16 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%19 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %18, i32 0, i32 0
%20 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %19, i32 0, i32 0
store i32 %17, i32* %20
%21 = load i32, i32* %8
%22 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %22, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %23, i32 0, i32 1
store i32 %21, i32* %24
%25 = load i32, i32* %9
switch i32 %25, label %77 [
i32 1, label %26
i32 2, label %43
i32 3, label %60
]
26: ; preds = %4
%27 = load i32, i32* @V4L2_PIX_FMT_RGB24
%28 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %28, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %29, i32 0, i32 7
store i32 %27, i32* %30
%31 = load i32, i32* %7
%32 = mul nsw i32 %31, 3
%33 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%34 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %33, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %34, i32 0, i32 2
store i32 %32, i32* %35
%36 = load i32, i32* %7
%37 = load i32, i32* %8
%38 = mul nsw i32 %36, %37
%39 = mul nsw i32 %38, 3
%40 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %40, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %41, i32 0, i32 3
store i32 %39, i32* %42
br label %78
43: ; preds = %4
%44 = load i32, i32* @V4L2_PIX_FMT_UYVY
%45 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %45, i32 0, i32 0
%47 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %46, i32 0, i32 7
store i32 %44, i32* %47
%48 = load i32, i32* %7
%49 = mul nsw i32 %48, 2
%50 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%51 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %50, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %51, i32 0, i32 2
store i32 %49, i32* %52
%53 = load i32, i32* %7
%54 = load i32, i32* %8
%55 = mul nsw i32 %53, %54
%56 = mul nsw i32 %55, 2
%57 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %57, i32 0, i32 0
%59 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %58, i32 0, i32 3
store i32 %56, i32* %59
br label %78
60: ; preds = %4
%61 = load i32, i32* @V4L2_PIX_FMT_BGR24
%62 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%63 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %62, i32 0, i32 0
%64 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %63, i32 0, i32 7
store i32 %61, i32* %64
%65 = load i32, i32* %7
%66 = mul nsw i32 %65, 3
%67 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%68 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %67, i32 0, i32 0
%69 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %68, i32 0, i32 2
store i32 %66, i32* %69
%70 = load i32, i32* %7
%71 = load i32, i32* %8
%72 = mul nsw i32 %70, %71
%73 = mul nsw i32 %72, 3
%74 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%75 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %74, i32 0, i32 0
%76 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %75, i32 0, i32 3
store i32 %73, i32* %76
br label %78
77: ; preds = %4
store i32 -1, i32* %5
br label %101
78: ; preds = %60, %43, %26
%79 = load i32, i32* @V4L2_FIELD_NONE
%80 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%81 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %80, i32 0, i32 0
%82 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %81, i32 0, i32 6
store i32 %79, i32* %82
%83 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%84 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %83, i32 0, i32 0
%85 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %84, i32 0, i32 5
store i64 0, i64* %85
%86 = load i32, i32* @V4L2_COLORSPACE_JPEG
%87 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %11, i32 0, i32 0
%88 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %87, i32 0, i32 0
%89 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %88, i32 0, i32 4
store i32 %86, i32* %89
%90 = load i8*, i8** %6
%91 = load i32, i32* @O_RDWR
%92 = call i32 @open(i8* %90, i32 %91)
store i32 %92, i32* %12
%93 = load i32, i32* %12
%94 = load i32, i32* @VIDIOC_QUERYCAP
%95 = bitcast %struct.v4l2_capability* %10 to %struct.v4l2_format*
%96 = call i32 @ioctl(i32 %93, i32 %94, %struct.v4l2_format* %95)
store i32 %96, i32* %13
%97 = load i32, i32* %12
%98 = load i32, i32* @VIDIOC_S_FMT
%99 = call i32 @ioctl(i32 %97, i32 %98, %struct.v4l2_format* %11)
store i32 %99, i32* %13
%100 = load i32, i32* %12
store i32 %100, i32* %5
br label %101
101: ; preds = %78, %77
%102 = load i32, i32* %5
ret i32 %102
}

LLVM IR (-O2):
%struct.v4l2_capability = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32, i32, i32, i32, i32, i64, i32, i32 }
%struct.v4l2_format = type { %struct.TYPE_4__, i32 }
@V4L2_BUF_TYPE_VIDEO_OUTPUT = global i32 0
@V4L2_PIX_FMT_RGB24 = global i32 0
@V4L2_PIX_FMT_UYVY = global i32 0
@V4L2_PIX_FMT_BGR24 = global i32 0
@V4L2_FIELD_NONE = global i32 0
@V4L2_COLORSPACE_JPEG = global i32 0
@O_RDWR = global i32 0
@VIDIOC_QUERYCAP = global i32 0
@VIDIOC_S_FMT = global i32 0
define i32 @open_device(i8* nocapture %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.v4l2_capability
%6 = alloca %struct.v4l2_format
%7 = bitcast %struct.v4l2_capability* %5 to i8*
%8 = bitcast %struct.v4l2_format* %6 to i8*
%9 = call i32 @memset(%struct.v4l2_format* %6, i32 0, i32 48)
%10 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_OUTPUT
%11 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 0
store i32 %1, i32* %12
%13 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 1
store i32 %2, i32* %13
switch i32 %3, label %48 [
i32 1, label %14
i32 2, label %20
i32 3, label %27
]
14: ; preds = %4
%15 = load i32, i32* @V4L2_PIX_FMT_RGB24
%16 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 7
store i32 %15, i32* %16
%17 = mul i32 %1, 3
%18 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 2
store i32 %17, i32* %18
%19 = mul i32 %17, %2
br label %33
20: ; preds = %4
%21 = load i32, i32* @V4L2_PIX_FMT_UYVY
%22 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 7
store i32 %21, i32* %22
%23 = shl nsw i32 %1, 1
%24 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 2
store i32 %23, i32* %24
%25 = mul nsw i32 %2, %1
%26 = shl nsw i32 %25, 1
br label %33
27: ; preds = %4
%28 = load i32, i32* @V4L2_PIX_FMT_BGR24
%29 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 7
store i32 %28, i32* %29
%30 = mul i32 %1, 3
%31 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 2
store i32 %30, i32* %31
%32 = mul i32 %30, %2
br label %33
33: ; preds = %27, %20, %14
%34 = phi i32 [ %32, %27 ], [ %26, %20 ], [ %19, %14 ]
%35 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 3
store i32 %34, i32* %35
%36 = load i32, i32* @V4L2_FIELD_NONE
%37 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 6
store i32 %36, i32* %37
%38 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 5
store i64 0, i64* %38
%39 = load i32, i32* @V4L2_COLORSPACE_JPEG
%40 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 4
store i32 %39, i32* %40
%41 = load i32, i32* @O_RDWR
%42 = call i32 @open(i8* %0, i32 %41)
%43 = load i32, i32* @VIDIOC_QUERYCAP
%44 = bitcast %struct.v4l2_capability* %5 to %struct.v4l2_format*
%45 = call i32 @ioctl(i32 %42, i32 %43, %struct.v4l2_format* %44)
%46 = load i32, i32* @VIDIOC_S_FMT
%47 = call i32 @ioctl(i32 %42, i32 %46, %struct.v4l2_format* %6)
br label %48
48: ; preds = %4, %33
%49 = phi i32 [ %42, %33 ], [ -1, %4 ]
ret i32 %49
}