#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 */
struct dirent {
  scalar_t__ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int atoi(scalar_t__);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
scalar_t__ getuid();
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
scalar_t__ strncmp(scalar_t__, char *, int);
int find_wave_pid() {
  DIR *dir;
  struct dirent *dir_entry;
  char pattern[16];
  int i, imax = 0, pl;
  dir = opendir("/tmp");
  sprintf(pattern, ".wave.%d.", (int)getuid());
  pl = strlen(pattern);
  while (dir_entry = readdir(dir)) {
    if (strncmp(dir_entry->d_name, pattern, pl) == 0) {
      i = atoi(dir_entry->d_name + pl);
      if (i > imax)
        imax = i;
    }
  }
  closedir(dir);
  return (imax);
}


x86 assembly (clang, -O0):
<find_wave_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x28(%rbp)
lea    0xe6a(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x8(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <getuid@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe4a(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1232 <find_wave_pid+0xb2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    122d <find_wave_pid+0xad>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    1228 <find_wave_pid+0xa8>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    122d <find_wave_pid+0xad>
jmp    11cc <find_wave_pid+0x4c>
mov    -0x8(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_wave_pid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe6b(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r14
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <getuid@plt>
lea    0xe57(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
je     1218 <find_wave_pid+0x98>
mov    %rax,%rbp
movslq %ebx,%r13
xor    %r12d,%r12d
mov    %rsp,%r15
jmp    11f0 <find_wave_pid+0x70>
mov    %r14,%rdi
call   1080 <readdir@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1218 <find_wave_pid+0x98>
mov    0x0(%rbp),%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11e0 <find_wave_pid+0x60>
mov    0x0(%rbp),%rdi
add    %r13,%rdi
call   1090 <atoi@plt>
cmp    %r12d,%eax
cmovg  %eax,%r12d
jmp    11e0 <find_wave_pid+0x60>
mov    %r14,%rdi
call   1070 <closedir@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64 }
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [10 x i8] c".wave.%d.\00"
define i32 @find_wave_pid() {
%1 = alloca i32*
%2 = alloca %struct.dirent*
%3 = alloca [16 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %5
%7 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %1
%8 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%9 = call i64 (...) @getuid()
%10 = trunc i64 %9 to i32
%11 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %6
br label %14
14: ; preds = %40, %0
%15 = load i32*, i32** %1
%16 = call %struct.dirent* @readdir(i32* %15)
store %struct.dirent* %16, %struct.dirent** %2
%17 = icmp ne %struct.dirent* %16, null
br i1 %17, label %18, label %41
18: ; preds = %14
%19 = load %struct.dirent*, %struct.dirent** %2
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%23 = load i32, i32* %6
%24 = call i64 @strncmp(i64 %21, i8* %22, i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %18
%27 = load %struct.dirent*, %struct.dirent** %2
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = add nsw i64 %29, %31
%33 = call i32 @atoi(i64 %32)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = load i32, i32* %5
%36 = icmp sgt i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = load i32, i32* %4
store i32 %38, i32* %5
br label %39
39: ; preds = %37, %26
br label %40
40: ; preds = %39, %18
br label %14
41: ; preds = %14
%42 = load i32*, i32** %1
%43 = call i32 @closedir(i32* %42)
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
%struct.dirent = type { i64 }
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [10 x i8] c".wave.%d.\00"
define i32 @find_wave_pid() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i64 (...) @getuid()
%5 = trunc i64 %4 to i32
%6 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = call i32 @strlen(i8* %2)
%8 = call %struct.dirent* @readdir(i32* %3)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %29, label %10
10: ; preds = %0
%11 = sext i32 %7 to i64
br label %12
12: ; preds = %10, %25
%13 = phi %struct.dirent* [ %8, %10 ], [ %27, %25 ]
%14 = phi i32 [ 0, %10 ], [ %26, %25 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %13, i64 0, i32 0
%16 = load i64, i64* %15
%17 = call i64 @strncmp(i64 %16, i8* %2, i32 %7)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %12
%20 = load i64, i64* %15
%21 = add nsw i64 %20, %11
%22 = call i32 @atoi(i64 %21)
%23 = icmp sgt i32 %22, %14
%24 = select i1 %23, i32 %22, i32 %14
br label %25
25: ; preds = %19, %12
%26 = phi i32 [ %14, %12 ], [ %24, %19 ]
%27 = call %struct.dirent* @readdir(i32* %3)
%28 = icmp eq %struct.dirent* %27, null
br i1 %28, label %29, label %12
29: ; preds = %25, %0
%30 = phi i32 [ 0, %0 ], [ %26, %25 ]
%31 = call i32 @closedir(i32* %3)
ret i32 %30
}