#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 O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(unsigned char, int /*<<< orphan*/ *);
unsigned char *malloc(int);
int open(char *, int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int write(int, unsigned char *, int);
int export_pic(unsigned char KH, unsigned char KL, unsigned char MH,
               unsigned char ML, int i) {
  int camera = open("/dev/ttyMFD1", O_RDWR | O_NOCTTY);
  if (camera == -1)
    return -1;
  unsigned char XH = 0x00;
  unsigned char XL = 0x00;
  unsigned char jpeg[16] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00,
                            MH,   ML,   0x00, 0x00, KH,   KL,   XH,   XL};
  int n = write(camera, jpeg, 16);
  int fsizeH = ((long)KH) << 8;
  int fsizeL = ((long)KL);
  fsizeH += fsizeL + 10;
  unsigned char *photo_buffer = malloc(fsizeH);
  int total = fsizeH / 2048;
  int x, aloc = 0;
  for (x = 0; x < total + 1; x++) {
    n = read(camera, photo_buffer + aloc, 2048);
    aloc += n;
  }
  char buffer[32] = {0x00};
  snprintf(buffer, sizeof(buffer), "foto%d.jpg", i);
  FILE *foto = fopen(buffer, "w");
  int m;
  for (m = 5; m < fsizeH - 5; m++)
    fputc(photo_buffer[m], foto);
  fclose(foto);
  close(camera);
  return (aloc - fsizeH);
}


x86 assembly (clang, -O0):
<export_pic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %sil,%dl
mov    %dil,%sil
mov    %sil,-0x5(%rbp)
mov    %dl,-0x6(%rbp)
mov    %cl,-0x7(%rbp)
mov    %al,-0x8(%rbp)
mov    %r8d,-0xc(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
or     (%rax),%esi
lea    0xe21(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11fd <export_pic+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1399 <export_pic+0x1f9>
movb   $0x0,-0x11(%rbp)
movb   $0x0,-0x12(%rbp)
movb   $0x56,-0x30(%rbp)
movb   $0x0,-0x2f(%rbp)
movb   $0x32,-0x2e(%rbp)
movb   $0xc,-0x2d(%rbp)
movb   $0x0,-0x2c(%rbp)
movb   $0xa,-0x2b(%rbp)
movb   $0x0,-0x2a(%rbp)
movb   $0x0,-0x29(%rbp)
mov    -0x7(%rbp),%al
mov    %al,-0x28(%rbp)
mov    -0x8(%rbp),%al
mov    %al,-0x27(%rbp)
movb   $0x0,-0x26(%rbp)
movb   $0x0,-0x25(%rbp)
mov    -0x5(%rbp),%al
mov    %al,-0x24(%rbp)
mov    -0x6(%rbp),%al
mov    %al,-0x23(%rbp)
mov    -0x11(%rbp),%al
mov    %al,-0x22(%rbp)
mov    -0x12(%rbp),%al
mov    %al,-0x21(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
movzbl -0x5(%rbp),%eax
shl    $0x8,%rax
mov    %eax,-0x38(%rbp)
movzbl -0x6(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0xa,%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%eax
mov    $0x800,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4c(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x50(%rbp)
mov    -0x50(%rbp),%eax
mov    -0x4c(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    12ee <export_pic+0x14e>
mov    -0x10(%rbp),%edi
mov    -0x48(%rbp),%rsi
movslq -0x54(%rbp),%rax
add    %rax,%rsi
mov    $0x800,%edx
call   1090 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    -0x54(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    12ab <export_pic+0x10b>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1060 <memset@plt>
lea    -0x80(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x20,%esi
lea    0xcfc(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xcf7(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x88(%rbp)
movl   $0x5,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
mov    -0x38(%rbp),%ecx
sub    $0x5,%ecx
cmp    %ecx,%eax
jge    137c <export_pic+0x1dc>
mov    -0x48(%rbp),%rax
movslq -0x8c(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x88(%rbp),%rsi
movzbl %al,%edi
call   1080 <fputc@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1337 <export_pic+0x197>
mov    -0x88(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x54(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<export_pic>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %edx,%r13d
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e36(%rip),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3c(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12ed <export_pic+0x15d>
mov    %eax,%r12d
movabs $0xa000c320056,%rax
mov    %rax,0x10(%rsp)
mov    %r13b,0x18(%rsp)
mov    %r15b,0x19(%rsp)
movw   $0x0,0x1a(%rsp)
mov    %bpl,0x1c(%rsp)
mov    %bl,0x1d(%rsp)
movw   $0x0,0x1e(%rsp)
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x10,%edx
call   1030 <write@plt>
movzbl %bpl,%eax
shl    $0x8,%eax
movzbl %bl,%ecx
lea    (%rcx,%rax,1),%r15d
add    $0xa,%r15d
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,%r13
mov    %r15d,%ebx
shr    $0xb,%ebx
add    $0x1,%ebx
xor    %ebp,%ebp
nop
movslq %ebp,%rbp
mov    %r13,%rsi
add    %rbp,%rsi
mov    %r12d,%edi
mov    $0x800,%edx
call   1080 <read@plt>
add    %eax,%ebp
add    $0xffffffff,%ebx
jne    1240 <export_pic+0xb0>
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
lea    0xd9c(%rip),%rdx
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
lea    0xd8b(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
mov    %rax,%r14
mov    %r15d,0xc(%rsp)
cmp    $0xb,%r15d
jb     12d7 <export_pic+0x147>
mov    $0xb,%ebx
cmovg  0xc(%rsp),%ebx
add    $0xfffffffb,%ebx
mov    $0x5,%r15d
cs nopw 0x0(%rax,%rax,1)
movzbl 0x0(%r13,%r15,1),%edi
mov    %r14,%rsi
call   1070 <fputc@plt>
add    $0x1,%r15
cmp    %r15,%rbx
jne    12c0 <export_pic+0x130>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r12d,%edi
call   1060 <close@plt>
sub    0xc(%rsp),%ebp
jmp    12f2 <export_pic+0x162>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyMFD1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [11 x i8] c"foto%d.jpg\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @export_pic(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8
%14 = alloca i8
%15 = alloca [16 x i8]
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i8*
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca [32 x i8]
%24 = alloca i32*
%25 = alloca i32
store i8 %0, i8* %7
store i8 %1, i8* %8
store i8 %2, i8* %9
store i8 %3, i8* %10
store i32 %4, i32* %11
%26 = load i32, i32* @O_RDWR
%27 = load i32, i32* @O_NOCTTY
%28 = or i32 %26, %27
%29 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %28)
store i32 %29, i32* %12
%30 = load i32, i32* %12
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %5
store i32 -1, i32* %6
br label %123
33: ; preds = %5
store i8 0, i8* %13
store i8 0, i8* %14
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
store i8 86, i8* %34
%35 = getelementptr inbounds i8, i8* %34, i64 1
store i8 0, i8* %35
%36 = getelementptr inbounds i8, i8* %35, i64 1
store i8 50, i8* %36
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 12, i8* %37
%38 = getelementptr inbounds i8, i8* %37, i64 1
store i8 0, i8* %38
%39 = getelementptr inbounds i8, i8* %38, i64 1
store i8 10, i8* %39
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8 0, i8* %40
%41 = getelementptr inbounds i8, i8* %40, i64 1
store i8 0, i8* %41
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = load i8, i8* %9
store i8 %43, i8* %42
%44 = getelementptr inbounds i8, i8* %42, i64 1
%45 = load i8, i8* %10
store i8 %45, i8* %44
%46 = getelementptr inbounds i8, i8* %44, i64 1
store i8 0, i8* %46
%47 = getelementptr inbounds i8, i8* %46, i64 1
store i8 0, i8* %47
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = load i8, i8* %7
store i8 %49, i8* %48
%50 = getelementptr inbounds i8, i8* %48, i64 1
%51 = load i8, i8* %8
store i8 %51, i8* %50
%52 = getelementptr inbounds i8, i8* %50, i64 1
%53 = load i8, i8* %13
store i8 %53, i8* %52
%54 = getelementptr inbounds i8, i8* %52, i64 1
%55 = load i8, i8* %14
store i8 %55, i8* %54
%56 = load i32, i32* %12
%57 = getelementptr inbounds [16 x i8], [16 x i8]* %15, i64 0, i64 0
%58 = call i32 @write(i32 %56, i8* %57, i32 16)
store i32 %58, i32* %16
%59 = load i8, i8* %7
%60 = zext i8 %59 to i64
%61 = shl i64 %60, 8
%62 = trunc i64 %61 to i32
store i32 %62, i32* %17
%63 = load i8, i8* %8
%64 = zext i8 %63 to i64
%65 = trunc i64 %64 to i32
store i32 %65, i32* %18
%66 = load i32, i32* %18
%67 = add nsw i32 %66, 10
%68 = load i32, i32* %17
%69 = add nsw i32 %68, %67
store i32 %69, i32* %17
%70 = load i32, i32* %17
%71 = call i8* @malloc(i32 %70)
store i8* %71, i8** %19
%72 = load i32, i32* %17
%73 = sdiv i32 %72, 2048
store i32 %73, i32* %20
store i32 0, i32* %22
store i32 0, i32* %21
br label %74
74: ; preds = %89, %33
%75 = load i32, i32* %21
%76 = load i32, i32* %20
%77 = add nsw i32 %76, 1
%78 = icmp slt i32 %75, %77
br i1 %78, label %79, label %92
79: ; preds = %74
%80 = load i32, i32* %12
%81 = load i8*, i8** %19
%82 = load i32, i32* %22
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %81, i64 %83
%85 = call i32 @read(i32 %80, i8* %84, i32 2048)
store i32 %85, i32* %16
%86 = load i32, i32* %16
%87 = load i32, i32* %22
%88 = add nsw i32 %87, %86
store i32 %88, i32* %22
br label %89
89: ; preds = %79
%90 = load i32, i32* %21
%91 = add nsw i32 %90, 1
store i32 %91, i32* %21
br label %74
92: ; preds = %74
%93 = bitcast [32 x i8]* %23 to i8*
%94 = getelementptr inbounds [32 x i8], [32 x i8]* %23, i64 0, i64 0
%95 = load i32, i32* %11
%96 = call i32 @snprintf(i8* %94, i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %95)
%97 = getelementptr inbounds [32 x i8], [32 x i8]* %23, i64 0, i64 0
%98 = call i32* @fopen(i8* %97, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %98, i32** %24
store i32 5, i32* %25
br label %99
99: ; preds = %112, %92
%100 = load i32, i32* %25
%101 = load i32, i32* %17
%102 = sub nsw i32 %101, 5
%103 = icmp slt i32 %100, %102
br i1 %103, label %104, label %115
104: ; preds = %99
%105 = load i8*, i8** %19
%106 = load i32, i32* %25
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %105, i64 %107
%109 = load i8, i8* %108
%110 = load i32*, i32** %24
%111 = call i32 @fputc(i8 zeroext %109, i32* %110)
br label %112
112: ; preds = %104
%113 = load i32, i32* %25
%114 = add nsw i32 %113, 1
store i32 %114, i32* %25
br label %99
115: ; preds = %99
%116 = load i32*, i32** %24
%117 = call i32 @fclose(i32* %116)
%118 = load i32, i32* %12
%119 = call i32 @close(i32 %118)
%120 = load i32, i32* %22
%121 = load i32, i32* %17
%122 = sub nsw i32 %120, %121
store i32 %122, i32* %6
br label %123
123: ; preds = %115, %32
%124 = load i32, i32* %6
ret i32 %124
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyMFD1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [11 x i8] c"foto%d.jpg\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @export_pic(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i32 %4) {
%6 = alloca [16 x i8]
%7 = alloca [32 x i8]
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %9, %8
%11 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %67, label %13
13: ; preds = %5
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
store i8 86, i8* %14
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 1
store i8 0, i8* %15
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 2
store i8 50, i8* %16
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 3
store i8 12, i8* %17
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 4
store i8 0, i8* %18
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 5
store i8 10, i8* %19
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 6
store i8 0, i8* %20
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 7
store i8 0, i8* %21
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 8
store i8 %2, i8* %22
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 9
store i8 %3, i8* %23
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 10
store i8 0, i8* %24
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 11
store i8 0, i8* %25
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 12
store i8 %0, i8* %26
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 13
store i8 %1, i8* %27
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 14
store i8 0, i8* %28
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 15
store i8 0, i8* %29
%30 = call i32 @write(i32 %11, i8* %14, i32 16)
%31 = zext i8 %0 to i32
%32 = shl nuw nsw i32 %31, 8
%33 = zext i8 %1 to i32
%34 = add nuw nsw i32 %33, 10
%35 = add nuw nsw i32 %34, %32
%36 = tail call i8* @malloc(i32 %35)
%37 = lshr i32 %35, 11
br label %38
38: ; preds = %13, %38
%39 = phi i32 [ 0, %13 ], [ %44, %38 ]
%40 = phi i32 [ 0, %13 ], [ %45, %38 ]
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %36, i64 %41
%43 = tail call i32 @read(i32 %11, i8* %42, i32 2048)
%44 = add nsw i32 %43, %39
%45 = add nuw nsw i32 %40, 1
%46 = icmp eq i32 %40, %37
br i1 %46, label %47, label %38
47: ; preds = %38
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%49 = call i32 @snprintf(i8* %48, i32 32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%50 = call i32* @fopen(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%51 = icmp ugt i32 %35, 10
br i1 %51, label %52, label %63
52: ; preds = %47
%53 = call i32 @llvm.smax.i32(i32 %35, i32 11)
%54 = add nsw i32 %53, -5
%55 = zext i32 %54 to i64
br label %56
56: ; preds = %52, %56
%57 = phi i64 [ 5, %52 ], [ %61, %56 ]
%58 = getelementptr inbounds i8, i8* %36, i64 %57
%59 = load i8, i8* %58
%60 = call i32 @fputc(i8 zeroext %59, i32* %50)
%61 = add nuw nsw i64 %57, 1
%62 = icmp eq i64 %61, %55
br i1 %62, label %63, label %56
63: ; preds = %56, %47
%64 = call i32 @fclose(i32* %50)
%65 = call i32 @close(i32 %11)
%66 = sub nsw i32 %44, %35
br label %67
67: ; preds = %5, %63
%68 = phi i32 [ %66, %63 ], [ -1, %5 ]
ret i32 %68
}