#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*/ string;
typedef int /*<<< orphan*/ enable;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ framer_close_p300(int);
int /*<<< orphan*/ framer_set_result(int);
int /*<<< orphan*/ logIT(int /*<<< orphan*/, char *);
int /*<<< orphan*/ my_send(int, char *, int);
int receive_nb(int, char *, int, unsigned long *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int framer_open_p300(int fd) {
  char string[100];
  int i;
  char rbuf = 0;
  char enable[] = {0x16, 0x00, 0x00};
  ;
  unsigned long etime;
  int rlen;
  if (!framer_close_p300(fd)) {
    snprintf(string, sizeof(string), ">FRAMER: could not set start cond");
    logIT(LOG_ERR, string);
    return 0;
  }
  for (i = 0; i < 3; i++) {
    if (!my_send(fd, enable, sizeof(enable))) {
      framer_set_result(0x15);
      snprintf(string, sizeof(string), ">FRAMER: enable not send");
      logIT(LOG_ERR, string);
      return 0;
      ;
    }
    etime = 0;
    rlen = receive_nb(fd, &rbuf, 1, &etime);
    if (rlen < 0) {
      framer_set_result(0x15);
      snprintf(string, sizeof(string), ">FRAMER: enable read failure");
      logIT(LOG_ERR, string);
      return 0;
    } else if (rlen == 0) {
      framer_set_result(0x15);
      snprintf(string, sizeof(string), ">FRAMER: enable read timeout for ack");
      logIT(LOG_ERR, string);
      return 0;
    } else if (rbuf == 0x06) {
      framer_set_result(0x06);
      snprintf(string, sizeof(string), ">FRAMER: opened");
      logIT(LOG_INFO, string);
      return 1;
    }
  }
  framer_set_result(0x15);
  snprintf(string, sizeof(string), ">FRAMER: could not close (%d attempts)", 3);
  logIT(LOG_ERR, string);
  return 0;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x0,-0x75(%rbp)
mov    0xe87(%rip),%ax
mov    %ax,-0x78(%rbp)
mov    0xe7f(%rip),%al
mov    %al,-0x76(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <framer_close_p300@plt>
cmp    $0x0,%eax
jne    11cc <framer_open_p300+0x6c>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xe5c(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1080 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    1399 <framer_open_p300+0x239>
movl   $0x0,-0x74(%rbp)
cmpl   $0x3,-0x74(%rbp)
jge    135a <framer_open_p300+0x1fa>
mov    -0x8(%rbp),%edi
lea    -0x78(%rbp),%rsi
mov    $0x3,%edx
call   1050 <my_send@plt>
cmp    $0x0,%eax
jne    1236 <framer_open_p300+0xd6>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xe14(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1080 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    1399 <framer_open_p300+0x239>
movq   $0x0,-0x80(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x75(%rbp),%rsi
mov    $0x1,%edx
lea    -0x80(%rbp),%rcx
call   1070 <receive_nb@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    12a5 <framer_open_p300+0x145>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xdbe(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1080 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    1399 <framer_open_p300+0x239>
cmpl   $0x0,-0x84(%rbp)
jne    12f1 <framer_open_p300+0x191>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xd8f(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1080 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    1399 <framer_open_p300+0x239>
movsbl -0x75(%rbp),%eax
cmp    $0x6,%eax
jne    133d <framer_open_p300+0x1dd>
mov    $0x6,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xd68(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1080 <logIT@plt>
movl   $0x1,-0x4(%rbp)
jmp    1399 <framer_open_p300+0x239>
jmp    1342 <framer_open_p300+0x1e2>
jmp    1347 <framer_open_p300+0x1e7>
jmp    134c <framer_open_p300+0x1ec>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    11d3 <framer_open_p300+0x73>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xd1c(%rip),%rdx
mov    $0x3,%ecx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1080 <logIT@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbx
sub    $0x80,%rsp
mov    %edi,%ebx
movb   $0x0,0x3(%rsp)
movb   $0x0,0x6(%rsp)
movw   $0x16,0x4(%rsp)
call   1040 <framer_close_p300@plt>
test   %eax,%eax
je     1208 <framer_open_p300+0xa8>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <my_send@plt>
test   %eax,%eax
je     12d5 <framer_open_p300+0x175>
movq   $0x0,0x8(%rsp)
lea    0x3(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <receive_nb@plt>
test   %eax,%eax
js     12e8 <framer_open_p300+0x188>
je     12fb <framer_open_p300+0x19b>
cmpb   $0x6,0x3(%rsp)
jne    1214 <framer_open_p300+0xb4>
mov    $0x6,%edi
call   1030 <framer_set_result@plt>
lea    0xe9a(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    $0x1,%ebx
mov    0x2ddd(%rip),%rax
jmp    1326 <framer_open_p300+0x1c6>
lea    0xdf4(%rip),%rdx
jmp    130c <framer_open_p300+0x1ac>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <my_send@plt>
test   %eax,%eax
je     12d5 <framer_open_p300+0x175>
movq   $0x0,0x8(%rsp)
lea    0x3(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <receive_nb@plt>
test   %eax,%eax
js     12e8 <framer_open_p300+0x188>
je     12fb <framer_open_p300+0x19b>
cmpb   $0x6,0x3(%rsp)
je     11d5 <framer_open_p300+0x75>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <my_send@plt>
test   %eax,%eax
je     12d5 <framer_open_p300+0x175>
movq   $0x0,0x8(%rsp)
lea    0x3(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1,%edx
call   1070 <receive_nb@plt>
test   %eax,%eax
js     12e8 <framer_open_p300+0x188>
je     12fb <framer_open_p300+0x19b>
cmpb   $0x6,0x3(%rsp)
je     11d5 <framer_open_p300+0x75>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xdd5(%rip),%rdx
xor    %ebx,%ebx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
mov    $0x3,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    131f <framer_open_p300+0x1bf>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xd3f(%rip),%rdx
jmp    130c <framer_open_p300+0x1ac>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xd45(%rip),%rdx
jmp    130c <framer_open_p300+0x1ac>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xd4f(%rip),%rdx
xor    %ebx,%ebx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x2caa(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   1080 <logIT@plt>
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.framer_open_p300.enable = [3 x i8] c"\16\00\00"
@.str = [34 x i8] c">FRAMER: could not set start cond\00"
@LOG_ERR = global i32 0
@.str.1 = [25 x i8] c">FRAMER: enable not send\00"
@.str.2 = [29 x i8] c">FRAMER: enable read failure\00"
@.str.3 = [37 x i8] c">FRAMER: enable read timeout for ack\00"
@.str.4 = [16 x i8] c">FRAMER: opened\00"
@LOG_INFO = global i32 0
@.str.5 = [39 x i8] c">FRAMER: could not close (%d attempts)\00"
define i32 @framer_open_p300(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i8
%7 = alloca [3 x i8]
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %3
store i8 0, i8* %6
%10 = bitcast [3 x i8]* %7 to i8*
%11 = load i32, i32* %3
%12 = call i32 @framer_close_p300(i32 %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %1
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%16 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %15, i32 100, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @LOG_ERR
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%19 = call i32 @logIT(i32 %17, i8* %18)
store i32 0, i32* %2
br label %82
20: ; preds = %1
store i32 0, i32* %5
br label %21
21: ; preds = %72, %20
%22 = load i32, i32* %5
%23 = icmp slt i32 %22, 3
br i1 %23, label %24, label %75
24: ; preds = %21
%25 = load i32, i32* %3
%26 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%27 = call i32 @my_send(i32 %25, i8* %26, i32 3)
%28 = icmp ne i32 %27, 0
br i1 %28, label %36, label %29
29: ; preds = %24
%30 = call i32 @framer_set_result(i32 21)
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%32 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %31, i32 100, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @LOG_ERR
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%35 = call i32 @logIT(i32 %33, i8* %34)
store i32 0, i32* %2
br label %82
36: ; preds = %24
store i64 0, i64* %8
%37 = load i32, i32* %3
%38 = call i32 @receive_nb(i32 %37, i8* %6, i32 1, i64* %8)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %48
41: ; preds = %36
%42 = call i32 @framer_set_result(i32 21)
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%44 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %43, i32 100, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @LOG_ERR
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%47 = call i32 @logIT(i32 %45, i8* %46)
store i32 0, i32* %2
br label %82
48: ; preds = %36
%49 = load i32, i32* %9
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = call i32 @framer_set_result(i32 21)
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%54 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %53, i32 100, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* @LOG_ERR
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%57 = call i32 @logIT(i32 %55, i8* %56)
store i32 0, i32* %2
br label %82
58: ; preds = %48
%59 = load i8, i8* %6
%60 = sext i8 %59 to i32
%61 = icmp eq i32 %60, 6
br i1 %61, label %62, label %69
62: ; preds = %58
%63 = call i32 @framer_set_result(i32 6)
%64 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%65 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %64, i32 100, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%66 = load i32, i32* @LOG_INFO
%67 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%68 = call i32 @logIT(i32 %66, i8* %67)
store i32 1, i32* %2
br label %82
69: ; preds = %58
br label %70
70: ; preds = %69
br label %71
71: ; preds = %70
br label %72
72: ; preds = %71
%73 = load i32, i32* %5
%74 = add nsw i32 %73, 1
store i32 %74, i32* %5
br label %21
75: ; preds = %21
%76 = call i32 @framer_set_result(i32 21)
%77 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%78 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %77, i32 100, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 3)
%79 = load i32, i32* @LOG_ERR
%80 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%81 = call i32 @logIT(i32 %79, i8* %80)
store i32 0, i32* %2
br label %82
82: ; preds = %75, %62, %51, %41, %29, %14
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
@__const.framer_open_p300.enable = [3 x i8] c"\16\00\00"
@.str = [34 x i8] c">FRAMER: could not set start cond\00"
@LOG_ERR = global i32 0
@.str.1 = [25 x i8] c">FRAMER: enable not send\00"
@.str.2 = [29 x i8] c">FRAMER: enable read failure\00"
@.str.3 = [37 x i8] c">FRAMER: enable read timeout for ack\00"
@.str.4 = [16 x i8] c">FRAMER: opened\00"
@LOG_INFO = global i32 0
@.str.5 = [39 x i8] c">FRAMER: could not close (%d attempts)\00"
define i32 @framer_open_p300(i32 %0) {
%2 = alloca [100 x i8]
%3 = alloca i8
%4 = alloca [3 x i8]
%5 = alloca i64
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
store i8 0, i8* %3
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%8 = bitcast i64* %5 to i8*
%9 = tail call i32 @framer_close_p300(i32 %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = call i32 @my_send(i32 %0, i8* %7, i32 3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %41, label %44
14: ; preds = %1
%15 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %61
16: ; preds = %55
%17 = call i32 @my_send(i32 %0, i8* %7, i32 3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %41, label %19
19: ; preds = %16
store i64 0, i64* %5
%20 = call i32 @receive_nb(i32 %0, i8* %3, i32 1, i64* %5)
%21 = icmp slt i32 %20, 0
br i1 %21, label %47, label %22
22: ; preds = %19
%23 = icmp eq i32 %20, 0
br i1 %23, label %52, label %24
24: ; preds = %22
%25 = load i8, i8* %3
%26 = icmp eq i8 %25, 6
br i1 %26, label %58, label %27
27: ; preds = %24
%28 = call i32 @my_send(i32 %0, i8* %7, i32 3)
%29 = icmp eq i32 %28, 0
br i1 %29, label %41, label %30
30: ; preds = %27
store i64 0, i64* %5
%31 = call i32 @receive_nb(i32 %0, i8* %3, i32 1, i64* %5)
%32 = icmp slt i32 %31, 0
br i1 %32, label %47, label %33
33: ; preds = %30
%34 = icmp eq i32 %31, 0
br i1 %34, label %52, label %35
35: ; preds = %33
%36 = load i8, i8* %3
%37 = icmp eq i8 %36, 6
br i1 %37, label %58, label %38
38: ; preds = %35
%39 = call i32 @framer_set_result(i32 21)
%40 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0), i32 3)
br label %61
41: ; preds = %27, %16, %11
%42 = call i32 @framer_set_result(i32 21)
%43 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %61
44: ; preds = %11
store i64 0, i64* %5
%45 = call i32 @receive_nb(i32 %0, i8* %3, i32 1, i64* %5)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %30, %19, %44
%48 = call i32 @framer_set_result(i32 21)
%49 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %61
50: ; preds = %44
%51 = icmp eq i32 %45, 0
br i1 %51, label %52, label %55
52: ; preds = %33, %22, %50
%53 = call i32 @framer_set_result(i32 21)
%54 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %61
55: ; preds = %50
%56 = load i8, i8* %3
%57 = icmp eq i8 %56, 6
br i1 %57, label %58, label %16
58: ; preds = %35, %24, %55
%59 = call i32 @framer_set_result(i32 6)
%60 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %38, %58, %52, %47, %41, %14
%62 = phi i32* [ @LOG_ERR, %38 ], [ @LOG_INFO, %58 ], [ @LOG_ERR, %52 ], [ @LOG_ERR, %47 ], [ @LOG_ERR, %41 ], [ @LOG_ERR, %14 ]
%63 = phi i32 [ 0, %38 ], [ 1, %58 ], [ 0, %52 ], [ 0, %47 ], [ 0, %41 ], [ 0, %14 ]
%64 = load i32, i32* %62
%65 = call i32 @logIT(i32 %64, i8* %6)
ret i32 %63
}