#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*/ sun;
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ EEXIST;
int SOCK_CLOEXEC;
int SOCK_DGRAM;
int SOCK_NONBLOCK;
int asprintf(char **, char *, char *, int /*<<< orphan*/);
int bind(int, struct sockaddr *, scalar_t__);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, scalar_t__);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getlogin();
int getpid();
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int mkdir(char *, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *, ...);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
char *strdup(int /*<<< orphan*/);
scalar_t__ strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int wpa_open(const char *ifname, char **path) {
  static int counter;
  int fd;
  socklen_t len;
  struct sockaddr_un sun;
  char *tmpdir;
  if (asprintf(&tmpdir, "%s-%s", "/tmp/dhcpcd", getlogin()) == -1)
    return -1;
  if (mkdir(tmpdir, 0700) == -1 && errno != EEXIST) {
    free(tmpdir);
    return -1;
  }
  if ((fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) ==
      -1) {
    free(tmpdir);
    return -1;
  }
  memset(&sun, 0, sizeof(sun));
  sun.sun_family = AF_UNIX;
  snprintf(sun.sun_path, sizeof(sun.sun_path), "%s/libdhcpcd-wpa-%d.%d", tmpdir,
           getpid(), counter++);
  *path = strdup(sun.sun_path);
  len = (socklen_t)(sizeof(*(&sun)) - sizeof((&sun)->sun_path) +
                    strlen((&sun)->sun_path));
  if (bind(fd, (struct sockaddr *)&sun, len) == -1)
    goto failure;
  snprintf(sun.sun_path, sizeof(sun.sun_path),
           "/var/run/wpa_supplicant"
           "/%s",
           ifname);
  len = (socklen_t)(sizeof(*(&sun)) - sizeof((&sun)->sun_path) +
                    strlen((&sun)->sun_path));
  if (connect(fd, (struct sockaddr *)&sun, len) == -1)
    goto failure;
  free(tmpdir);
  return fd;
failure:
  free(tmpdir);
  close(fd);
  unlink(*path);
  free(*path);
  *path = NULL;
  return -1;
}


x86 assembly (clang, -O0):
<wpa_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <getlogin@plt>
mov    %eax,%ecx
lea    -0x38(%rbp),%rdi
lea    0xdfc(%rip),%rsi
lea    0xdfb(%rip),%rdx
call   10c0 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1225 <wpa_open+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ef <wpa_open+0x20f>
mov    -0x38(%rbp),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    126b <wpa_open+0x8b>
mov    0x2d75(%rip),%rax
mov    (%rax),%rax
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%rax
je     126b <wpa_open+0x8b>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ef <wpa_open+0x20f>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
or     (%rax),%esi
mov    0x2d33(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    12b7 <wpa_open+0xd7>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ef <wpa_open+0x20f>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
mov    0x2d9f(%rip),%r9d
mov    %r9d,%eax
add    $0x1,%eax
mov    %eax,0x2d93(%rip)
mov    $0x4,%esi
lea    0xd01(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    -0x30(%rbp),%edi
call   10f0 <strdup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%edi
call   1070 <strlen@plt>
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1358 <wpa_open+0x178>
jmp    13b4 <wpa_open+0x1d4>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xcbe(%rip),%rdx
mov    $0x0,%al
call   1080 <snprintf@plt>
mov    -0x30(%rbp),%edi
call   1070 <strlen@plt>
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    13a0 <wpa_open+0x1c0>
jmp    13b4 <wpa_open+0x1d4>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ef <wpa_open+0x20f>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <unlink@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wpa_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   10b0 <getlogin@plt>
lea    0xdfe(%rip),%rsi
lea    0xdfd(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    %eax,%ecx
call   10c0 <asprintf@plt>
mov    $0xffffffff,%r13d
cmp    $0xffffffff,%eax
je     1380 <wpa_open+0x1a0>
mov    0x10(%rsp),%rdi
mov    $0x1c0,%esi
call   1050 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1252 <wpa_open+0x72>
mov    0x2d79(%rip),%rax
mov    (%rax),%rax
mov    0x2d7f(%rip),%rcx
cmp    (%rcx),%rax
jne    134c <wpa_open+0x16c>
mov    0x2d87(%rip),%rbx
mov    0x2d78(%rip),%rax
mov    0x2d89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    (%rbx),%edi
mov    0x2d4c(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
je     134c <wpa_open+0x16c>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%ebx
mov    0x10(%rsp),%r12
xor    %eax,%eax
call   1060 <getpid@plt>
mov    0x2de2(%rip),%r9d
lea    0x1(%r9),%ecx
mov    %ecx,0x2dd8(%rip)
lea    0xd4b(%rip),%rdx
mov    %ebx,%edi
mov    $0x4,%esi
mov    %r12,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    0x8(%rsp),%edi
call   10f0 <strdup@plt>
mov    %rax,(%r14)
mov    0x8(%rsp),%edi
call   1070 <strlen@plt>
lea    0x4(%rax),%rdx
mov    %ebp,%edi
lea    0x8(%rsp),%rsi
call   10d0 <bind@plt>
cmp    $0xffffffff,%eax
je     1358 <wpa_open+0x178>
mov    0x8(%rsp),%edi
lea    0xd19(%rip),%rdx
mov    $0x4,%esi
mov    %r15,%rcx
xor    %eax,%eax
call   1080 <snprintf@plt>
mov    0x8(%rsp),%edi
call   1070 <strlen@plt>
lea    0x4(%rax),%rdx
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     1358 <wpa_open+0x178>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%r13d
jmp    1380 <wpa_open+0x1a0>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
jmp    1380 <wpa_open+0x1a0>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
mov    (%r14),%rdi
call   1040 <unlink@plt>
mov    (%r14),%rdi
call   1030 <free@plt>
movq   $0x0,(%r14)
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@wpa_open.counter = internal global i32 0
@.str = [6 x i8] c"%s-%s\00"
@.str.1 = [12 x i8] c"/tmp/dhcpcd\00"
@errno = global i64 0
@EEXIST = global i64 0
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_CLOEXEC = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str.2 = [23 x i8] c"%s/libdhcpcd-wpa-%d.%d\00"
@.str.3 = [27 x i8] c"/var/run/wpa_supplicant/%s\00"
define i32 @wpa_open(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.sockaddr_un
%9 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
%10 = call i32 (...) @getlogin()
%11 = call i32 @asprintf(i8** %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %93
14: ; preds = %2
%15 = load i8*, i8** %9
%16 = call i32 @mkdir(i8* %15, i32 448)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = load i64, i64* @errno
%20 = load i64, i64* @EEXIST
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i8*, i8** %9
%24 = call i32 @free(i8* %23)
store i32 -1, i32* %3
br label %93
25: ; preds = %18, %14
%26 = load i32, i32* @AF_UNIX
%27 = load i32, i32* @SOCK_DGRAM
%28 = load i32, i32* @SOCK_CLOEXEC
%29 = or i32 %27, %28
%30 = load i32, i32* @SOCK_NONBLOCK
%31 = or i32 %29, %30
%32 = call i32 @socket(i32 %26, i32 %31, i32 0)
store i32 %32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load i8*, i8** %9
%36 = call i32 @free(i8* %35)
store i32 -1, i32* %3
br label %93
37: ; preds = %25
%38 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 8)
%39 = load i32, i32* @AF_UNIX
%40 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i8*, i8** %9
%44 = call i32 (...) @getpid()
%45 = load i32, i32* @wpa_open.counter
%46 = add nsw i32 %45, 1
store i32 %46, i32* @wpa_open.counter
%47 = call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 %42, i32 4, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %43, i32 %44, i32 %45)
%48 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @strdup(i32 %49)
%51 = load i8**, i8*** %5
store i8* %50, i8** %51
%52 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @strlen(i32 %53)
%55 = add i64 4, %54
store i64 %55, i64* %7
%56 = load i32, i32* %6
%57 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%58 = load i64, i64* %7
%59 = call i32 @bind(i32 %56, %struct.sockaddr* %57, i64 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %37
br label %81
62: ; preds = %37
%63 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%64 = load i32, i32* %63
%65 = load i8*, i8** %4
%66 = call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 %64, i32 4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %65)
%67 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%68 = load i32, i32* %67
%69 = call i64 @strlen(i32 %68)
%70 = add i64 4, %69
store i64 %70, i64* %7
%71 = load i32, i32* %6
%72 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%73 = load i64, i64* %7
%74 = call i32 @connect(i32 %71, %struct.sockaddr* %72, i64 %73)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %77
76: ; preds = %62
br label %81
77: ; preds = %62
%78 = load i8*, i8** %9
%79 = call i32 @free(i8* %78)
%80 = load i32, i32* %6
store i32 %80, i32* %3
br label %93
81: ; preds = %76, %61
%82 = load i8*, i8** %9
%83 = call i32 @free(i8* %82)
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = load i8**, i8*** %5
%87 = load i8*, i8** %86
%88 = call i32 @unlink(i8* %87)
%89 = load i8**, i8*** %5
%90 = load i8*, i8** %89
%91 = call i32 @free(i8* %90)
%92 = load i8**, i8*** %5
store i8* null, i8** %92
store i32 -1, i32* %3
br label %93
93: ; preds = %81, %77, %34, %22, %13
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@wpa_open.counter = internal unnamed_addr global i32 0
@.str = [6 x i8] c"%s-%s\00"
@.str.1 = [12 x i8] c"/tmp/dhcpcd\00"
@errno = global i64 0
@EEXIST = global i64 0
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_CLOEXEC = global i32 0
@SOCK_NONBLOCK = global i32 0
@.str.2 = [23 x i8] c"%s/libdhcpcd-wpa-%d.%d\00"
@.str.3 = [27 x i8] c"/var/run/wpa_supplicant/%s\00"
define i32 @wpa_open(i8* %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca i8*
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = tail call i32 (...) @getlogin()
%8 = call i32 @asprintf(i8** %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %71, label %10
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = call i32 @mkdir(i8* %11, i32 448)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %21
14: ; preds = %10
%15 = load i64, i64* @errno
%16 = load i64, i64* @EEXIST
%17 = icmp eq i64 %15, %16
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = call i32 @free(i8* %19)
br label %71
21: ; preds = %14, %10
%22 = load i32, i32* @AF_UNIX
%23 = load i32, i32* @SOCK_DGRAM
%24 = load i32, i32* @SOCK_CLOEXEC
%25 = or i32 %24, %23
%26 = load i32, i32* @SOCK_NONBLOCK
%27 = or i32 %25, %26
%28 = call i32 @socket(i32 %22, i32 %27, i32 0)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i8*, i8** %4
%32 = call i32 @free(i8* %31)
br label %71
33: ; preds = %21
%34 = call i32 @memset(%struct.sockaddr_un* %3, i32 0, i32 8)
%35 = load i32, i32* @AF_UNIX
%36 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%38 = load i32, i32* %37
%39 = load i8*, i8** %4
%40 = call i32 (...) @getpid()
%41 = load i32, i32* @wpa_open.counter
%42 = add nsw i32 %41, 1
store i32 %42, i32* @wpa_open.counter
%43 = call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 %38, i32 4, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %39, i32 %40, i32 %41)
%44 = load i32, i32* %37
%45 = call i8* @strdup(i32 %44)
store i8* %45, i8** %1
%46 = load i32, i32* %37
%47 = call i64 @strlen(i32 %46)
%48 = add i64 %47, 4
%49 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%50 = call i32 @bind(i32 %28, %struct.sockaddr* %49, i64 %48)
%51 = icmp eq i32 %50, -1
br i1 %51, label %63, label %52
52: ; preds = %33
%53 = load i32, i32* %37
%54 = call i32 (i32, i32, i8*, i8*, ...) @snprintf(i32 %53, i32 4, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%55 = load i32, i32* %37
%56 = call i64 @strlen(i32 %55)
%57 = add i64 %56, 4
%58 = call i32 @connect(i32 %28, %struct.sockaddr* %49, i64 %57)
%59 = icmp eq i32 %58, -1
br i1 %59, label %63, label %60
60: ; preds = %52
%61 = load i8*, i8** %4
%62 = call i32 @free(i8* %61)
br label %71
63: ; preds = %52, %33
%64 = load i8*, i8** %4
%65 = call i32 @free(i8* %64)
%66 = call i32 @close(i32 %28)
%67 = load i8*, i8** %1
%68 = call i32 @unlink(i8* %67)
%69 = load i8*, i8** %1
%70 = call i32 @free(i8* %69)
store i8* null, i8** %1
br label %71
71: ; preds = %2, %63, %60, %30, %18
%72 = phi i32 [ -1, %18 ], [ -1, %30 ], [ -1, %63 ], [ %28, %60 ], [ -1, %2 ]
ret i32 %72
}