#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 sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
struct passwd {
  char *pw_name;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ap_connect_session(int session) {
  int socket_fd;
  struct sockaddr_un saddr;
  struct passwd *pwd;
  pwd = getpwuid(geteuid());
  if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) {
    saddr.sun_family = AF_UNIX;
    snprintf(saddr.sun_path, sizeof(saddr.sun_path), "/tmp/alsaplayer_%s_%d",
             pwd == NULL ? "anonymous" : pwd->pw_name, session);
    if (connect(socket_fd, (struct sockaddr *)&saddr, sizeof(saddr)) != -1) {
      if (socket_fd < 0)
        close(socket_fd);
      return socket_fd;
    }
  }
  close(socket_fd);
  return -1;
}


x86 assembly (clang, -O0):
<ap_connect_session>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
je     1232 <ap_connect_session+0xd2>
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cf <ap_connect_session+0x6f>
lea    0xe50(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11da <ap_connect_session+0x7a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rcx
mov    -0x8(%rbp),%r8d
mov    $0x4,%esi
lea    0xe0f(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     122d <ap_connect_session+0xcd>
cmpl   $0x0,-0xc(%rbp)
jge    1222 <ap_connect_session+0xc2>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1241 <ap_connect_session+0xe1>
jmp    1232 <ap_connect_session+0xd2>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ap_connect_session>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%edi
call   1030 <getpwuid@plt>
mov    %rax,%rbp
mov    0x2e61(%rip),%r12
mov    (%r12),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11f3 <ap_connect_session+0x93>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
test   %rbp,%rbp
je     11bb <ap_connect_session+0x5b>
mov    0x0(%rbp),%rcx
jmp    11c2 <ap_connect_session+0x62>
lea    0xe54(%rip),%rcx
lea    0xe37(%rip),%rdx
mov    $0x4,%esi
mov    %r14d,%r8d
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11f3 <ap_connect_session+0x93>
mov    %ebx,%r15d
test   %ebx,%ebx
jns    11fd <ap_connect_session+0x9d>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15d,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.passwd = type { i8* }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"/tmp/alsaplayer_%s_%d\00"
@.str.1 = [10 x i8] c"anonymous\00"
define i32 @ap_connect_session(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca %struct.passwd*
store i32 %0, i32* %3
%7 = call i32 (...) @geteuid()
%8 = call %struct.passwd* @getpwuid(i32 %7)
store %struct.passwd* %8, %struct.passwd** %6
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %42
13: ; preds = %1
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load %struct.passwd*, %struct.passwd** %6
%19 = icmp eq %struct.passwd* %18, null
br i1 %19, label %20, label %21
20: ; preds = %13
br label %25
21: ; preds = %13
%22 = load %struct.passwd*, %struct.passwd** %6
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %22, i32 0, i32 0
%24 = load i8*, i8** %23
br label %25
25: ; preds = %21, %20
%26 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), %20 ], [ %24, %21 ]
%27 = load i32, i32* %3
%28 = call i32 @snprintf(i32 %17, i32 4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %26, i32 %27)
%29 = load i32, i32* %4
%30 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 8)
%32 = icmp ne i32 %31, -1
br i1 %32, label %33, label %41
33: ; preds = %25
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %4
store i32 %40, i32* %2
br label %45
41: ; preds = %25
br label %42
42: ; preds = %41, %1
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
store i32 -1, i32* %2
br label %45
45: ; preds = %42, %39
%46 = load i32, i32* %2
ret i32 %46
}

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