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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(unsigned char, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
void buf_to_pic(unsigned char **photo_buffer, int size, int num) {
  char buffer[32] = {0x00};
  snprintf(buffer, sizeof(buffer), "foto%d.jpg", num);
  FILE *foto = fopen(buffer, "w");
  int x;
  for (x = 5; x < size - 5; x++)
    fputc((*photo_buffer)[x], foto);
  fclose(foto);
}


x86 assembly (clang, -O0):
<buf_to_pic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%ecx
mov    $0x20,%esi
lea    0xe7b(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
movl   $0x5,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x5,%ecx
cmp    %ecx,%eax
jge    11de <buf_to_pic+0x8e>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1060 <fputc@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11a5 <buf_to_pic+0x55>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_to_pic>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ecx
mov    %esi,%r15d
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe9a(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%r12
cmp    $0xb,%r15d
jl     11b8 <buf_to_pic+0x78>
add    $0xfffffffb,%r15d
mov    $0x5,%ebx
nopw   0x0(%rax,%rax,1)
mov    (%r14),%rax
movzbl (%rax,%rbx,1),%edi
mov    %r12,%rsi
call   1050 <fputc@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11a0 <buf_to_pic+0x60>
mov    %r12,%rdi
call   1030 <fclose@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"foto%d.jpg\00"
@.str.1 = [2 x i8] c"w\00"
define void @buf_to_pic(i8** %0, i32 %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca i32*
%9 = alloca i32
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = bitcast [32 x i8]* %7 to i8*
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%12 = load i32, i32* %6
%13 = call i32 @snprintf(i8* %11, i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %8
store i32 5, i32* %9
br label %16
16: ; preds = %30, %3
%17 = load i32, i32* %9
%18 = load i32, i32* %5
%19 = sub nsw i32 %18, 5
%20 = icmp slt i32 %17, %19
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = load i8**, i8*** %4
%23 = load i8*, i8** %22
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = load i32*, i32** %8
%29 = call i32 @fputc(i8 zeroext %27, i32* %28)
br label %30
30: ; preds = %21
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 1
store i32 %32, i32* %9
br label %16
33: ; preds = %16
%34 = load i32*, i32** %8
%35 = call i32 @fclose(i32* %34)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"foto%d.jpg\00"
@.str.1 = [2 x i8] c"w\00"
define void @buf_to_pic(i8** nocapture %0, i32 %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %2)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp sgt i32 %1, 10
br i1 %8, label %9, label %20
9: ; preds = %3
%10 = add nsw i32 %1, -5
%11 = zext i32 %10 to i64
br label %12
12: ; preds = %9, %12
%13 = phi i64 [ 5, %9 ], [ %18, %12 ]
%14 = load i8*, i8** %0
%15 = getelementptr inbounds i8, i8* %14, i64 %13
%16 = load i8, i8* %15
%17 = call i32 @fputc(i8 zeroext %16, i32* %7)
%18 = add nuw nsw i64 %13, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %20, label %12
20: ; preds = %12, %3
%21 = call i32 @fclose(i32* %7)
ret void
}