#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*/ username;
typedef int /*<<< orphan*/ tmp;
typedef int /*<<< orphan*/ test_path;
struct passwd {
  char *pw_name;
};
struct dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ ap_get_session_name(int, char *);
int ap_session_running(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int sscanf(int /*<<< orphan*/, char *, int *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int ap_find_session(char *session_name, int *session) {
  char remote_name[256];
  char test_path[1024];
  char tmp[1024];
  char username[512];
  struct passwd *pwd;
  DIR *dir;
  struct dirent *entry;
  int session_id = 0;
  if (!session_name)
    return 0;
  dir = opendir("/tmp");
  pwd = getpwuid(geteuid());
  snprintf(username, sizeof(username), "%s",
           pwd == NULL ? "anonymous" : pwd->pw_name);
  snprintf(test_path, sizeof(test_path), "alsaplayer_%s_", username);
  if (dir) {
    while ((entry = readdir(dir)) != NULL) {
      if (strncmp(entry->d_name, test_path, strlen(test_path)) == 0) {
        snprintf(tmp, sizeof(tmp), "%s%%d", test_path);
        if (sscanf(entry->d_name, tmp, &session_id) == 1) {
          if (ap_session_running(session_id) == 1) {
            if (ap_get_session_name(session_id, remote_name)) {
              if (strcmp(remote_name, session_name) == 0) {
                *session = session_id;
                closedir(dir);
                return 1;
              }
            }
          }
        }
      }
    }
    closedir(dir);
  }
  return 0;
}


x86 assembly (clang, -O0):
<ap_find_session>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0xb3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f4 <ap_find_session+0x34>
movl   $0x0,-0x4(%rbp)
jmp    13ef <ap_find_session+0x22f>
lea    0xe05(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0xb30(%rbp)
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
mov    %rax,-0xb28(%rbp)
lea    -0xb20(%rbp),%rax
mov    %rax,-0xb48(%rbp)
cmpq   $0x0,-0xb28(%rbp)
jne    124b <ap_find_session+0x8b>
lea    0xdc9(%rip),%rax
mov    %rax,-0xb50(%rbp)
jmp    125c <ap_find_session+0x9c>
mov    -0xb28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0xb50(%rbp)
mov    -0xb48(%rbp),%rdi
mov    -0xb50(%rbp),%rcx
mov    $0x200,%esi
lea    0xd8f(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x520(%rbp),%rdi
lea    -0xb20(%rbp),%rcx
mov    $0x400,%esi
lea    0xd7d(%rip),%rdx
call   1070 <snprintf@plt>
cmpq   $0x0,-0xb30(%rbp)
je     13e8 <ap_find_session+0x228>
jmp    12ad <ap_find_session+0xed>
mov    -0xb30(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0xb38(%rbp)
cmp    $0x0,%rax
je     13dc <ap_find_session+0x21c>
mov    -0xb38(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xb5c(%rbp)
lea    -0x520(%rbp),%rax
mov    %rax,-0xb58(%rbp)
lea    -0x520(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xb5c(%rbp),%edi
mov    -0xb58(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    13d7 <ap_find_session+0x217>
lea    -0x920(%rbp),%rdi
lea    -0x520(%rbp),%rcx
mov    $0x400,%esi
lea    0xcf6(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0xb38(%rbp),%rax
mov    (%rax),%edi
lea    -0x920(%rbp),%rsi
lea    -0xb3c(%rbp),%rdx
call   10e0 <sscanf@plt>
cmp    $0x1,%eax
jne    13d2 <ap_find_session+0x212>
mov    -0xb3c(%rbp),%edi
call   10b0 <ap_session_running@plt>
cmp    $0x1,%eax
jne    13cd <ap_find_session+0x20d>
mov    -0xb3c(%rbp),%edi
lea    -0x120(%rbp),%rsi
call   10c0 <ap_get_session_name@plt>
cmp    $0x0,%rax
je     13c8 <ap_find_session+0x208>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
jne    13c3 <ap_find_session+0x203>
mov    -0xb3c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xb30(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ef <ap_find_session+0x22f>
jmp    13c8 <ap_find_session+0x208>
jmp    13cd <ap_find_session+0x20d>
jmp    13d2 <ap_find_session+0x212>
jmp    13d7 <ap_find_session+0x217>
jmp    12ad <ap_find_session+0xed>
mov    -0xb30(%rbp),%rdi
call   1090 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ap_find_session>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xb28,%rsp
movl   $0x0,0xc(%rsp)
test   %rdi,%rdi
je     120d <ap_find_session+0x4d>
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xe15(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <geteuid@plt>
mov    %eax,%edi
call   1050 <getpwuid@plt>
test   %rax,%rax
je     1214 <ap_find_session+0x54>
mov    (%rax),%rcx
jmp    121b <ap_find_session+0x5b>
xor    %ebp,%ebp
jmp    1352 <ap_find_session+0x192>
lea    0xded(%rip),%rcx
lea    0xde3(%rip),%rdx
lea    0x120(%rsp),%r15
mov    %r15,%rdi
mov    $0x200,%esi
call   1070 <snprintf@plt>
lea    0xdd4(%rip),%rdx
lea    0x320(%rsp),%rdi
mov    $0x400,%esi
mov    %r15,%rcx
call   1070 <snprintf@plt>
test   %rbx,%rbx
je     1352 <ap_find_session+0x192>
mov    %rbx,%rdi
call   10d0 <readdir@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     134a <ap_find_session+0x18a>
mov    %rax,%r12
mov    %r13,0x18(%rsp)
mov    %r14,0x10(%rsp)
lea    0x320(%rsp),%rbp
lea    0x720(%rsp),%r14
lea    0xc(%rsp),%r15
jmp    12b4 <ap_find_session+0xf4>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
call   10d0 <readdir@plt>
mov    %rax,%r12
test   %rax,%rax
je     1348 <ap_find_session+0x188>
mov    (%r12),%r13d
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    12a0 <ap_find_session+0xe0>
mov    %r14,%rdi
mov    $0x400,%esi
lea    0xd40(%rip),%rdx
mov    %rbp,%rcx
call   1070 <snprintf@plt>
mov    (%r12),%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10e0 <sscanf@plt>
cmp    $0x1,%eax
jne    12a0 <ap_find_session+0xe0>
mov    0xc(%rsp),%edi
call   10b0 <ap_session_running@plt>
cmp    $0x1,%eax
jne    12a0 <ap_find_session+0xe0>
mov    0xc(%rsp),%edi
lea    0x20(%rsp),%rsi
call   10c0 <ap_get_session_name@plt>
test   %rax,%rax
je     12a0 <ap_find_session+0xe0>
lea    0x20(%rsp),%rdi
mov    0x18(%rsp),%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    12a0 <ap_find_session+0xe0>
mov    0xc(%rsp),%eax
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx)
mov    $0x1,%ebp
jmp    134a <ap_find_session+0x18a>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1090 <closedir@plt>
mov    %ebp,%eax
add    $0xb28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
%struct.dirent = type { i32 }
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [10 x i8] c"anonymous\00"
@.str.3 = [15 x i8] c"alsaplayer_%s_\00"
@.str.4 = [6 x i8] c"%s%%d\00"
define i32 @ap_find_session(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [256 x i8]
%7 = alloca [1024 x i8]
%8 = alloca [1024 x i8]
%9 = alloca [512 x i8]
%10 = alloca %struct.passwd*
%11 = alloca i32*
%12 = alloca %struct.dirent*
%13 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 0, i32* %13
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %2
store i32 0, i32* %3
br label %89
17: ; preds = %2
%18 = call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %11
%19 = call i32 (...) @geteuid()
%20 = call %struct.passwd* @getpwuid(i32 %19)
store %struct.passwd* %20, %struct.passwd** %10
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%22 = load %struct.passwd*, %struct.passwd** %10
%23 = icmp eq %struct.passwd* %22, null
br i1 %23, label %24, label %25
24: ; preds = %17
br label %29
25: ; preds = %17
%26 = load %struct.passwd*, %struct.passwd** %10
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 0
%28 = load i8*, i8** %27
br label %29
29: ; preds = %25, %24
%30 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %24 ], [ %28, %25 ]
%31 = call i32 @snprintf(i8* %21, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%34 = call i32 @snprintf(i8* %32, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %33)
%35 = load i32*, i32** %11
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %88
37: ; preds = %29
br label %38
38: ; preds = %84, %37
%39 = load i32*, i32** %11
%40 = call %struct.dirent* @readdir(i32* %39)
store %struct.dirent* %40, %struct.dirent** %12
%41 = icmp ne %struct.dirent* %40, null
br i1 %41, label %42, label %85
42: ; preds = %38
%43 = load %struct.dirent*, %struct.dirent** %12
%44 = getelementptr inbounds %struct.dirent, %struct.dirent* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = call i64 @strncmp(i32 %45, i8* %46, i32 %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %84
51: ; preds = %42
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%54 = call i32 @snprintf(i8* %52, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %53)
%55 = load %struct.dirent*, %struct.dirent** %12
%56 = getelementptr inbounds %struct.dirent, %struct.dirent* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%59 = call i32 @sscanf(i32 %57, i8* %58, i32* %13)
%60 = icmp eq i32 %59, 1
br i1 %60, label %61, label %83
61: ; preds = %51
%62 = load i32, i32* %13
%63 = call i32 @ap_session_running(i32 %62)
%64 = icmp eq i32 %63, 1
br i1 %64, label %65, label %82
65: ; preds = %61
%66 = load i32, i32* %13
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%68 = call i64 @ap_get_session_name(i32 %66, i8* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %81
70: ; preds = %65
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%72 = load i8*, i8** %4
%73 = call i64 @strcmp(i8* %71, i8* %72)
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %70
%76 = load i32, i32* %13
%77 = load i32*, i32** %5
store i32 %76, i32* %77
%78 = load i32*, i32** %11
%79 = call i32 @closedir(i32* %78)
store i32 1, i32* %3
br label %89
80: ; preds = %70
br label %81
81: ; preds = %80, %65
br label %82
82: ; preds = %81, %61
br label %83
83: ; preds = %82, %51
br label %84
84: ; preds = %83, %42
br label %38
85: ; preds = %38
%86 = load i32*, i32** %11
%87 = call i32 @closedir(i32* %86)
br label %88
88: ; preds = %85, %29
store i32 0, i32* %3
br label %89
89: ; preds = %88, %75, %16
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
%struct.dirent = type { i32 }
@.str = [5 x i8] c"/tmp\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [10 x i8] c"anonymous\00"
@.str.3 = [15 x i8] c"alsaplayer_%s_\00"
@.str.4 = [6 x i8] c"%s%%d\00"
define i32 @ap_find_session(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [256 x i8]
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca [512 x i8]
%7 = alloca i32
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%12 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%13 = icmp eq i8* %0, null
br i1 %13, label %61, label %14
14: ; preds = %2
%15 = tail call i32* @opendir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 (...) @geteuid()
%17 = tail call %struct.passwd* @getpwuid(i32 %16)
%18 = icmp eq %struct.passwd* %17, null
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %17, i64 0, i32 0
%21 = load i8*, i8** %20
br label %22
22: ; preds = %14, %19
%23 = phi i8* [ %21, %19 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), %14 ]
%24 = call i32 @snprintf(i8* %11, i32 512, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = call i32 @snprintf(i8* %9, i32 1024, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%26 = icmp eq i32* %15, null
br i1 %26, label %61, label %27
27: ; preds = %22
%28 = call %struct.dirent* @readdir(i32* %15)
%29 = icmp eq %struct.dirent* %28, null
br i1 %29, label %58, label %30
30: ; preds = %27, %55
%31 = phi %struct.dirent* [ %56, %55 ], [ %28, %27 ]
%32 = getelementptr inbounds %struct.dirent, %struct.dirent* %31, i64 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @strlen(i8* %9)
%35 = call i64 @strncmp(i32 %33, i8* %9, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %55
37: ; preds = %30
%38 = call i32 @snprintf(i8* %10, i32 1024, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%39 = load i32, i32* %32
%40 = call i32 @sscanf(i32 %39, i8* %10, i32* %7)
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %55
42: ; preds = %37
%43 = load i32, i32* %7
%44 = call i32 @ap_session_running(i32 %43)
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %55
46: ; preds = %42
%47 = load i32, i32* %7
%48 = call i64 @ap_get_session_name(i32 %47, i8* %8)
%49 = icmp eq i64 %48, 0
br i1 %49, label %55, label %50
50: ; preds = %46
%51 = call i64 @strcmp(i8* %8, i8* %0)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = load i32, i32* %7
store i32 %54, i32* %1
br label %58
55: ; preds = %37, %46, %50, %42, %30
%56 = call %struct.dirent* @readdir(i32* %15)
%57 = icmp eq %struct.dirent* %56, null
br i1 %57, label %58, label %30
58: ; preds = %55, %27, %53
%59 = phi i32 [ 1, %53 ], [ 0, %27 ], [ 0, %55 ]
%60 = call i32 @closedir(i32* %15)
br label %61
61: ; preds = %58, %22, %2
%62 = phi i32 [ 0, %2 ], [ 0, %22 ], [ %59, %58 ]
ret i32 %62
}