#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*/ response;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ errno;
int /*<<< orphan*/ error(int, scalar_t__, char *, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char, unsigned char,
                            ...);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int comm_command(int tty, unsigned char command, unsigned char argument,
                 int (*response_callback)(char *, size_t)) {
  char cmd[16], response[64];
  int cmd_size, response_size, read_timeout = 0;
  if (argument > 0) {
    snprintf(cmd, sizeof(cmd),
             "\xE6\xE6\xE6\xE6"
             "%c"
             "%c"
             "%c"
             "%c",
             command, argument, command ^ 0xFF, argument ^ 0xFF);
    cmd_size = 8;
  } else if (command > 0) {
    snprintf(cmd, sizeof(cmd),
             "\xE6\xE6\xE6\xE6"
             "%c"
             "%c"
             "\xFF",
             command, command ^ 0xFF);
    cmd_size = 7;
  } else {
    memcpy(cmd, "\xE6\xE6\xE6\xE6\xE6\xE6\xE6\xE6\x00\xFF\xFF", 11);
    cmd_size = 11;
  }
  if (write(tty, cmd, cmd_size) == -1)
    error(1, errno, "%s.%d: write()",
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/"
          "comm.c",
          46);
  usleep(10000);
read_again:
  response_size = read(tty, response, sizeof(response));
  if (response_size == -1) {
    if (errno == EAGAIN) {
      if (read_timeout)
        error(1, 0, "%s.%d: Camera not responding.",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/"
              "comm.c",
              56);
      else {
        read_timeout = 1;
        sleep(3);
        goto read_again;
      }
    } else
      error(1, errno, "%s.%d: read()",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/"
            "comm.c",
            64);
  }
  if (response_callback != NULL)
    return response_callback(response, response_size);
  return 0;
}


x86 assembly (clang, -O0):
<comm_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %dl,%al
mov    %sil,%dl
mov    %edi,-0x8(%rbp)
mov    %dl,-0x9(%rbp)
mov    %al,-0xa(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x7c(%rbp)
movzbl -0xa(%rbp),%eax
cmp    $0x0,%eax
jle    11ea <comm_command+0x7a>
lea    -0x30(%rbp),%rdi
mov    -0x9(%rbp),%cl
mov    -0xa(%rbp),%r8b
movzbl -0x9(%rbp),%r9d
xor    $0xff,%r9d
movzbl -0xa(%rbp),%eax
xor    $0xff,%eax
mov    $0x10,%esi
lea    0xe33(%rip),%rdx
movzbl %cl,%ecx
movzbl %r8b,%r8d
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <snprintf@plt>
movl   $0x8,-0x74(%rbp)
jmp    124e <comm_command+0xde>
movzbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jle    122d <comm_command+0xbd>
lea    -0x30(%rbp),%rdi
mov    -0x9(%rbp),%cl
movzbl -0x9(%rbp),%eax
xor    $0xff,%eax
mov    $0x10,%esi
lea    0xdfa(%rip),%rdx
movzbl %cl,%ecx
movzbl %al,%r8d
mov    $0x0,%al
call   1040 <snprintf@plt>
movl   $0x7,-0x74(%rbp)
jmp    1249 <comm_command+0xd9>
lea    -0x30(%rbp),%rdi
lea    0xea1(%rip),%rsi
mov    $0xb,%edx
call   1060 <memcpy@plt>
movl   $0xb,-0x74(%rbp)
jmp    124e <comm_command+0xde>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x74(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    128e <comm_command+0x11e>
mov    0x2d73(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%edi
lea    0xd9b(%rip),%rdx
lea    0xda3(%rip),%rcx
mov    $0x2e,%r8d
call   1070 <error@plt>
mov    $0x2710,%edi
call   1090 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1050 <read@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0xffffffff,-0x78(%rbp)
jne    1349 <comm_command+0x1d9>
mov    0x2d23(%rip),%rax
mov    (%rax),%rax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%rax
jne    131c <comm_command+0x1ac>
cmpl   $0x0,-0x7c(%rbp)
je     1301 <comm_command+0x191>
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%esi
lea    0xdc3(%rip),%rdx
lea    0xd35(%rip),%rcx
mov    $0x38,%r8d
call   1070 <error@plt>
jmp    1317 <comm_command+0x1a7>
movl   $0x1,-0x7c(%rbp)
mov    $0x3,%edi
call   1080 <sleep@plt>
jmp    1298 <comm_command+0x128>
jmp    1344 <comm_command+0x1d4>
mov    0x2cbd(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%edi
lea    0xd99(%rip),%rdx
lea    0xced(%rip),%rcx
mov    $0x40,%r8d
call   1070 <error@plt>
jmp    1349 <comm_command+0x1d9>
cmpq   $0x0,-0x18(%rbp)
je     136a <comm_command+0x1fa>
mov    -0x18(%rbp),%rax
lea    -0x70(%rbp),%rdi
movslq -0x78(%rbp),%rsi
call   *%rax
mov    %eax,-0x4(%rbp)
jmp    1371 <comm_command+0x201>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<comm_command>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rcx,%r14
mov    %edi,%ebp
test   %edx,%edx
je     11b9 <comm_command+0x49>
movzbl %sil,%ecx
not    %sil
movzbl %sil,%r9d
movzbl %dl,%r8d
not    %dl
movzbl %dl,%eax
mov    %eax,(%rsp)
lea    0xe5f(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    $0x8,%ebx
jmp    1203 <comm_command+0x93>
test   %sil,%sil
je     11e8 <comm_command+0x78>
movzbl %sil,%ecx
not    %sil
movzbl %sil,%r8d
lea    0xe3d(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x10,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    $0x7,%ebx
jmp    1203 <comm_command+0x93>
lea    0xeea(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    $0xb,%ebx
mov    $0xb,%edx
call   1060 <memcpy@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    123e <comm_command+0xce>
mov    0x2dc3(%rip),%rax
mov    (%rax),%rsi
lea    0xdf0(%rip),%rdx
lea    0xdf8(%rip),%rcx
mov    $0x1,%edi
mov    $0x2e,%r8d
call   1070 <error@plt>
mov    $0x2710,%edi
call   1090 <usleep@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0x40,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    12dc <comm_command+0x16c>
mov    0x2d7b(%rip),%r15
mov    (%r15),%rsi
mov    0x2d61(%rip),%rbx
cmp    (%rbx),%rsi
jne    12b9 <comm_command+0x149>
mov    $0x3,%edi
call   1080 <sleep@plt>
lea    0x20(%rsp),%rsi
mov    %ebp,%edi
mov    $0x40,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    12dc <comm_command+0x16c>
mov    (%r15),%rsi
cmp    (%rbx),%rsi
jne    12b9 <comm_command+0x149>
lea    0xe0a(%rip),%rdx
lea    0xd7c(%rip),%rcx
mov    $0x1,%edi
xor    %esi,%esi
mov    $0x38,%r8d
jmp    12d2 <comm_command+0x162>
lea    0xe0b(%rip),%rdx
lea    0xd5f(%rip),%rcx
mov    $0x1,%edi
mov    $0x40,%r8d
call   1070 <error@plt>
mov    $0xffffffff,%eax
test   %r14,%r14
je     12ee <comm_command+0x17e>
movslq %eax,%rsi
lea    0x20(%rsp),%rdi
call   *%r14
jmp    12f0 <comm_command+0x180>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"\E6\E6\E6\E6%c%c%c%c\00"
@.str.1 = [10 x i8] c"\E6\E6\E6\E6%c%c\FF\00"
@.str.2 = [12 x i8] c"\E6\E6\E6\E6\E6\E6\E6\E6\00\FF\FF\00"
@errno = global i64 0
@.str.3 = [15 x i8] c"%s.%d: write()\00"
@.str.4 = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/comm.c\00"
@EAGAIN = global i64 0
@.str.5 = [30 x i8] c"%s.%d: Camera not responding.\00"
@.str.6 = [14 x i8] c"%s.%d: read()\00"
define i32 @comm_command(i32 %0, i8 zeroext %1, i8 zeroext %2, i32 (i8*, i64)* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i8
%9 = alloca i32 (i8*, i64)*
%10 = alloca [16 x i8]
%11 = alloca [64 x i8]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i32 (i8*, i64)* %3, i32 (i8*, i64)** %9
store i32 0, i32* %14
%15 = load i8, i8* %8
%16 = zext i8 %15 to i32
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %4
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%20 = load i8, i8* %7
%21 = load i8, i8* %8
%22 = load i8, i8* %7
%23 = zext i8 %22 to i32
%24 = xor i32 %23, 255
%25 = load i8, i8* %8
%26 = zext i8 %25 to i32
%27 = xor i32 %26, 255
%28 = call i32 (i8*, i32, i8*, i8, i8, ...) @snprintf(i8* %19, i32 16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8 zeroext %20, i8 zeroext %21, i32 %24, i32 %27)
store i32 8, i32* %12
br label %45
29: ; preds = %4
%30 = load i8, i8* %7
%31 = zext i8 %30 to i32
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%35 = load i8, i8* %7
%36 = load i8, i8* %7
%37 = zext i8 %36 to i32
%38 = xor i32 %37, 255
%39 = trunc i32 %38 to i8
%40 = call i32 (i8*, i32, i8*, i8, i8, ...) @snprintf(i8* %34, i32 16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %35, i8 zeroext %39)
store i32 7, i32* %12
br label %44
41: ; preds = %29
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%43 = call i32 @memcpy(i8* %42, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
store i32 11, i32* %12
br label %44
44: ; preds = %41, %33
br label %45
45: ; preds = %44, %18
%46 = load i32, i32* %6
%47 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%48 = load i32, i32* %12
%49 = call i32 @write(i32 %46, i8* %47, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %45
%52 = load i64, i64* @errno
%53 = call i32 @error(i32 1, i64 %52, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 46)
br label %54
54: ; preds = %51, %45
%55 = call i32 @usleep(i32 10000)
br label %56
56: ; preds = %71, %54
%57 = load i32, i32* %6
%58 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 64)
store i32 %59, i32* %13
%60 = load i32, i32* %13
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %78
62: ; preds = %56
%63 = load i64, i64* @errno
%64 = load i64, i64* @EAGAIN
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %74
66: ; preds = %62
%67 = load i32, i32* %14
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = call i32 @error(i32 1, i64 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 56)
br label %73
71: ; preds = %66
store i32 1, i32* %14
%72 = call i32 @sleep(i32 3)
br label %56
73: ; preds = %69
br label %77
74: ; preds = %62
%75 = load i64, i64* @errno
%76 = call i32 @error(i32 1, i64 %75, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 64)
br label %77
77: ; preds = %74, %73
br label %78
78: ; preds = %77, %56
%79 = load i32 (i8*, i64)*, i32 (i8*, i64)** %9
%80 = icmp ne i32 (i8*, i64)* %79, null
br i1 %80, label %81, label %87
81: ; preds = %78
%82 = load i32 (i8*, i64)*, i32 (i8*, i64)** %9
%83 = getelementptr inbounds [64 x i8], [64 x i8]* %11, i64 0, i64 0
%84 = load i32, i32* %13
%85 = sext i32 %84 to i64
%86 = call i32 %82(i8* %83, i64 %85)
store i32 %86, i32* %5
br label %88
87: ; preds = %78
store i32 0, i32* %5
br label %88
88: ; preds = %87, %81
%89 = load i32, i32* %5
ret i32 %89
}

LLVM IR (-O2):
@.str = [13 x i8] c"\E6\E6\E6\E6%c%c%c%c\00"
@.str.1 = [10 x i8] c"\E6\E6\E6\E6%c%c\FF\00"
@.str.2 = [12 x i8] c"\E6\E6\E6\E6\E6\E6\E6\E6\00\FF\FF\00"
@errno = global i64 0
@.str.3 = [15 x i8] c"%s.%d: write()\00"
@.str.4 = [135 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/kobolt/polaroid-320/refs/heads/master/comm.c\00"
@EAGAIN = global i64 0
@.str.5 = [30 x i8] c"%s.%d: Camera not responding.\00"
@.str.6 = [14 x i8] c"%s.%d: read()\00"
define i32 @comm_command(i32 %0, i8 zeroext %1, i8 zeroext %2, i32 (i8*, i64)* %3) {
%5 = alloca [16 x i8]
%6 = alloca [64 x i8]
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %6, i64 0, i64 0
%9 = icmp eq i8 %2, 0
br i1 %9, label %16, label %10
10: ; preds = %4
%11 = xor i8 %1, -1
%12 = zext i8 %11 to i32
%13 = xor i8 %2, -1
%14 = zext i8 %13 to i32
%15 = call i32 (i8*, i32, i8*, i8, i8, ...) @snprintf(i8* %7, i32 16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8 zeroext %1, i8 zeroext %2, i32 %12, i32 %14)
br label %23
16: ; preds = %4
%17 = icmp eq i8 %1, 0
br i1 %17, label %21, label %18
18: ; preds = %16
%19 = xor i8 %1, -1
%20 = call i32 (i8*, i32, i8*, i8, i8, ...) @snprintf(i8* %7, i32 16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %1, i8 zeroext %19)
br label %23
21: ; preds = %16
%22 = call i32 @memcpy(i8* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
br label %23
23: ; preds = %18, %21, %10
%24 = phi i32 [ 8, %10 ], [ 7, %18 ], [ 11, %21 ]
%25 = call i32 @write(i32 %0, i8* %7, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = call i32 @error(i32 1, i64 %28, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 46)
br label %30
30: ; preds = %27, %23
%31 = call i32 @usleep(i32 10000)
%32 = call i32 @read(i32 %0, i8* %8, i32 64)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %51
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EAGAIN
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %48
38: ; preds = %34
%39 = call i32 @sleep(i32 3)
%40 = call i32 @read(i32 %0, i8* %8, i32 64)
%41 = icmp eq i32 %40, -1
br i1 %41, label %44, label %51
42: ; preds = %44
%43 = call i32 @error(i32 1, i64 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 56)
br label %51
44: ; preds = %38
%45 = load i64, i64* @errno
%46 = load i64, i64* @EAGAIN
%47 = icmp eq i64 %45, %46
br i1 %47, label %42, label %48
48: ; preds = %44, %34
%49 = phi i64 [ %35, %34 ], [ %45, %44 ]
%50 = call i32 @error(i32 1, i64 %49, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i32 64)
br label %51
51: ; preds = %38, %30, %42, %48
%52 = phi i32 [ -1, %42 ], [ -1, %48 ], [ %32, %30 ], [ %40, %38 ]
%53 = icmp eq i32 (i8*, i64)* %3, null
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = sext i32 %52 to i64
%56 = call i32 %3(i8* %8, i64 %55)
br label %57
57: ; preds = %51, %54
%58 = phi i32 [ %56, %54 ], [ 0, %51 ]
ret i32 %58
}