#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 /*<<< orphan*/ camserv_log(char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int manager_new_picture(char *, int, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int sscanf(char const *, char *, int *);
int dispatch_pictaker(const char *cmdbuf, const char *picture_mem) {
  int command;
  if (sscanf(cmdbuf, "%d", &command) != 1) {
    camserv_log("mainloop", "Parse error reading data from pictaker!");
    return -1;
  }
  if (command == 0) {
    char *new_picture_data;
    int nbytes;
    sscanf(cmdbuf, "%*d %d", &nbytes);
    if ((new_picture_data = malloc(nbytes)) == NULL) {
      camserv_log("mainloop", "Error allocating memory to manage picture!");
      return -1;
    }
    memcpy(new_picture_data, picture_mem, nbytes);
    if (manager_new_picture(new_picture_data, nbytes, 100) == -1) {
      camserv_log("mainloop", "Unable to manage picture!");
      free(new_picture_data);
      return -1;
    }
  } else {
    camserv_log("mainloop", "Unknown pictaker dispatch command: %d", command);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<dispatch_pictaker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xf25(%rip),%rsi
lea    -0x1c(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
je     11ae <dispatch_pictaker+0x4e>
lea    0xe6c(%rip),%rdi
lea    0xe6e(%rip),%rsi
mov    $0x0,%al
call   1050 <camserv_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <dispatch_pictaker+0x127>
cmpl   $0x0,-0x1c(%rbp)
jne    125c <dispatch_pictaker+0xfc>
mov    -0x10(%rbp),%rdi
lea    0xe6e(%rip),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x2c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1203 <dispatch_pictaker+0xa3>
lea    0xe17(%rip),%rdi
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1050 <camserv_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <dispatch_pictaker+0x127>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    $0x64,%edx
call   1040 <manager_new_picture@plt>
cmp    $0xffffffff,%eax
jne    1257 <dispatch_pictaker+0xf7>
lea    0xdcc(%rip),%rdi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <camserv_log@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <dispatch_pictaker+0x127>
jmp    1280 <dispatch_pictaker+0x120>
mov    -0x1c(%rbp),%edx
lea    0xd9a(%rip),%rdi
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1050 <camserv_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <dispatch_pictaker+0x127>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dispatch_pictaker>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xf2a(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1080 <sscanf@plt>
cmp    $0x1,%eax
jne    11a4 <dispatch_pictaker+0x44>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     11c8 <dispatch_pictaker+0x68>
lea    0xe6c(%rip),%rdi
lea    0xee2(%rip),%rsi
xor    %eax,%eax
call   1050 <camserv_log@plt>
jmp    11b9 <dispatch_pictaker+0x59>
lea    0xe55(%rip),%rdi
lea    0xe57(%rip),%rsi
xor    %eax,%eax
call   1050 <camserv_log@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe62(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1080 <sscanf@plt>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1216 <dispatch_pictaker+0xb6>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
mov    0x8(%rsp),%esi
mov    %r14,%rdi
mov    $0x64,%edx
call   1040 <manager_new_picture@plt>
cmp    $0xffffffff,%eax
je     1226 <dispatch_pictaker+0xc6>
xor    %eax,%eax
jmp    11be <dispatch_pictaker+0x5e>
lea    0xde3(%rip),%rdi
lea    0xe14(%rip),%rsi
jmp    11b2 <dispatch_pictaker+0x52>
lea    0xdd3(%rip),%rdi
lea    0xe2f(%rip),%rsi
xor    %eax,%eax
call   1050 <camserv_log@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11b9 <dispatch_pictaker+0x59>

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c"mainloop\00"
@.str.2 = [40 x i8] c"Parse error reading data from pictaker!\00"
@.str.3 = [7 x i8] c"%*d %d\00"
@.str.4 = [43 x i8] c"Error allocating memory to manage picture!\00"
@.str.5 = [26 x i8] c"Unable to manage picture!\00"
@.str.6 = [38 x i8] c"Unknown pictaker dispatch command: %d\00"
define i32 @dispatch_pictaker(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
14: ; preds = %2
%15 = load i32, i32* %6
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %39
17: ; preds = %14
%18 = load i8*, i8** %4
%19 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* %8)
%20 = load i32, i32* %8
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %7
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
25: ; preds = %17
%26 = load i8*, i8** %7
%27 = load i8*, i8** %5
%28 = load i32, i32* %8
%29 = call i32 @memcpy(i8* %26, i8* %27, i32 %28)
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = call i32 @manager_new_picture(i8* %30, i32 %31, i32 100)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%36 = load i8*, i8** %7
%37 = call i32 @free(i8* %36)
store i32 -1, i32* %3
br label %43
38: ; preds = %25
br label %42
39: ; preds = %14
%40 = load i32, i32* %6
%41 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i32 %40)
store i32 -1, i32* %3
br label %43
42: ; preds = %38
store i32 0, i32* %3
br label %43
43: ; preds = %42, %39, %34, %23, %12
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [9 x i8] c"mainloop\00"
@.str.2 = [40 x i8] c"Parse error reading data from pictaker!\00"
@.str.3 = [7 x i8] c"%*d %d\00"
@.str.4 = [43 x i8] c"Error allocating memory to manage picture!\00"
@.str.5 = [26 x i8] c"Unable to manage picture!\00"
@.str.6 = [38 x i8] c"Unknown pictaker dispatch command: %d\00"
define i32 @dispatch_pictaker(i8* nocapture %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%7 = icmp eq i32 %6, 1
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %33
10: ; preds = %2
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = bitcast i32* %4 to i8*
%15 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%16 = load i32, i32* %4
%17 = call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0))
br label %29
21: ; preds = %13
%22 = call i32 @memcpy(i8* %17, i8* %1, i32 %16)
%23 = load i32, i32* %4
%24 = call i32 @manager_new_picture(i8* %17, i32 %23, i32 100)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @free(i8* %17)
br label %29
29: ; preds = %26, %19
br label %33
30: ; preds = %21
br label %33
31: ; preds = %10
%32 = call i32 (i8*, i8*, ...) @camserv_log(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0), i32 %11)
br label %33
33: ; preds = %30, %29, %31, %8
%34 = phi i32 [ -1, %8 ], [ -1, %31 ], [ 0, %30 ], [ -1, %29 ]
ret i32 %34
}