#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;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
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_close_p300(int fd) {
  char string[100];
  int i;
  char wbuf = 0x04;
  char rbuf = 0;
  unsigned long etime;
  int rlen;
  for (i = 0; i < 3; i++) {
    if (!my_send(fd, &wbuf, 1)) {
      framer_set_result(0x15);
      snprintf(string, sizeof(string), ">FRAMER: reset 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: close read failure");
      logIT(LOG_ERR, string);
      return 0;
    } else if (rlen == 0) {
      framer_set_result(0x15);
      snprintf(string, sizeof(string), ">FRAMER: close read timeout for ack");
      logIT(LOG_ERR, string);
      return 0;
    } else if ((rbuf == 0x06) || (rbuf == 0x05)) {
      framer_set_result(0x05);
      snprintf(string, sizeof(string), ">FRAMER: closed");
      logIT(LOG_INFO, string);
      return 1;
    } else {
      snprintf(string, sizeof(string), ">FRAMER: unexpected data %02Xk", rbuf);
      logIT(LOG_ERR, string);
    }
  }
  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   $0x4,-0x75(%rbp)
movb   $0x0,-0x76(%rbp)
movl   $0x0,-0x74(%rbp)
cmpl   $0x3,-0x74(%rbp)
jge    132e <framer_close_p300+0x1de>
mov    -0x8(%rbp),%edi
lea    -0x75(%rbp),%rsi
mov    $0x1,%edx
call   1040 <my_send@plt>
cmp    $0x0,%eax
jne    11d0 <framer_close_p300+0x80>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xe55(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1070 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    136d <framer_close_p300+0x21d>
movq   $0x0,-0x80(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x76(%rbp),%rsi
mov    $0x1,%edx
lea    -0x80(%rbp),%rcx
call   1060 <receive_nb@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    123f <framer_close_p300+0xef>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xdfe(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1070 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    136d <framer_close_p300+0x21d>
cmpl   $0x0,-0x84(%rbp)
jne    128b <framer_close_p300+0x13b>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xdce(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1070 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    136d <framer_close_p300+0x21d>
movsbl -0x76(%rbp),%eax
cmp    $0x6,%eax
je     12a5 <framer_close_p300+0x155>
movsbl -0x76(%rbp),%eax
cmp    $0x5,%eax
jne    12e4 <framer_close_p300+0x194>
mov    $0x5,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xd99(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1070 <logIT@plt>
movl   $0x1,-0x4(%rbp)
jmp    136d <framer_close_p300+0x21d>
lea    -0x70(%rbp),%rdi
movsbl -0x76(%rbp),%ecx
mov    $0x64,%esi
lea    0xd70(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1070 <logIT@plt>
jmp    1316 <framer_close_p300+0x1c6>
jmp    131b <framer_close_p300+0x1cb>
jmp    1320 <framer_close_p300+0x1d0>
mov    -0x74(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x74(%rbp)
jmp    116d <framer_close_p300+0x1d>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    -0x70(%rbp),%rdi
mov    $0x64,%esi
lea    0xd3f(%rip),%rdx
mov    $0x3,%ecx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
lea    -0x70(%rbp),%rsi
call   1070 <logIT@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %edi,%ebx
movb   $0x4,0x7(%rsp)
movb   $0x0,0x6(%rsp)
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <my_send@plt>
test   %eax,%eax
je     1335 <framer_close_p300+0x1e5>
movq   $0x0,0x8(%rsp)
lea    0x6(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <receive_nb@plt>
test   %eax,%eax
js     1348 <framer_close_p300+0x1f8>
je     135b <framer_close_p300+0x20b>
movsbl 0x6(%rsp),%ecx
lea    -0x5(%rcx),%eax
cmp    $0x1,%al
ja     11eb <framer_close_p300+0x9b>
mov    $0x5,%edi
call   1030 <framer_set_result@plt>
lea    0xe8f(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    $0x1,%ebx
mov    0x2dfa(%rip),%r15
jmp    1386 <framer_close_p300+0x236>
lea    0xe76(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2dc3(%rip),%r15
mov    (%r15),%edi
mov    %r14,%rsi
call   1070 <logIT@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <my_send@plt>
test   %eax,%eax
je     1335 <framer_close_p300+0x1e5>
movq   $0x0,0x8(%rsp)
lea    0x6(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <receive_nb@plt>
test   %eax,%eax
js     1348 <framer_close_p300+0x1f8>
je     135b <framer_close_p300+0x20b>
movsbl 0x6(%rsp),%ecx
lea    -0x5(%rcx),%eax
cmp    $0x2,%al
jb     11b8 <framer_close_p300+0x68>
lea    0xdf3(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    (%r15),%edi
mov    %r14,%rsi
call   1070 <logIT@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1040 <my_send@plt>
test   %eax,%eax
je     1335 <framer_close_p300+0x1e5>
movq   $0x0,0x8(%rsp)
lea    0x6(%rsp),%rsi
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <receive_nb@plt>
test   %eax,%eax
js     1348 <framer_close_p300+0x1f8>
je     135b <framer_close_p300+0x20b>
movsbl 0x6(%rsp),%ecx
lea    -0x5(%rcx),%eax
cmp    $0x2,%al
jb     11b8 <framer_close_p300+0x68>
lea    0xd7b(%rip),%rdx
xor    %ebx,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    (%r15),%edi
mov    %r14,%rsi
call   1070 <logIT@plt>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xd68(%rip),%rdx
mov    %r14,%rdi
mov    $0x64,%esi
mov    $0x3,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    1386 <framer_close_p300+0x236>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xcba(%rip),%rdx
jmp    136c <framer_close_p300+0x21c>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xcbf(%rip),%rdx
jmp    136c <framer_close_p300+0x21c>
mov    $0x15,%edi
call   1030 <framer_set_result@plt>
lea    0xcc8(%rip),%rdx
xor    %ebx,%ebx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2c4a(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%rsi
call   1070 <logIT@plt>
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c">FRAMER: reset not send\00"
@LOG_ERR = global i32 0
@.str.1 = [28 x i8] c">FRAMER: close read failure\00"
@.str.2 = [36 x i8] c">FRAMER: close read timeout for ack\00"
@.str.3 = [16 x i8] c">FRAMER: closed\00"
@LOG_INFO = global i32 0
@.str.4 = [31 x i8] c">FRAMER: unexpected data %02Xk\00"
@.str.5 = [39 x i8] c">FRAMER: could not close (%d attempts)\00"
define i32 @framer_close_p300(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %3
store i8 4, i8* %6
store i8 0, i8* %7
store i32 0, i32* %5
br label %10
10: ; preds = %72, %1
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %75
13: ; preds = %10
%14 = load i32, i32* %3
%15 = call i32 @my_send(i32 %14, i8* %6, i32 1)
%16 = icmp ne i32 %15, 0
br i1 %16, label %24, label %17
17: ; preds = %13
%18 = call i32 @framer_set_result(i32 21)
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%20 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %19, i32 100, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @LOG_ERR
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%23 = call i32 @logIT(i32 %21, i8* %22)
store i32 0, i32* %2
br label %82
24: ; preds = %13
store i64 0, i64* %8
%25 = load i32, i32* %3
%26 = call i32 @receive_nb(i32 %25, i8* %7, i32 1, i64* %8)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %36
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 ([28 x i8], [28 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
%37 = load i32, i32* %9
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = call i32 @framer_set_result(i32 21)
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%42 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %41, i32 100, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @LOG_ERR
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%45 = call i32 @logIT(i32 %43, i8* %44)
store i32 0, i32* %2
br label %82
46: ; preds = %36
%47 = load i8, i8* %7
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 6
br i1 %49, label %54, label %50
50: ; preds = %46
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 5
br i1 %53, label %54, label %61
54: ; preds = %50, %46
%55 = call i32 @framer_set_result(i32 5)
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%57 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %56, i32 100, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* @LOG_INFO
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%60 = call i32 @logIT(i32 %58, i8* %59)
store i32 1, i32* %2
br label %82
61: ; preds = %50
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%63 = load i8, i8* %7
%64 = sext i8 %63 to i32
%65 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %62, i32 100, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %64)
%66 = load i32, i32* @LOG_ERR
%67 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%68 = call i32 @logIT(i32 %66, i8* %67)
br label %69
69: ; preds = %61
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 %10
75: ; preds = %10
%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, %54, %39, %29, %17
%83 = load i32, i32* %2
ret i32 %83
}

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