#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ sscanf(char *, char *, int *, char **);
int /*<<< orphan*/ *strstr(char *, char *);
int /*<<< orphan*/ system(char *);
char *port_search() {
  int pid, oldport = 0;
  FILE *ttyfile;
  char *tty, suffix[200], instring[200];
  system("\\ps -x > term-search");
  ttyfile = fopen("term-search", "r");
  tty = (char *)malloc(200);
  while (fgets(instring, 200, ttyfile) != NULL) {
    if (strstr(instring, "sleep 31536000") != NULL) {
      sscanf(instring, "%d %s", &pid, &suffix);
      oldport = 1;
      sprintf(tty, "/dev/%s%s", "", suffix);
      break;
    };
  };
  fclose(ttyfile);
  system("rm term-search");
  if (!oldport) {
    free(tty);
    return ((char *)NULL);
  } else
    return (tty);
}


x86 assembly (clang, -O0):
<port_search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
movl   $0x0,-0x10(%rbp)
lea    0xe57(%rip),%rdi
call   1050 <system@plt>
lea    0xe54(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    $0xc8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
lea    -0x1c0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xc8,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     125c <port_search+0xcc>
lea    -0x1c0(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1257 <port_search+0xc7>
lea    -0x1c0(%rbp),%rdi
lea    -0xf0(%rbp),%rcx
lea    0xe02(%rip),%rsi
lea    -0xc(%rbp),%rdx
call   1080 <sscanf@plt>
movl   $0x1,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0xf0(%rbp),%rcx
lea    0xde6(%rip),%rsi
lea    0xdd8(%rip),%rdx
call   10a0 <sprintf@plt>
jmp    125c <port_search+0xcc>
jmp    11d3 <port_search+0x43>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdca(%rip),%rdi
call   1050 <system@plt>
cmpl   $0x0,-0x10(%rbp)
jne    1291 <port_search+0x101>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1299 <port_search+0x109>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<port_search>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1a8,%rsp
lea    0xe5b(%rip),%rdi
call   1050 <system@plt>
lea    0xe58(%rip),%rdi
lea    0xe5d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    $0xc8,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0x10(%rsp),%rbx
lea    0xe3e(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0xc8,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1255 <port_search+0xc5>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     11e0 <port_search+0x50>
lea    0xe1a(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
lea    0xe0(%rsp),%rbx
mov    %rbx,%rcx
call   1080 <sscanf@plt>
lea    0xdff(%rip),%rsi
lea    0xdf1(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rcx
call   10a0 <sprintf@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    0xde8(%rip),%rdi
call   1050 <system@plt>
jmp    1274 <port_search+0xe4>
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    0xdd2(%rip),%rdi
call   1050 <system@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x1a8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [21 x i8] c"\\ps -x > term-search\00"
@.str.1 = [12 x i8] c"term-search\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c"sleep 31536000\00"
@.str.4 = [6 x i8] c"%d %s\00"
@.str.5 = [10 x i8] c"/dev/%s%s\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [15 x i8] c"rm term-search\00"
define i8* @port_search() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [200 x i8]
%7 = alloca [200 x i8]
store i32 0, i32* %3
%8 = call i32 @system(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %9, i32** %4
%10 = call i64 @malloc(i32 200)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
br label %12
12: ; preds = %28, %0
%13 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32* @fgets(i8* %13, i32 200, i32* %14)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %29
17: ; preds = %12
%18 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%19 = call i32* @strstr(i8* %18, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %28
21: ; preds = %17
%22 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%23 = bitcast [200 x i8]* %6 to i8**
%24 = call i32 @sscanf(i8* %22, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %2, i8** %23)
store i32 1, i32* %3
%25 = load i8*, i8** %5
%26 = getelementptr inbounds [200 x i8], [200 x i8]* %6, i64 0, i64 0
%27 = call i32 @sprintf(i8* %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8* %26)
br label %29
28: ; preds = %17
br label %12
29: ; preds = %21, %12
%30 = load i32*, i32** %4
%31 = call i32 @fclose(i32* %30)
%32 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
%33 = load i32, i32* %3
%34 = icmp ne i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = load i8*, i8** %5
%37 = call i32 @free(i8* %36)
store i8* null, i8** %1
br label %40
38: ; preds = %29
%39 = load i8*, i8** %5
store i8* %39, i8** %1
br label %40
40: ; preds = %38, %35
%41 = load i8*, i8** %1
ret i8* %41
}

LLVM IR (-O2):
@.str = [21 x i8] c"\\ps -x > term-search\00"
@.str.1 = [12 x i8] c"term-search\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [15 x i8] c"sleep 31536000\00"
@.str.4 = [6 x i8] c"%d %s\00"
@.str.5 = [10 x i8] c"/dev/%s%s\00"
@.str.6 = [1 x i8] zeroinitializer
@.str.7 = [15 x i8] c"rm term-search\00"
define i8* @port_search() {
%1 = alloca i32
%2 = alloca [200 x i8]
%3 = alloca [200 x i8]
%4 = bitcast i32* %1 to i8*
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @system(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i64 @malloc(i32 200)
%10 = inttoptr i64 %9 to i8*
br label %11
11: ; preds = %14, %0
%12 = call i32* @fgets(i8* %6, i32 200, i32* %8)
%13 = icmp eq i32* %12, null
br i1 %13, label %23, label %14
14: ; preds = %11
%15 = call i32* @strstr(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %11, label %17
17: ; preds = %14
%18 = bitcast [200 x i8]* %2 to i8**
%19 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32* %1, i8** %18)
%20 = call i32 @sprintf(i8* dereferenceable(1) %10, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0), i8* %5)
%21 = call i32 @fclose(i32* %8)
%22 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %27
23: ; preds = %11
%24 = call i32 @fclose(i32* %8)
%25 = call i32 @system(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
%26 = call i32 @free(i8* %10)
br label %27
27: ; preds = %17, %23
%28 = phi i8* [ null, %23 ], [ %10, %17 ]
ret i8* %28
}