#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_buffer {
  int /*<<< orphan*/ memory;
  int /*<<< orphan*/ type;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ V4L2_BUF_TYPE_VIDEO_CAPTURE;
int /*<<< orphan*/ V4L2_MEMORY_MMAP;
int /*<<< orphan*/ VIDIOC_DQBUF;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct v4l2_buffer *, int /*<<< orphan*/, int);
int xioctl(int, int /*<<< orphan*/, struct v4l2_buffer *);
int read_frame(int fd, struct v4l2_buffer *buf) {
  if (fd < 0 || buf == NULL) {
    errno = EINVAL;
    return -1;
  }
  memset(buf, 0, sizeof(struct v4l2_buffer));
  buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  buf->memory = V4L2_MEMORY_MMAP;
  return xioctl(fd, VIDIOC_DQBUF, buf);
}


x86 assembly (clang, -O0):
<read_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1144 <read_frame+0x24>
cmpq   $0x0,-0x10(%rbp)
jne    1162 <read_frame+0x42>
mov    0x2e7d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7c(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <read_frame+0x89>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    0x2e4f(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1040 <xioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_frame>:
push   %rbp
push   %rbx
push   %rax
test   %edi,%edi
js     1170 <read_frame+0x50>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1170 <read_frame+0x50>
mov    %edi,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2ea9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rbx)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rbx)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <xioctl@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.v4l2_buffer = type { i32, i32 }
@EINVAL = global i32 0
@errno = global i32 0
@V4L2_BUF_TYPE_VIDEO_CAPTURE = global i32 0
@V4L2_MEMORY_MMAP = global i32 0
@VIDIOC_DQBUF = global i32 0
define i32 @read_frame(i32 %0, %struct.v4l2_buffer* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.v4l2_buffer*
store i32 %0, i32* %4
store %struct.v4l2_buffer* %1, %struct.v4l2_buffer** %5
%6 = load i32, i32* %4
%7 = icmp slt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load %struct.v4l2_buffer*, %struct.v4l2_buffer** %5
%10 = icmp eq %struct.v4l2_buffer* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8, %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i32 -1, i32* %3
br label %26
13: ; preds = %8
%14 = load %struct.v4l2_buffer*, %struct.v4l2_buffer** %5
%15 = call i32 @memset(%struct.v4l2_buffer* %14, i32 0, i32 8)
%16 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_CAPTURE
%17 = load %struct.v4l2_buffer*, %struct.v4l2_buffer** %5
%18 = getelementptr inbounds %struct.v4l2_buffer, %struct.v4l2_buffer* %17, i32 0, i32 1
store i32 %16, i32* %18
%19 = load i32, i32* @V4L2_MEMORY_MMAP
%20 = load %struct.v4l2_buffer*, %struct.v4l2_buffer** %5
%21 = getelementptr inbounds %struct.v4l2_buffer, %struct.v4l2_buffer* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = load i32, i32* %4
%23 = load i32, i32* @VIDIOC_DQBUF
%24 = load %struct.v4l2_buffer*, %struct.v4l2_buffer** %5
%25 = call i32 @xioctl(i32 %22, i32 %23, %struct.v4l2_buffer* %24)
store i32 %25, i32* %3
br label %26
26: ; preds = %13, %11
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
%struct.v4l2_buffer = type { i32, i32 }
@EINVAL = global i32 0
@errno = global i32 0
@V4L2_BUF_TYPE_VIDEO_CAPTURE = global i32 0
@V4L2_MEMORY_MMAP = global i32 0
@VIDIOC_DQBUF = global i32 0
define i32 @read_frame(i32 %0, %struct.v4l2_buffer* %1) {
%3 = icmp slt i32 %0, 0
%4 = icmp eq %struct.v4l2_buffer* %1, null
%5 = or i1 %3, %4
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %16
8: ; preds = %2
%9 = tail call i32 @memset(%struct.v4l2_buffer* %1, i32 0, i32 8)
%10 = load i32, i32* @V4L2_BUF_TYPE_VIDEO_CAPTURE
%11 = getelementptr inbounds %struct.v4l2_buffer, %struct.v4l2_buffer* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @V4L2_MEMORY_MMAP
%13 = getelementptr inbounds %struct.v4l2_buffer, %struct.v4l2_buffer* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @VIDIOC_DQBUF
%15 = tail call i32 @xioctl(i32 %0, i32 %14, %struct.v4l2_buffer* %1)
br label %16
16: ; preds = %8, %6
%17 = phi i32 [ -1, %6 ], [ %15, %8 ]
ret i32 %17
}