#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 */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pgm_write(int /*<<< orphan*/ *, unsigned char const *,
                             size_t, size_t);
int /*<<< orphan*/ sprintf(char *, char *, char const *, size_t);
int /*<<< orphan*/ srt_render_time(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stdout;
int save_sup_image(FILE *srt_file, size_t subtitle_num, uint32_t start_time,
                   uint32_t end_time, char *timecode_buf,
                   const char *img_base_filename, char *img_filename_buf,
                   const unsigned char *img, size_t img_width,
                   size_t img_height) {
  int result = -1;
  FILE *img_file;
  if (img == NULL) {
    return result;
  }
  sprintf(img_filename_buf, "%s%05lu.pgm", img_base_filename, subtitle_num);
  if ((img_file = fopen(img_filename_buf, "wb")) == NULL) {
    perror("main(): fopen(PGM)");
    return result;
  }
  if (!pgm_write(img_file, img, img_width, img_height)) {
    result = 0;
    fprintf(stdout, "Saving image %lu.\n\n", subtitle_num);
    fprintf(srt_file, "%lu\n", subtitle_num + 1);
    srt_render_time(start_time, timecode_buf);
    fprintf(srt_file, "%s --> ", timecode_buf);
    srt_render_time(end_time, timecode_buf);
    fprintf(srt_file, "%s\n", timecode_buf);
    fprintf(srt_file, "%s\n", img_filename_buf);
    fprintf(srt_file, "\n");
  }
  fclose(img_file);
  return result;
}


x86 assembly (clang, -O0):
<save_sup_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x28(%rbp),%rax
mov    0x20(%rbp),%rax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
cmpq   $0x0,0x18(%rbp)
jne    11bb <save_sup_image+0x4b>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12de <save_sup_image+0x16e>
mov    0x10(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe32(%rip),%rsi
call   1090 <sprintf@plt>
mov    0x10(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1208 <save_sup_image+0x98>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12de <save_sup_image+0x16e>
mov    -0x40(%rbp),%rdi
mov    0x18(%rbp),%rsi
mov    0x20(%rbp),%rdx
mov    0x28(%rbp),%rcx
call   1060 <pgm_write@plt>
cmp    $0x0,%eax
jne    12cf <save_sup_image+0x15f>
movl   $0x0,-0x34(%rbp)
mov    0x2dac(%rip),%rax
movslq (%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <srt_render_time@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <srt_render_time@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xda3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x10(%rbp),%rdx
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xd6c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_sup_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x48(%rsp),%r12
mov    $0xffffffff,%ebp
test   %r12,%r12
je     12a3 <save_sup_image+0x123>
mov    %r8,%r15
mov    %rsi,%r13
mov    %rdi,%r14
mov    %edx,(%rsp)
mov    %ecx,0x4(%rsp)
mov    0x40(%rsp),%rbx
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
mov    %r9,%rdx
mov    %r13,%rcx
call   10a0 <sprintf@plt>
lea    0xe3d(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12b4 <save_sup_image+0x134>
mov    %rax,%rbx
mov    0x58(%rsp),%rcx
mov    0x50(%rsp),%rdx
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <pgm_write@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    129b <save_sup_image+0x11b>
mov    0x2dd4(%rip),%rax
movslq (%rax),%rdi
lea    0xe0c(%rip),%rsi
xor    %ebp,%ebp
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%r13
lea    0xe09(%rip),%rsi
mov    %r14,%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    (%rsp),%edi
mov    %r15,%rsi
call   1050 <srt_render_time@plt>
lea    0xdef(%rip),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x4(%rsp),%edi
mov    %r15,%rsi
call   1050 <srt_render_time@plt>
lea    0xdd7(%rip),%r12
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    0x40(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xa,%edi
mov    %r14,%rsi
call   1040 <fputc@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd54(%rip),%rdi
call   1090 <perror@plt>
jmp    12a3 <save_sup_image+0x123>

LLVM IR (-O0):
@.str = [12 x i8] c"%s%05lu.pgm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [19 x i8] c"main(): fopen(PGM)\00"
@stdout = global i32 0
@.str.3 = [20 x i8] c"Saving image %lu.\0A\0A\00"
@.str.4 = [5 x i8] c"%lu\0A\00"
@.str.5 = [8 x i8] c"%s --> \00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @save_sup_image(i32* %0, i64 %1, i32 %2, i32 %3, i8* %4, i8* %5, i8* %6, i8* %7, i64 %8, i64 %9) {
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca i8*
%19 = alloca i8*
%20 = alloca i64
%21 = alloca i64
%22 = alloca i32
%23 = alloca i32*
store i32* %0, i32** %12
store i64 %1, i64* %13
store i32 %2, i32* %14
store i32 %3, i32* %15
store i8* %4, i8** %16
store i8* %5, i8** %17
store i8* %6, i8** %18
store i8* %7, i8** %19
store i64 %8, i64* %20
store i64 %9, i64* %21
store i32 -1, i32* %22
%24 = load i8*, i8** %19
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %10
%27 = load i32, i32* %22
store i32 %27, i32* %11
br label %77
28: ; preds = %10
%29 = load i8*, i8** %18
%30 = load i8*, i8** %17
%31 = load i64, i64* %13
%32 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %30, i64 %31)
%33 = load i8*, i8** %18
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %23
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %39
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %22
store i32 %38, i32* %11
br label %77
39: ; preds = %28
%40 = load i32*, i32** %23
%41 = load i8*, i8** %19
%42 = load i64, i64* %20
%43 = load i64, i64* %21
%44 = call i32 @pgm_write(i32* %40, i8* %41, i64 %42, i64 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %73, label %46
46: ; preds = %39
store i32 0, i32* %22
%47 = load i32, i32* @stdout
%48 = sext i32 %47 to i64
%49 = inttoptr i64 %48 to i32*
%50 = load i64, i64* %13
%51 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %50)
%52 = load i32*, i32** %12
%53 = load i64, i64* %13
%54 = add i64 %53, 1
%55 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %54)
%56 = load i32, i32* %14
%57 = load i8*, i8** %16
%58 = call i32 @srt_render_time(i32 %56, i8* %57)
%59 = load i32*, i32** %12
%60 = load i8*, i8** %16
%61 = call i32 (i32*, i8*, ...) @fprintf(i32* %59, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %60)
%62 = load i32, i32* %15
%63 = load i8*, i8** %16
%64 = call i32 @srt_render_time(i32 %62, i8* %63)
%65 = load i32*, i32** %12
%66 = load i8*, i8** %16
%67 = call i32 (i32*, i8*, ...) @fprintf(i32* %65, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %66)
%68 = load i32*, i32** %12
%69 = load i8*, i8** %18
%70 = call i32 (i32*, i8*, ...) @fprintf(i32* %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %69)
%71 = load i32*, i32** %12
%72 = call i32 (i32*, i8*, ...) @fprintf(i32* %71, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %73
73: ; preds = %46, %39
%74 = load i32*, i32** %23
%75 = call i32 @fclose(i32* %74)
%76 = load i32, i32* %22
store i32 %76, i32* %11
br label %77
77: ; preds = %73, %36, %26
%78 = load i32, i32* %11
ret i32 %78
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s%05lu.pgm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [19 x i8] c"main(): fopen(PGM)\00"
@stdout = global i32 0
@.str.3 = [20 x i8] c"Saving image %lu.\0A\0A\00"
@.str.4 = [5 x i8] c"%lu\0A\00"
@.str.5 = [8 x i8] c"%s --> \00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @save_sup_image(i32* nocapture %0, i64 %1, i32 %2, i32 %3, i8* %4, i8* %5, i8* %6, i8* %7, i64 %8, i64 %9) {
%11 = icmp eq i8* %7, null
br i1 %11, label %37, label %12
12: ; preds = %10
%13 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %5, i64 %1)
%14 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %37
18: ; preds = %12
%19 = tail call i32 @pgm_write(i32* %14, i8* %7, i64 %8, i64 %9)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = load i32, i32* @stdout
%23 = sext i32 %22 to i64
%24 = inttoptr i64 %23 to i32*
%25 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%26 = add i64 %1, 1
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %26)
%28 = tail call i32 @srt_render_time(i32 %2, i8* %4)
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%30 = tail call i32 @srt_render_time(i32 %3, i8* %4)
%31 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%32 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%33 = tail call i32 @fputc(i32 10, i32* %0)
br label %34
34: ; preds = %21, %18
%35 = phi i32 [ -1, %18 ], [ 0, %21 ]
%36 = tail call i32 @fclose(i32* %14)
br label %37
37: ; preds = %10, %34, %16
%38 = phi i32 [ -1, %16 ], [ %35, %34 ], [ -1, %10 ]
ret i32 %38
}