#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 stat {
  int /*<<< orphan*/ st_mode;
};
struct passwd {
  char *pw_name;
};
typedef int /*<<< orphan*/ path;

/* Variables and functions */
scalar_t__ S_ISSOCK(int /*<<< orphan*/);
scalar_t__ ap_ping(int);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int ap_session_running(int session) {
  struct stat statbuf;
  struct passwd *pwd;
  char path[1024];
  pwd = getpwuid(geteuid());
  snprintf(path, sizeof(path), "/tmp/alsaplayer_%s_%d",
           pwd == NULL ? "anonymous" : pwd->pw_name, session);
  if (stat(path, &statbuf) != 0)
    return 0;
  if (S_ISSOCK(statbuf.st_mode)) {
    if (ap_ping(session))
      return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ap_session_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ac <ap_session_running+0x4c>
lea    0xe76(%rip),%rax
mov    %rax,-0x430(%rbp)
jmp    11ba <ap_session_running+0x5a>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x430(%rbp)
mov    -0x428(%rbp),%rdi
mov    -0x430(%rbp),%rcx
mov    -0x8(%rbp),%r8d
mov    $0x400,%esi
lea    0xe28(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1203 <ap_session_running+0xa3>
movl   $0x0,-0x4(%rbp)
jmp    123f <ap_session_running+0xdf>
mov    -0x10(%rbp),%edi
call   1040 <S_ISSOCK@plt>
cmp    $0x0,%rax
je     1238 <ap_session_running+0xd8>
mov    -0x8(%rbp),%edi
call   1080 <ap_ping@plt>
cmp    $0x0,%rax
je     1233 <ap_session_running+0xd3>
movl   $0x1,-0x4(%rbp)
jmp    123f <ap_session_running+0xdf>
jmp    1238 <ap_session_running+0xd8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ap_session_running>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %edi,%r14d
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
test   %rax,%rax
je     1188 <ap_session_running+0x28>
mov    (%rax),%rcx
jmp    118f <ap_session_running+0x2f>
lea    0xe87(%rip),%rcx
lea    0xe6a(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14d,%r8d
call   1050 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jne    11df <ap_session_running+0x7f>
mov    0x8(%rsp),%edi
call   1040 <S_ISSOCK@plt>
test   %rax,%rax
je     11dd <ap_session_running+0x7d>
mov    %r14d,%edi
call   1080 <ap_ping@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    11df <ap_session_running+0x7f>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.passwd = type { i8* }
@.str = [22 x i8] c"/tmp/alsaplayer_%s_%d\00"
@.str.1 = [10 x i8] c"anonymous\00"
define i32 @ap_session_running(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca %struct.passwd*
%6 = alloca [1024 x i8]
store i32 %0, i32* %3
%7 = call i32 (...) @geteuid()
%8 = call %struct.passwd* @getpwuid(i32 %7)
store %struct.passwd* %8, %struct.passwd** %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = load %struct.passwd*, %struct.passwd** %5
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %13
12: ; preds = %1
br label %17
13: ; preds = %1
%14 = load %struct.passwd*, %struct.passwd** %5
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i8*, i8** %15
br label %17
17: ; preds = %13, %12
%18 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %12 ], [ %16, %13 ]
%19 = load i32, i32* %3
%20 = call i32 @snprintf(i8* %9, i32 1024, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %19)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%22 = call i64 @stat(i8* %21, %struct.stat* %4)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 0, i32* %2
br label %37
25: ; preds = %17
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i64 @S_ISSOCK(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %25
%31 = load i32, i32* %3
%32 = call i64 @ap_ping(i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %30
store i32 1, i32* %2
br label %37
35: ; preds = %30
br label %36
36: ; preds = %35, %25
store i32 0, i32* %2
br label %37
37: ; preds = %36, %34, %24
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.passwd = type { i8* }
@.str = [22 x i8] c"/tmp/alsaplayer_%s_%d\00"
@.str.1 = [10 x i8] c"anonymous\00"
define i32 @ap_session_running(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca [1024 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = tail call i32 (...) @geteuid()
%7 = tail call %struct.passwd* @getpwuid(i32 %6)
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%11 = load i8*, i8** %10
br label %12
12: ; preds = %1, %9
%13 = phi i8* [ %11, %9 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %1 ]
%14 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %0)
%15 = call i64 @stat(i8* %5, %struct.stat* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %12
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i64 @S_ISSOCK(i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %17
%23 = call i64 @ap_ping(i32 %0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %22, %17
br label %26
26: ; preds = %22, %12, %25
%27 = phi i32 [ 0, %25 ], [ 0, %12 ], [ 1, %22 ]
ret i32 %27
}