#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 video_still_picture {
  char *iFrame;
  int size;
};
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ VIDEO_STILLPICTURE;
int /*<<< orphan*/ fstat(int, struct stat *);
int ioctl(int, int /*<<< orphan*/, struct video_still_picture *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int videoStillPicture(int fd, char *arg) {
  int sifd;
  struct stat st;
  struct video_still_picture sp;
  if (!arg)
    return -1;
  sifd = open(arg, O_RDONLY);
  if (sifd == -1) {
    perror("open stillimage file");
    printf("(%s)\n", arg);
    return 0;
  }
  fstat(sifd, &st);
  sp.iFrame = (char *)malloc(st.st_size);
  sp.size = st.st_size;
  printf("I-frame size: %d\n", sp.size);
  if (!sp.iFrame) {
    printf("No memory for I-Frame\n");
    return 0;
  }
  printf("read: %d bytes\n", (int)read(sifd, sp.iFrame, sp.size));
  if (ioctl(fd, VIDEO_STILLPICTURE, &sp) == -1)
    perror("VIDEO_STILLPICTURE");
  return 0;
}


x86 assembly (clang, -O0):
<videoStillPicture>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <videoStillPicture+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <videoStillPicture+0x113>
mov    -0x10(%rbp),%rdi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11df <videoStillPicture+0x6f>
lea    0xe44(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1283 <videoStillPicture+0x113>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x18(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1233 <videoStillPicture+0xc3>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1283 <videoStillPicture+0x113>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <read@plt>
mov    %eax,%esi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    127c <videoStillPicture+0x10c>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<videoStillPicture>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
test   %rsi,%rsi
je     1239 <videoStillPicture+0xb9>
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     125d <videoStillPicture+0xdd>
mov    %eax,%r12d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    %rax,0x10(%rsp)
mov    %ebx,0x18(%rsp)
lea    0xe39(%rip),%rdi
xor    %r15d,%r15d
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %rbp,%rbp
je     1241 <videoStillPicture+0xc1>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
lea    0xe26(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %r14d,%edi
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    124d <videoStillPicture+0xcd>
lea    0xe0b(%rip),%rdi
call   1090 <perror@plt>
jmp    124d <videoStillPicture+0xcd>
mov    $0xffffffff,%r15d
jmp    124d <videoStillPicture+0xcd>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9c(%rip),%rdi
call   1090 <perror@plt>
lea    0xda5(%rip),%rdi
xor    %r15d,%r15d
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    124d <videoStillPicture+0xcd>

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.video_still_picture = type { i8*, i32 }
@O_RDONLY = global i32 0
@.str = [21 x i8] c"open stillimage file\00"
@.str.1 = [6 x i8] c"(%s)\0A\00"
@.str.2 = [18 x i8] c"I-frame size: %d\0A\00"
@.str.3 = [23 x i8] c"No memory for I-Frame\0A\00"
@.str.4 = [16 x i8] c"read: %d bytes\0A\00"
@VIDEO_STILLPICTURE = global i32 0
@.str.5 = [19 x i8] c"VIDEO_STILLPICTURE\00"
define i32 @videoStillPicture(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca %struct.video_still_picture
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i32 -1, i32* %3
br label %57
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %5
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 0, i32* %3
br label %57
22: ; preds = %12
%23 = load i32, i32* %6
%24 = call i32 @fstat(i32 %23, %struct.stat* %7)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
%29 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %8, i32 0, i32 0
store i8* %28, i8** %29
%30 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %8, i32 0, i32 1
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %8, i32 0, i32 1
%34 = load i32, i32* %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %8, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = icmp ne i8* %37, null
br i1 %38, label %41, label %39
39: ; preds = %22
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %57
41: ; preds = %22
%42 = load i32, i32* %6
%43 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %8, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %8, i32 0, i32 1
%46 = load i32, i32* %45
%47 = call i64 @read(i32 %42, i8* %44, i32 %46)
%48 = trunc i64 %47 to i32
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %4
%51 = load i32, i32* @VIDEO_STILLPICTURE
%52 = call i32 @ioctl(i32 %50, i32 %51, %struct.video_still_picture* %8)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %41
%55 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %54, %41
store i32 0, i32* %3
br label %57
57: ; preds = %56, %39, %18, %11
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.video_still_picture = type { i8*, i32 }
@O_RDONLY = global i32 0
@.str = [21 x i8] c"open stillimage file\00"
@.str.1 = [6 x i8] c"(%s)\0A\00"
@.str.2 = [18 x i8] c"I-frame size: %d\0A\00"
@.str.4 = [16 x i8] c"read: %d bytes\0A\00"
@VIDEO_STILLPICTURE = global i32 0
@.str.5 = [19 x i8] c"VIDEO_STILLPICTURE\00"
@str = [22 x i8] c"No memory for I-Frame\00"
define i32 @videoStillPicture(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.video_still_picture
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.video_still_picture* %4 to i8*
%7 = icmp eq i8* %1, null
br i1 %7, label %36, label %8
8: ; preds = %2
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* %1, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %36
15: ; preds = %8
%16 = call i32 @fstat(i32 %10, %struct.stat* %3)
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%18 = load i32, i32* %17
%19 = tail call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
%21 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %4, i64 0, i32 0
store i8* %20, i8** %21
%22 = getelementptr inbounds %struct.video_still_picture, %struct.video_still_picture* %4, i64 0, i32 1
store i32 %18, i32* %22
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%24 = icmp eq i64 %19, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %36
27: ; preds = %15
%28 = tail call i64 @read(i32 %10, i8* %20, i32 %18)
%29 = trunc i64 %28 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @VIDEO_STILLPICTURE
%32 = call i32 @ioctl(i32 %0, i32 %31, %struct.video_still_picture* %4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %27, %34, %2, %25, %12
%37 = phi i32 [ 0, %12 ], [ 0, %25 ], [ -1, %2 ], [ 0, %34 ], [ 0, %27 ]
ret i32 %37
}