#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*/ packet4;
typedef int /*<<< orphan*/ packet3;
typedef int /*<<< orphan*/ packet2;
typedef int /*<<< orphan*/ packet1;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
char *malloc(size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, size_t);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char const *argv[]) {
  int fd = open("/dev/usb_accessory", O_RDWR);
  size_t offset = 0;
  size_t bytes_read;
  int i = 0;
  static size_t bsize = 100;
  static char *buffer = NULL;
  char packet1[] = "\x2D\x05\x03\x02";
  char packet2[] = "\x29\x05\x01\x02";
  char packet3[] = "\x65\x03\x00\x00\x09\x2D\x05\x03\x02\x04";
  char packet4[] = "\x65\x03\x00\x00\x09\x2D\x05\x01\x02\x04";
  if (buffer == NULL) {
    bsize = 1024;
    if ((buffer = malloc(bsize)) == NULL)
      printf("Error with buffer\n");
  }
  write(fd, packet3, sizeof(packet3));
  read(fd, buffer, bsize);
  printf("result: %d\n", i);
  sleep(5);
  write(fd, packet4, sizeof(packet4));
  read(fd, buffer, bsize);
  printf("result: %d\n", i);
  sleep(5);
  while (1) {
    write(fd, packet1, sizeof(packet1));
    read(fd, buffer, bsize);
    printf("result: %d\n", i);
    sleep(1);
    write(fd, packet2, sizeof(packet2));
    read(fd, buffer, bsize);
    printf("result: %d\n", i);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0xe70(%rip),%eax
mov    %eax,-0x31(%rbp)
mov    0xe6b(%rip),%al
mov    %al,-0x2d(%rbp)
mov    0xe63(%rip),%eax
mov    %eax,-0x36(%rbp)
mov    0xe5e(%rip),%al
mov    %al,-0x32(%rbp)
mov    0xe74(%rip),%rax
mov    %rax,-0x41(%rbp)
mov    0xe71(%rip),%ax
mov    %ax,-0x39(%rbp)
mov    0xe69(%rip),%al
mov    %al,-0x37(%rbp)
mov    0xe60(%rip),%rax
mov    %rax,-0x4c(%rbp)
mov    0xe5d(%rip),%ax
mov    %ax,-0x44(%rbp)
mov    0xe55(%rip),%al
mov    %al,-0x42(%rbp)
cmpq   $0x0,0x2e61(%rip)
jne    1248 <main+0xe8>
movq   $0x400,0x2e38(%rip)
mov    0x2e31(%rip),%rdi
call   1060 <malloc@plt>
mov    %rax,0x2e3d(%rip)
cmp    $0x0,%rax
jne    1243 <main+0xe3>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1248 <main+0xe8>
mov    -0x14(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
mov    0x2e05(%rip),%rsi
mov    0x2de6(%rip),%rdx
call   1050 <read@plt>
mov    -0x2c(%rbp),%esi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%edi
lea    -0x4c(%rbp),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
mov    0x2dc3(%rip),%rsi
mov    0x2da4(%rip),%rdx
call   1050 <read@plt>
mov    -0x2c(%rbp),%esi
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
mov    0x2d81(%rip),%rsi
mov    0x2d62(%rip),%rdx
call   1050 <read@plt>
mov    -0x2c(%rbp),%esi
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x14(%rbp),%edi
lea    -0x36(%rbp),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
mov    0x2d3f(%rip),%rsi
mov    0x2d20(%rip),%rdx
call   1050 <read@plt>
mov    -0x2c(%rbp),%esi
lea    0xcf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    12cc <main+0x16c>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe73(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
movb   $0x0,0xc(%rsp)
movl   $0x203052d,0x8(%rsp)
02
movb   $0x0,0x4(%rsp)
movl   $0x2010529,(%rsp)
movabs $0x3052d0900000365,%rax
2d 05 03
mov    %rax,0x20(%rsp)
movl   $0x40203,0x27(%rsp)
movabs $0x1052d0900000365,%rax
2d 05 01
mov    %rax,0x10(%rsp)
movl   $0x40201,0x17(%rsp)
cmpq   $0x0,0x2e8d(%rip)
jne    120e <main+0x9e>
movb   $0x1,0x2e7c(%rip)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,0x2e73(%rip)
test   %rax,%rax
jne    120e <main+0x9e>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0xb,%edx
call   1040 <write@plt>
mov    0x2e4a(%rip),%rsi
cmpb   $0x0,0x2e3b(%rip)
mov    $0x400,%r13d
mov    $0x64,%r15d
mov    $0x64,%edx
cmovne %r13,%rdx
mov    %ebx,%edi
call   1060 <read@plt>
lea    0xdcd(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xb,%edx
call   1040 <write@plt>
mov    0x2df2(%rip),%rsi
cmpb   $0x0,0x2de3(%rip)
cmovne %r13,%r15
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <read@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
lea    0x8(%rsp),%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x5,%edx
call   1040 <write@plt>
cmpb   $0x0,0x2d92(%rip)
mov    0x2d93(%rip),%rsi
mov    $0x64,%edx
cmovne %r13,%rdx
mov    %ebx,%edi
call   1060 <read@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x5,%edx
call   1040 <write@plt>
cmpb   $0x0,0x2d4f(%rip)
mov    0x2d50(%rip),%rsi
mov    $0x64,%edx
cmovne %r13,%rdx
mov    %ebx,%edi
call   1060 <read@plt>
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    12c0 <main+0x150>

LLVM IR (-O0):
@.str = [19 x i8] c"/dev/usb_accessory\00"
@O_RDWR = global i32 0
@main.bsize = internal global i64 100
@main.buffer = internal global i8* null
@__const.main.packet1 = [5 x i8] c"-\05\03\02\00"
@__const.main.packet2 = [5 x i8] c")\05\01\02\00"
@__const.main.packet3 = [11 x i8] c"e\03\00\00\09-\05\03\02\04\00"
@__const.main.packet4 = [11 x i8] c"e\03\00\00\09-\05\01\02\04\00"
@.str.1 = [19 x i8] c"Error with buffer\0A\00"
@.str.2 = [12 x i8] c"result: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca [5 x i8]
%11 = alloca [5 x i8]
%12 = alloca [11 x i8]
%13 = alloca [11 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @O_RDWR
%15 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
store i64 0, i64* %7
store i32 0, i32* %9
%16 = bitcast [5 x i8]* %10 to i8*
%17 = bitcast [5 x i8]* %11 to i8*
%18 = bitcast [11 x i8]* %12 to i8*
%19 = bitcast [11 x i8]* %13 to i8*
%20 = load i8*, i8** @main.buffer
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %29
22: ; preds = %2
store i64 1024, i64* @main.bsize
%23 = load i64, i64* @main.bsize
%24 = call i8* @malloc(i64 %23)
store i8* %24, i8** @main.buffer
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
br label %29
29: ; preds = %28, %2
%30 = load i32, i32* %6
%31 = getelementptr inbounds [11 x i8], [11 x i8]* %12, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 11)
%33 = load i32, i32* %6
%34 = load i8*, i8** @main.buffer
%35 = load i64, i64* @main.bsize
%36 = call i32 @read(i32 %33, i8* %34, i64 %35)
%37 = load i32, i32* %9
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = call i32 @sleep(i32 5)
%40 = load i32, i32* %6
%41 = getelementptr inbounds [11 x i8], [11 x i8]* %13, i64 0, i64 0
%42 = call i32 @write(i32 %40, i8* %41, i32 11)
%43 = load i32, i32* %6
%44 = load i8*, i8** @main.buffer
%45 = load i64, i64* @main.bsize
%46 = call i32 @read(i32 %43, i8* %44, i64 %45)
%47 = load i32, i32* %9
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = call i32 @sleep(i32 5)
br label %50
50: ; preds = %29, %50
%51 = load i32, i32* %6
%52 = getelementptr inbounds [5 x i8], [5 x i8]* %10, i64 0, i64 0
%53 = call i32 @write(i32 %51, i8* %52, i32 5)
%54 = load i32, i32* %6
%55 = load i8*, i8** @main.buffer
%56 = load i64, i64* @main.bsize
%57 = call i32 @read(i32 %54, i8* %55, i64 %56)
%58 = load i32, i32* %9
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %58)
%60 = call i32 @sleep(i32 1)
%61 = load i32, i32* %6
%62 = getelementptr inbounds [5 x i8], [5 x i8]* %11, i64 0, i64 0
%63 = call i32 @write(i32 %61, i8* %62, i32 5)
%64 = load i32, i32* %6
%65 = load i8*, i8** @main.buffer
%66 = load i64, i64* @main.bsize
%67 = call i32 @read(i32 %64, i8* %65, i64 %66)
%68 = load i32, i32* %9
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %68)
%70 = call i32 @sleep(i32 1)
br label %50
}

LLVM IR (-O2):
@.str = [19 x i8] c"/dev/usb_accessory\00"
@O_RDWR = global i32 0
@main.bsize = internal unnamed_addr global i1 false
@main.buffer = internal unnamed_addr global i8* null
@__const.main.packet1 = [5 x i8] c"-\05\03\02\00"
@__const.main.packet2 = [5 x i8] c")\05\01\02\00"
@__const.main.packet3 = [11 x i8] c"e\03\00\00\09-\05\03\02\04\00"
@__const.main.packet4 = [11 x i8] c"e\03\00\00\09-\05\01\02\04\00"
@.str.2 = [12 x i8] c"result: %d\0A\00"
@str = [18 x i8] c"Error with buffer\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [5 x i8]
%4 = alloca [5 x i8]
%5 = alloca [11 x i8]
%6 = alloca [11 x i8]
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [11 x i8], [11 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 0
%13 = load i8*, i8** @main.buffer
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %2
store i1 true, i1* @main.bsize
%16 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
store i8* %16, i8** @main.buffer
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %15, %18, %2
%21 = call i32 @write(i32 %8, i8* %11, i32 11)
%22 = load i8*, i8** @main.buffer
%23 = load i1, i1* @main.bsize
%24 = select i1 %23, i64 1024, i64 100
%25 = tail call i32 @read(i32 %8, i8* %22, i64 %24)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 0)
%27 = tail call i32 @sleep(i32 5)
%28 = call i32 @write(i32 %8, i8* %12, i32 11)
%29 = load i8*, i8** @main.buffer
%30 = load i1, i1* @main.bsize
%31 = select i1 %30, i64 1024, i64 100
%32 = tail call i32 @read(i32 %8, i8* %29, i64 %31)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 0)
%34 = tail call i32 @sleep(i32 5)
br label %35
35: ; preds = %35, %20
%36 = call i32 @write(i32 %8, i8* %9, i32 5)
%37 = load i8*, i8** @main.buffer
%38 = load i1, i1* @main.bsize
%39 = select i1 %38, i64 1024, i64 100
%40 = tail call i32 @read(i32 %8, i8* %37, i64 %39)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 0)
%42 = tail call i32 @sleep(i32 1)
%43 = call i32 @write(i32 %8, i8* %10, i32 5)
%44 = load i8*, i8** @main.buffer
%45 = load i1, i1* @main.bsize
%46 = select i1 %45, i64 1024, i64 100
%47 = tail call i32 @read(i32 %8, i8* %44, i64 %46)
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 0)
%49 = tail call i32 @sleep(i32 1)
br label %35
}