#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char *,
                            char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char *, int);
int make_sock(struct sockaddr_un *sa, const char *ifname, int unpriv) {
  int fd;
  int flags;
  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    return -1;
  if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
      fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
    close(fd);
    return -1;
  }
  if ((flags = fcntl(fd, F_GETFL, 0)) == -1 ||
      fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
    close(fd);
    return -1;
  }
  memset(sa, 0, sizeof(*sa));
  sa->sun_family = AF_UNIX;
  if (unpriv)
    strlcpy(sa->sun_path,
            "/var/run"
            "/"
            "dhcpcd"
            ".unpriv.sock",
            sizeof(sa->sun_path));
  else {
    snprintf(sa->sun_path, sizeof(sa->sun_path),
             "/var/run"
             "/"
             "dhcpcd"
             "%s%s.sock",
             ifname ? "-" : "", ifname ? ifname : "");
  }
  return fd;
}


x86 assembly (clang, -O0):
<make_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11a4 <make_sock+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <make_sock+0x1ad>
mov    -0x20(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     11e9 <make_sock+0x89>
mov    -0x20(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    0x2ddf(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11fd <make_sock+0x9d>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <make_sock+0x1ad>
mov    -0x20(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1242 <make_sock+0xe2>
mov    -0x20(%rbp),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    0x2db6(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    1256 <make_sock+0xf6>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130d <make_sock+0x1ad>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
cmpl   $0x0,-0x1c(%rbp)
je     129c <make_sock+0x13c>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
mov    $0x4,%edx
call   1070 <strlcpy@plt>
jmp    1307 <make_sock+0x1a7>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdx
lea    0xd86(%rip),%rax
lea    0xd7e(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12db <make_sock+0x17b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    12eb <make_sock+0x18b>
lea    0xd54(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    12eb <make_sock+0x18b>
mov    -0x30(%rbp),%rcx
mov    -0x34(%rbp),%edi
mov    -0x40(%rbp),%r8
mov    $0x4,%esi
lea    0xd1a(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_sock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e65(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     124c <make_sock+0xec>
mov    %eax,%ebx
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1245 <make_sock+0xe5>
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dee(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1245 <make_sock+0xe5>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1245 <make_sock+0xe5>
mov    0x2dd0(%rip),%rcx
mov    (%rcx),%esi
mov    0x2de7(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1245 <make_sock+0xe5>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x4(%r12)
mov    (%r12),%edi
test   %r15d,%r15d
je     125d <make_sock+0xfd>
lea    0xdc7(%rip),%rsi
mov    $0x4,%edx
call   1070 <strlcpy@plt>
jmp    128a <make_sock+0x12a>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r14,%r14
lea    0xdcf(%rip),%rax
lea    0xdc7(%rip),%rcx
cmove  %rax,%rcx
cmove  %rax,%r14
lea    0xd9f(%rip),%rdx
mov    $0x4,%esi
mov    %r14,%r8
call   1040 <snprintf@plt>
mov    %ebx,%ebp
jmp    124c <make_sock+0xec>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [28 x i8] c"/var/run/dhcpcd.unpriv.sock\00"
@.str.1 = [25 x i8] c"/var/run/dhcpcd%s%s.sock\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [1 x i8] zeroinitializer
define i32 @make_sock(%struct.sockaddr_un* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_un*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.sockaddr_un* %0, %struct.sockaddr_un** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %78
15: ; preds = %3
%16 = load i32, i32* %8
%17 = load i32, i32* @F_GETFD
%18 = call i32 @fcntl(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %9
%19 = icmp eq i32 %18, -1
br i1 %19, label %28, label %20
20: ; preds = %15
%21 = load i32, i32* %8
%22 = load i32, i32* @F_SETFD
%23 = load i32, i32* %9
%24 = load i32, i32* @FD_CLOEXEC
%25 = or i32 %23, %24
%26 = call i32 @fcntl(i32 %21, i32 %22, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %20, %15
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %4
br label %78
31: ; preds = %20
%32 = load i32, i32* %8
%33 = load i32, i32* @F_GETFL
%34 = call i32 @fcntl(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %9
%35 = icmp eq i32 %34, -1
br i1 %35, label %44, label %36
36: ; preds = %31
%37 = load i32, i32* %8
%38 = load i32, i32* @F_SETFL
%39 = load i32, i32* %9
%40 = load i32, i32* @O_NONBLOCK
%41 = or i32 %39, %40
%42 = call i32 @fcntl(i32 %37, i32 %38, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %36, %31
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %4
br label %78
47: ; preds = %36
%48 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%49 = call i32 @memset(%struct.sockaddr_un* %48, i32 0, i32 8)
%50 = load i32, i32* @AF_UNIX
%51 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%52 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %51, i32 0, i32 1
store i32 %50, i32* %52
%53 = load i32, i32* %7
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %47
%56 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%57 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @strlcpy(i32 %58, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 4)
br label %76
60: ; preds = %47
%61 = load %struct.sockaddr_un*, %struct.sockaddr_un** %5
%62 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = load i8*, i8** %6
%65 = icmp ne i8* %64, null
%66 = zext i1 %65 to i64
%67 = select i1 %65, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0)
%68 = load i8*, i8** %6
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %72
70: ; preds = %60
%71 = load i8*, i8** %6
br label %73
72: ; preds = %60
br label %73
73: ; preds = %72, %70
%74 = phi i8* [ %71, %70 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), %72 ]
%75 = call i32 @snprintf(i32 %63, i32 4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %67, i8* %74)
br label %76
76: ; preds = %73, %55
%77 = load i32, i32* %8
store i32 %77, i32* %4
br label %78
78: ; preds = %76, %44, %28, %14
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFD = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str = [28 x i8] c"/var/run/dhcpcd.unpriv.sock\00"
@.str.1 = [25 x i8] c"/var/run/dhcpcd%s%s.sock\00"
@.str.2 = [2 x i8] c"-\00"
@.str.3 = [1 x i8] zeroinitializer
define i32 @make_sock(%struct.sockaddr_un* %0, i8* %1, i32 %2) {
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %46, label %8
8: ; preds = %3
%9 = load i32, i32* @F_GETFD
%10 = tail call i32 @fcntl(i32 %6, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %18, label %12
12: ; preds = %8
%13 = load i32, i32* @F_SETFD
%14 = load i32, i32* @FD_CLOEXEC
%15 = or i32 %14, %10
%16 = tail call i32 @fcntl(i32 %6, i32 %13, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12, %8
%19 = tail call i32 @close(i32 %6)
br label %46
20: ; preds = %12
%21 = load i32, i32* @F_GETFL
%22 = tail call i32 @fcntl(i32 %6, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* @O_NONBLOCK
%27 = or i32 %26, %22
%28 = tail call i32 @fcntl(i32 %6, i32 %25, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %24, %20
%31 = tail call i32 @close(i32 %6)
br label %46
32: ; preds = %24
%33 = tail call i32 @memset(%struct.sockaddr_un* %0, i32 0, i32 8)
%34 = load i32, i32* @AF_UNIX
%35 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 1
store i32 %34, i32* %35
%36 = icmp eq i32 %2, 0
%37 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %0, i64 0, i32 0
%38 = load i32, i32* %37
br i1 %36, label %41, label %39
39: ; preds = %32
%40 = tail call i32 @strlcpy(i32 %38, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 4)
br label %46
41: ; preds = %32
%42 = icmp eq i8* %1, null
%43 = select i1 %42, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%44 = select i1 %42, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i8* %1
%45 = tail call i32 @snprintf(i32 %38, i32 4, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %43, i8* %44)
br label %46
46: ; preds = %39, %41, %3, %30, %18
%47 = phi i32 [ -1, %18 ], [ -1, %30 ], [ -1, %3 ], [ %6, %41 ], [ %6, %39 ]
ret i32 %47
}