#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*/ where;
struct stat {
  int dummy;
};
struct sockaddr_un {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ close(int);
char *dirname(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(char *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ make_valid_path(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int open_unixsock(struct in_addr inetaddr) {
  struct sockaddr_un where;
  struct stat st;
  char *dir;
  int s;
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "socket: %s", strerror(errno));
    return s;
  }
  where.sun_family = AF_UNIX;
  snprintf(where.sun_path, sizeof(where.sun_path),
           "/var/run/pptp/"
           "%s",
           inet_ntoa(inetaddr));
  if (stat(where.sun_path, &st) >= 0) {
    fprintf(stderr, "Call manager for %s is already running.\n",
            inet_ntoa(inetaddr));
    fflush(stderr);
    close(s);
    return -1;
  }
  dir = dirname(where.sun_path);
  if (!make_valid_path(dir, 0770))
    fprintf(stderr, "Could not make path to %s: %s\n", where.sun_path,
            strerror(errno));
  free(dir);
  if (bind(s, (struct sockaddr *)&where, sizeof(where)) < 0) {
    fprintf(stderr, "bind: %s", strerror(errno));
    close(s);
    return -1;
  }
  chmod(where.sun_path, 0777);
  listen(s, 127);
  return s;
}


x86 assembly (clang, -O0):
<open_unixsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1249 <open_unixsock+0x69>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xdc9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13c3 <open_unixsock+0x1e3>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rcx
mov    $0x8,%esi
lea    0xd93(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jl     12de <open_unixsock+0xfe>
mov    0x2d3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c3 <open_unixsock+0x1e3>
mov    -0x18(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1f8,%esi
call   10c0 <make_valid_path@plt>
cmp    $0x0,%eax
jne    133c <open_unixsock+0x15c>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd10(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10e0 <bind@plt>
cmp    $0x0,%rax
jge    13a2 <open_unixsock+0x1c2>
mov    0x2c71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2c55(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x50(%rbp),%edi
mov    %rax,%rdx
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c3 <open_unixsock+0x1e3>
mov    -0x18(%rbp),%rdi
mov    $0x1ff,%esi
call   10d0 <chmod@plt>
mov    -0x2c(%rbp),%edi
mov    $0x7f,%esi
call   10b0 <listen@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_unixsock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2df5(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1279 <open_unixsock+0x99>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <inet_ntoa@plt>
lea    0xdeb(%rip),%rdx
mov    $0x8,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
mov    %rsp,%rsi
call   1090 <stat@plt>
test   %rax,%rax
js     12a5 <open_unixsock+0xc5>
mov    0x2d97(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1040 <inet_ntoa@plt>
lea    0xdca(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r15),%edi
call   10a0 <fflush@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r14d
jmp    1323 <open_unixsock+0x143>
mov    0x2d58(%rip),%rax
mov    (%rax),%ebp
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd69(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1323 <open_unixsock+0x143>
call   1060 <dirname@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1f8,%esi
call   10c0 <make_valid_path@plt>
test   %eax,%eax
jne    12e8 <open_unixsock+0x108>
mov    0x2d13(%rip),%rax
mov    (%rax),%ebx
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd69(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   10e0 <bind@plt>
test   %rax,%rax
js     1331 <open_unixsock+0x151>
mov    0x8(%rsp),%rdi
mov    $0x1ff,%esi
call   10d0 <chmod@plt>
mov    %r14d,%edi
mov    $0x7f,%esi
call   10b0 <listen@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ca0(%rip),%rax
mov    (%rax),%ebx
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd15(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    1266 <open_unixsock+0x86>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_un = type { i8*, i32 }
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [11 x i8] c"socket: %s\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"/var/run/pptp/%s\00"
@.str.2 = [41 x i8] c"Call manager for %s is already running.\0A\00"
@.str.3 = [31 x i8] c"Could not make path to %s: %s\0A\00"
@.str.4 = [9 x i8] c"bind: %s\00"
define i32 @open_unixsock(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.in_addr
%4 = alloca %struct.sockaddr_un
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i32
%8 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
store i32 %0, i32* %8
%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* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %7
store i32 %18, i32* %2
br label %77
19: ; preds = %1
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i8* @inet_ntoa(i32 %25)
%27 = call i32 @snprintf(i8* %23, i32 8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i64 @stat(i8* %29, %struct.stat* %5)
%31 = icmp sge i64 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %19
%33 = load i32, i32* @stderr
%34 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i8* @inet_ntoa(i32 %35)
%37 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @stderr
%39 = call i32 @fflush(i32 %38)
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %2
br label %77
42: ; preds = %19
%43 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = call i8* @dirname(i8* %44)
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = call i32 @make_valid_path(i8* %46, i32 504)
%48 = icmp ne i32 %47, 0
br i1 %48, label %56, label %49
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%52 = load i8*, i8** %51
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %52, i8* %54)
br label %56
56: ; preds = %49, %42
%57 = load i8*, i8** %6
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %7
%60 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%61 = call i64 @bind(i32 %59, %struct.sockaddr* %60, i32 16)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %56
%64 = load i32, i32* @stderr
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %66)
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %2
br label %77
70: ; preds = %56
%71 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = call i32 @chmod(i8* %72, i32 511)
%74 = load i32, i32* %7
%75 = call i32 @listen(i32 %74, i32 127)
%76 = load i32, i32* %7
store i32 %76, i32* %2
br label %77
77: ; preds = %70, %63, %32, %13
%78 = load i32, i32* %2
ret i32 %78
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [11 x i8] c"socket: %s\00"
@errno = global i32 0
@.str.1 = [17 x i8] c"/var/run/pptp/%s\00"
@.str.2 = [41 x i8] c"Call manager for %s is already running.\0A\00"
@.str.3 = [31 x i8] c"Could not make path to %s: %s\0A\00"
@.str.4 = [9 x i8] c"bind: %s\00"
define i32 @open_unixsock(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.stat
%4 = bitcast %struct.sockaddr_un* %2 to i8*
%5 = bitcast %struct.stat* %3 to i8*
%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 slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %54
15: ; preds = %1
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%19 = tail call i8* @inet_ntoa(i32 %0)
%20 = tail call i32 @snprintf(i8* undef, i32 8, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i64 @stat(i8* undef, %struct.stat* %3)
%22 = icmp sgt i64 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %15
%24 = load i32, i32* @stderr
%25 = call i8* @inet_ntoa(i32 %0)
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @stderr
%28 = call i32 @fflush(i32 %27)
%29 = call i32 @close(i32 %8)
br label %54
30: ; preds = %15
%31 = call i8* @dirname(i8* undef)
%32 = call i32 @make_valid_path(i8* %31, i32 504)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* undef, i8* %37)
br label %39
39: ; preds = %34, %30
%40 = call i32 @free(i8* %31)
%41 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%42 = call i64 @bind(i32 %8, %struct.sockaddr* %41, i32 16)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* %47)
%49 = call i32 @close(i32 %8)
br label %54
50: ; preds = %39
%51 = load i8*, i8** %18
%52 = call i32 @chmod(i8* %51, i32 511)
%53 = call i32 @listen(i32 %8, i32 127)
br label %54
54: ; preds = %50, %44, %23, %10
%55 = phi i32 [ %8, %10 ], [ -1, %23 ], [ -1, %44 ], [ %8, %50 ]
ret i32 %55
}