#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 */

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
unsigned char *malloc(int);
int open(char *, int);
int read(int, unsigned char *, int);
int write(int, unsigned char *, int);
int export_buf(unsigned char KH, unsigned char KL, unsigned char MH,
               unsigned char ML, unsigned char **photo_buffer, int *size) {
  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;
  *photo_buffer = malloc(fsizeH);
  *size = 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;
  }
  close(camera);
  return (aloc - *size);
}


x86 assembly (clang, -O0):
<export_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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    %r8,-0x10(%rbp)
mov    %r9,-0x18(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
lea    0xe70(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ae <export_buf+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c5 <export_buf+0x175>
movb   $0x0,-0x1d(%rbp)
movb   $0x0,-0x1e(%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    -0x1d(%rbp),%al
mov    %al,-0x22(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x21(%rbp)
mov    -0x1c(%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   1060 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%eax
mov    $0x800,%ecx
cltd
idiv   %ecx
mov    %eax,-0x40(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    -0x40(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    12b1 <export_buf+0x161>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
movslq -0x48(%rbp),%rax
add    %rax,%rsi
mov    $0x800,%edx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    126b <export_buf+0x11b>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x48(%rbp),%eax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<export_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,%r15
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e71(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe77(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     124a <export_buf+0xfa>
mov    %eax,%r12d
movabs $0xa000c320056,%rax
mov    %rax,(%rsp)
mov    %r14b,0x8(%rsp)
mov    %r13b,0x9(%rsp)
movw   $0x0,0xa(%rsp)
mov    %bpl,0xc(%rsp)
mov    %bl,0xd(%rsp)
movw   $0x0,0xe(%rsp)
mov    %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),%ebp
add    $0xa,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,(%r15)
mov    0x10(%rsp),%r14
mov    %ebp,(%r14)
mov    %r12d,%edi
mov    %rax,%rsi
mov    $0x800,%edx
call   1050 <read@plt>
mov    %eax,%ebx
cmp    $0x800,%ebp
jb     123d <export_buf+0xed>
shr    $0xb,%ebp
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rbx
mov    (%r15),%rsi
add    %rbx,%rsi
mov    %r12d,%edi
mov    $0x800,%edx
call   1050 <read@plt>
add    %eax,%ebx
add    $0xffffffff,%ebp
jne    1220 <export_buf+0xd0>
mov    %r12d,%edi
call   1040 <close@plt>
sub    (%r14),%ebx
jmp    124f <export_buf+0xff>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%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
define i32 @export_buf(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8** %4, i32* %5) {
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8
%11 = alloca i8
%12 = alloca i8**
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i8
%16 = alloca i8
%17 = alloca [16 x i8]
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
store i8 %0, i8* %8
store i8 %1, i8* %9
store i8 %2, i8* %10
store i8 %3, i8* %11
store i8** %4, i8*** %12
store i32* %5, i32** %13
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %24, %25
%27 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 %27, i32* %14
%28 = load i32, i32* %14
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %31
30: ; preds = %6
store i32 -1, i32* %7
br label %101
31: ; preds = %6
store i8 0, i8* %15
store i8 0, i8* %16
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %17, i64 0, i64 0
store i8 86, i8* %32
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8 0, i8* %33
%34 = getelementptr inbounds i8, i8* %33, i64 1
store i8 50, i8* %34
%35 = getelementptr inbounds i8, i8* %34, i64 1
store i8 12, i8* %35
%36 = getelementptr inbounds i8, i8* %35, i64 1
store i8 0, i8* %36
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 10, 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 0, i8* %39
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %10
store i8 %41, i8* %40
%42 = getelementptr inbounds i8, i8* %40, i64 1
%43 = load i8, i8* %11
store i8 %43, i8* %42
%44 = getelementptr inbounds i8, i8* %42, i64 1
store i8 0, i8* %44
%45 = getelementptr inbounds i8, i8* %44, i64 1
store i8 0, i8* %45
%46 = getelementptr inbounds i8, i8* %45, i64 1
%47 = load i8, i8* %8
store i8 %47, i8* %46
%48 = getelementptr inbounds i8, i8* %46, i64 1
%49 = load i8, i8* %9
store i8 %49, i8* %48
%50 = getelementptr inbounds i8, i8* %48, i64 1
%51 = load i8, i8* %15
store i8 %51, i8* %50
%52 = getelementptr inbounds i8, i8* %50, i64 1
%53 = load i8, i8* %16
store i8 %53, i8* %52
%54 = load i32, i32* %14
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %17, i64 0, i64 0
%56 = call i32 @write(i32 %54, i8* %55, i32 16)
store i32 %56, i32* %18
%57 = load i8, i8* %8
%58 = zext i8 %57 to i64
%59 = shl i64 %58, 8
%60 = trunc i64 %59 to i32
store i32 %60, i32* %19
%61 = load i8, i8* %9
%62 = zext i8 %61 to i64
%63 = trunc i64 %62 to i32
store i32 %63, i32* %20
%64 = load i32, i32* %20
%65 = add nsw i32 %64, 10
%66 = load i32, i32* %19
%67 = add nsw i32 %66, %65
store i32 %67, i32* %19
%68 = load i32, i32* %19
%69 = call i8* @malloc(i32 %68)
%70 = load i8**, i8*** %12
store i8* %69, i8** %70
%71 = load i32, i32* %19
%72 = load i32*, i32** %13
store i32 %71, i32* %72
%73 = load i32, i32* %19
%74 = sdiv i32 %73, 2048
store i32 %74, i32* %21
store i32 0, i32* %23
store i32 0, i32* %22
br label %75
75: ; preds = %91, %31
%76 = load i32, i32* %22
%77 = load i32, i32* %21
%78 = add nsw i32 %77, 1
%79 = icmp slt i32 %76, %78
br i1 %79, label %80, label %94
80: ; preds = %75
%81 = load i32, i32* %14
%82 = load i8**, i8*** %12
%83 = load i8*, i8** %82
%84 = load i32, i32* %23
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %83, i64 %85
%87 = call i32 @read(i32 %81, i8* %86, i32 2048)
store i32 %87, i32* %18
%88 = load i32, i32* %18
%89 = load i32, i32* %23
%90 = add nsw i32 %89, %88
store i32 %90, i32* %23
br label %91
91: ; preds = %80
%92 = load i32, i32* %22
%93 = add nsw i32 %92, 1
store i32 %93, i32* %22
br label %75
94: ; preds = %75
%95 = load i32, i32* %14
%96 = call i32 @close(i32 %95)
%97 = load i32, i32* %23
%98 = load i32*, i32** %13
%99 = load i32, i32* %98
%100 = sub nsw i32 %97, %99
store i32 %100, i32* %7
br label %101
101: ; preds = %94, %30
%102 = load i32, i32* %7
ret i32 %102
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyMFD1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
define i32 @export_buf(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8** nocapture %4, i32* nocapture %5) {
%7 = alloca [16 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 %55, label %13
13: ; preds = %6
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
store i8 86, i8* %14
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 1
store i8 0, i8* %15
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 2
store i8 50, i8* %16
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 3
store i8 12, i8* %17
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 4
store i8 0, i8* %18
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 5
store i8 10, i8* %19
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 6
store i8 0, i8* %20
%21 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 7
store i8 0, i8* %21
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 8
store i8 %2, i8* %22
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 9
store i8 %3, i8* %23
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 10
store i8 0, i8* %24
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 11
store i8 0, i8* %25
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 12
store i8 %0, i8* %26
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 13
store i8 %1, i8* %27
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 14
store i8 0, i8* %28
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %7, 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)
store i8* %36, i8** %4
store i32 %35, i32* %5
%37 = lshr i32 %35, 11
%38 = tail call i32 @read(i32 %11, i8* %36, i32 2048)
%39 = icmp ult i32 %35, 2048
br i1 %39, label %50, label %40
40: ; preds = %13, %40
%41 = phi i32 [ %48, %40 ], [ %38, %13 ]
%42 = phi i32 [ %43, %40 ], [ 0, %13 ]
%43 = add nuw nsw i32 %42, 1
%44 = load i8*, i8** %4
%45 = sext i32 %41 to i64
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = tail call i32 @read(i32 %11, i8* %46, i32 2048)
%48 = add nsw i32 %47, %41
%49 = icmp eq i32 %43, %37
br i1 %49, label %50, label %40
50: ; preds = %40, %13
%51 = phi i32 [ %38, %13 ], [ %48, %40 ]
%52 = tail call i32 @close(i32 %11)
%53 = load i32, i32* %5
%54 = sub nsw i32 %51, %53
br label %55
55: ; preds = %6, %50
%56 = phi i32 [ %54, %50 ], [ -1, %6 ]
ret i32 %56
}