#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*/ s;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ close_serial_port();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ system(char *);
int close_rfcomm() {
  FILE *fp;
  char s[] = "pidof /usr/bin/rfcomm |awk '{print $1;}'";
  int id;
  close_serial_port();
  fp = popen(s, "r");
  s[0] = '\0';
  if (fp) {
    if (feof(fp) == 0)
      fgets(s, sizeof(s), fp);
    fclose(fp);
  }
  id = atoi(s);
  if (id > 0) {
    sprintf(s, "sudo kill %d", id);
    printf("[%s]\n", s);
    system(s);
  }
  return id;
}


x86 assembly (clang, -O0):
<close_rfcomm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
lea    -0x40(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x29,%edx
call   1080 <memcpy@plt>
mov    $0x0,%al
call   1090 <close_serial_port@plt>
lea    -0x40(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   10a0 <popen@plt>
mov    %rax,-0x8(%rbp)
movb   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1215 <close_rfcomm+0x75>
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    120c <close_rfcomm+0x6c>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x29,%esi
call   1060 <fgets@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x40(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jle    1257 <close_rfcomm+0xb7>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
lea    0xdf2(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x40(%rbp),%rsi
lea    0xdef(%rip),%rdi
call   1050 <printf@plt>
lea    -0x40(%rbp),%rdi
call   1040 <system@plt>
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_rfcomm>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movups 0xe7b(%rip),%xmm0
movups %xmm0,0x19(%rsp)
movaps 0xe66(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe4a(%rip),%xmm0
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1080 <close_serial_port@plt>
lea    0xe61(%rip),%rsi
mov    %rsp,%rdi
call   1090 <popen@plt>
movb   $0x0,(%rsp)
test   %rax,%rax
je     1201 <close_rfcomm+0x71>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <feof@plt>
test   %rax,%rax
jne    11f9 <close_rfcomm+0x69>
mov    %rsp,%rdi
mov    $0x29,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rsp,%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    123a <close_rfcomm+0xaa>
lea    0xe15(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebx,%edx
call   10b0 <sprintf@plt>
lea    0xe0e(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %r14,%rdi
call   1040 <system@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.close_rfcomm.s = [41 x i8] c"pidof /usr/bin/rfcomm |awk '{print $1;}'\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"sudo kill %d\00"
@.str.2 = [6 x i8] c"[%s]\0A\00"
define i32 @close_rfcomm() {
%1 = alloca i32*
%2 = alloca [41 x i8]
%3 = alloca i32
%4 = bitcast [41 x i8]* %2 to i8*
%5 = call i32 (...) @close_serial_port()
%6 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
%7 = call i32* @popen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %1
%8 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
store i8 0, i8* %8
%9 = load i32*, i32** %1
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %22
11: ; preds = %0
%12 = load i32*, i32** %1
%13 = call i64 @feof(i32* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
%17 = load i32*, i32** %1
%18 = call i32 @fgets(i8* %16, i32 41, i32* %17)
br label %19
19: ; preds = %15, %11
%20 = load i32*, i32** %1
%21 = call i32 @fclose(i32* %20)
br label %22
22: ; preds = %19, %0
%23 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
%29 = load i32, i32* %3
%30 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [41 x i8], [41 x i8]* %2, i64 0, i64 0
%34 = call i32 @system(i8* %33)
br label %35
35: ; preds = %27, %22
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@__const.close_rfcomm.s = [41 x i8] c"pidof /usr/bin/rfcomm |awk '{print $1;}'\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [13 x i8] c"sudo kill %d\00"
@.str.2 = [6 x i8] c"[%s]\0A\00"
define i32 @close_rfcomm() {
%1 = alloca [41 x i8]
%2 = getelementptr inbounds [41 x i8], [41 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @close_serial_port()
%4 = call i32* @popen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8 0, i8* %2
%5 = icmp eq i32* %4, null
br i1 %5, label %13, label %6
6: ; preds = %0
%7 = tail call i64 @feof(i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = call i32 @fgets(i8* %2, i32 41, i32* %4)
br label %11
11: ; preds = %9, %6
%12 = call i32 @fclose(i32* %4)
br label %13
13: ; preds = %11, %0
%14 = call i32 @atoi(i8* %2)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%18 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%19 = call i32 @system(i8* %2)
br label %20
20: ; preds = %16, %13
ret i32 %14
}