#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_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;
  int fdwr = open(dev, O_RDWR);
  int ret_code = ioctl(fdwr, VIDIOC_QUERYCAP, &vid_caps);
  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;
  }
  vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  vid_format.fmt.pix.priv = 0;
  vid_format.fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  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,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x7c(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x80(%rbp)
lea    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x84(%rbp)
sub    $0x1,%eax
je     11c8 <open_device+0x98>
jmp    11b4 <open_device+0x84>
mov    -0x84(%rbp),%eax
sub    $0x2,%eax
je     11ed <open_device+0xbd>
jmp    120f <open_device+0xdf>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
imul   $0x3,-0xc(%rbp),%eax
mov    %eax,-0x70(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
imul   $0x3,%eax,%eax
mov    %eax,-0x6c(%rbp)
jmp    120f <open_device+0xdf>
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x70(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x6c(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x7c(%rbp),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    -0x78(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x80(%rbp)
mov    -0x7c(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_device>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r14d
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,%r15d
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0x38(%rsp),%rdx
mov    %r15d,%edi
call   1040 <ioctl@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    %r14d,0x8(%rsp)
mov    %ebp,0xc(%rsp)
cmp    $0x2,%ebx
je     11b3 <open_device+0x83>
cmp    $0x1,%ebx
jne    11d2 <open_device+0xa2>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
lea    (%r14,%r14,2),%eax
mov    %eax,0x10(%rsp)
imul   %eax,%ebp
jmp    11ce <open_device+0x9e>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
lea    (%r14,%r14,1),%eax
mov    %eax,0x10(%rsp)
imul   %r14d,%ebp
add    %ebp,%ebp
mov    %ebp,0x14(%rsp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1040 <ioctl@plt>
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
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 }
@O_RDWR = global i32 0
@VIDIOC_QUERYCAP = global i32 0
@V4L2_BUF_TYPE_VIDEO_OUTPUT = global i32 0
@V4L2_PIX_FMT_RGB24 = global i32 0
@V4L2_PIX_FMT_UYVY = global i32 0
@V4L2_FIELD_NONE = global i32 0
@V4L2_COLORSPACE_JPEG = global i32 0
@VIDIOC_S_FMT = global i32 0
define i32 @open_device(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.v4l2_capability
%10 = alloca %struct.v4l2_format
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = load i32, i32* @VIDIOC_QUERYCAP
%18 = bitcast %struct.v4l2_capability* %9 to %struct.v4l2_format*
%19 = call i32 @ioctl(i32 %16, i32 %17, %struct.v4l2_format* %18)
store i32 %19, i32* %12
%20 = call i32 @memset(%struct.v4l2_format* %10, i32 0, i32 48)
%21 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_OUTPUT
%22 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* %6
%24 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %24, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %25, i32 0, i32 0
store i32 %23, i32* %26
%27 = load i32, i32* %7
%28 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, 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 1
store i32 %27, i32* %30
%31 = load i32, i32* %8
switch i32 %31, label %66 [
i32 1, label %32
i32 2, label %49
]
32: ; preds = %4
%33 = load i32, i32* @V4L2_PIX_FMT_RGB24
%34 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %34, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %35, i32 0, i32 7
store i32 %33, i32* %36
%37 = load i32, i32* %6
%38 = mul nsw i32 %37, 3
%39 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %39, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %40, i32 0, i32 2
store i32 %38, i32* %41
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = mul nsw i32 %42, %43
%45 = mul nsw i32 %44, 3
%46 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%47 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %46, i32 0, i32 0
%48 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %47, i32 0, i32 3
store i32 %45, i32* %48
br label %66
49: ; preds = %4
%50 = load i32, i32* @V4L2_PIX_FMT_UYVY
%51 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%52 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %51, i32 0, i32 0
%53 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %52, i32 0, i32 7
store i32 %50, i32* %53
%54 = load i32, i32* %6
%55 = mul nsw i32 %54, 2
%56 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %56, i32 0, i32 0
%58 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %57, i32 0, i32 2
store i32 %55, i32* %58
%59 = load i32, i32* %6
%60 = load i32, i32* %7
%61 = mul nsw i32 %59, %60
%62 = mul nsw i32 %61, 2
%63 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%64 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %63, i32 0, i32 0
%65 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %64, i32 0, i32 3
store i32 %62, i32* %65
br label %66
66: ; preds = %4, %49, %32
%67 = load i32, i32* @V4L2_FIELD_NONE
%68 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%69 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %68, i32 0, i32 0
%70 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %69, i32 0, i32 6
store i32 %67, i32* %70
%71 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%72 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %71, i32 0, i32 0
%73 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %72, i32 0, i32 5
store i64 0, i64* %73
%74 = load i32, i32* @V4L2_COLORSPACE_JPEG
%75 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %10, i32 0, i32 0
%76 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %75, i32 0, i32 0
%77 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %76, i32 0, i32 4
store i32 %74, i32* %77
%78 = load i32, i32* %11
%79 = load i32, i32* @VIDIOC_S_FMT
%80 = call i32 @ioctl(i32 %78, i32 %79, %struct.v4l2_format* %10)
store i32 %80, i32* %12
%81 = load i32, i32* %11
ret i32 %81
}

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 }
@O_RDWR = global i32 0
@VIDIOC_QUERYCAP = global i32 0
@V4L2_BUF_TYPE_VIDEO_OUTPUT = global i32 0
@V4L2_PIX_FMT_RGB24 = global i32 0
@V4L2_PIX_FMT_UYVY = global i32 0
@V4L2_FIELD_NONE = global i32 0
@V4L2_COLORSPACE_JPEG = 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 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = load i32, i32* @VIDIOC_QUERYCAP
%12 = bitcast %struct.v4l2_capability* %5 to %struct.v4l2_format*
%13 = call i32 @ioctl(i32 %10, i32 %11, %struct.v4l2_format* %12)
%14 = call i32 @memset(%struct.v4l2_format* %6, i32 0, i32 48)
%15 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_OUTPUT
%16 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 0
store i32 %1, i32* %17
%18 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 1
store i32 %2, i32* %18
switch i32 %3, label %35 [
i32 1, label %19
i32 2, label %25
]
19: ; preds = %4
%20 = load i32, i32* @V4L2_PIX_FMT_RGB24
%21 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 7
store i32 %20, i32* %21
%22 = mul i32 %1, 3
%23 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 2
store i32 %22, i32* %23
%24 = mul i32 %22, %2
br label %32
25: ; preds = %4
%26 = load i32, i32* @V4L2_PIX_FMT_UYVY
%27 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 7
store i32 %26, i32* %27
%28 = shl nsw i32 %1, 1
%29 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 2
store i32 %28, i32* %29
%30 = mul nsw i32 %2, %1
%31 = shl nsw i32 %30, 1
br label %32
32: ; preds = %19, %25
%33 = phi i32 [ %31, %25 ], [ %24, %19 ]
%34 = getelementptr inbounds %struct.v4l2_format, %struct.v4l2_format* %6, i64 0, i32 0, i32 0, i32 3
store i32 %33, i32* %34
br label %35
35: ; preds = %32, %4
%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* @VIDIOC_S_FMT
%42 = call i32 @ioctl(i32 %10, i32 %41, %struct.v4l2_format* %6)
ret i32 %10
}