#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 {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int /*<<< orphan*/ sa_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ SOCK_DGRAM;
int SUN_LEN(struct sockaddr_un *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ elog(int /*<<< orphan*/, int /*<<< orphan*/, char *,
                        int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int umask(int);
int open_sock(const char *uri) {
  int namelen, saved_umask, ret, sk;
  union {
    struct sockaddr sa;
    struct sockaddr_un un;
  } name = {};
  name.un.sun_family = AF_UNIX;
  strncpy(name.un.sun_path, uri, sizeof(name.un.sun_path));
  namelen = SUN_LEN(&name.un);
  if (name.un.sun_path[0] == '@')
    name.un.sun_path[0] = 0;
  ret = sk = socket(name.sa.sa_family, SOCK_DGRAM, 0);
  if (ret < 0) {
    elog(LOG_WARNING, errno, "socket %i dgram 0", name.sa.sa_family);
    return ret;
  }
  fcntl(sk, F_SETFD, fcntl(sk, F_GETFD) | FD_CLOEXEC);
  saved_umask = umask(0);
  ret = bind(sk, &name.sa, namelen);
  umask(saved_umask);
  return sk;
}


x86 assembly (clang, -O0):
<open_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe7d(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
lea    -0x30(%rbp),%rdi
call   1080 <SUN_LEN@plt>
mov    %eax,-0x14(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x40,%eax
jne    11d3 <open_sock+0x63>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x20(%rbp)
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1222 <open_sock+0xb2>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%ecx
lea    0xdfe(%rip),%rdx
call   1060 <elog@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1289 <open_sock+0x119>
mov    -0x20(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    %eax,%edx
mov    0x2d6a(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
xor    %edi,%edi
call   1050 <umask@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <bind@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <umask@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_sock>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
xor    %edi,%edi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rdi
call   1080 <SUN_LEN@plt>
mov    %eax,%r14d
mov    (%rsp),%rdi
cmpb   $0x40,(%rdi)
jne    11b5 <open_sock+0x45>
movb   $0x0,(%rdi)
mov    (%rsp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     121b <open_sock+0xab>
mov    0x2df6(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2dd3(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
xor    %edi,%edi
call   1050 <umask@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %r14d,%edx
call   1070 <bind@plt>
mov    %ebx,%edi
call   1050 <umask@plt>
jmp    123c <open_sock+0xcc>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    (%rsp),%ecx
lea    0xdc9(%rip),%rdx
call   1060 <elog@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%union.anon = type { %struct.sockaddr_un }
%struct.sockaddr_un = type { i8*, i32 }
@__const.open_sock.name = { %struct.sockaddr, [12 x i8] } { %struct.sockaddr zeroinitializer, [12 x i8] undef }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@LOG_WARNING = global i32 0
@errno = global i32 0
@.str = [18 x i8] c"socket %i dgram 0\00"
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_sock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %union.anon
store i8* %0, i8** %3
%9 = bitcast %union.anon* %8 to i8*
%10 = load i32, i32* @AF_UNIX
%11 = bitcast %union.anon* %8 to %struct.sockaddr_un*
%12 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %11, i32 0, i32 1
store i32 %10, i32* %12
%13 = bitcast %union.anon* %8 to %struct.sockaddr_un*
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %13, i32 0, i32 0
%15 = load i8*, i8** %14
%16 = load i8*, i8** %3
%17 = call i32 @strncpy(i8* %15, i8* %16, i32 8)
%18 = bitcast %union.anon* %8 to %struct.sockaddr_un*
%19 = call i32 @SUN_LEN(%struct.sockaddr_un* %18)
store i32 %19, i32* %4
%20 = bitcast %union.anon* %8 to %struct.sockaddr_un*
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 64
br i1 %26, label %27, label %32
27: ; preds = %1
%28 = bitcast %union.anon* %8 to %struct.sockaddr_un*
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %28, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = getelementptr inbounds i8, i8* %30, i64 0
store i8 0, i8* %31
br label %32
32: ; preds = %27, %1
%33 = bitcast %union.anon* %8 to %struct.sockaddr*
%34 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @SOCK_DGRAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %7
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %48
40: ; preds = %32
%41 = load i32, i32* @LOG_WARNING
%42 = load i32, i32* @errno
%43 = bitcast %union.anon* %8 to %struct.sockaddr*
%44 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @elog(i32 %41, i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %6
store i32 %47, i32* %2
br label %65
48: ; preds = %32
%49 = load i32, i32* %7
%50 = load i32, i32* @F_SETFD
%51 = load i32, i32* %7
%52 = load i32, i32* @F_GETFD
%53 = call i32 (i32, i32, ...) @fcntl(i32 %51, i32 %52)
%54 = load i32, i32* @FD_CLOEXEC
%55 = or i32 %53, %54
%56 = call i32 (i32, i32, ...) @fcntl(i32 %49, i32 %50, i32 %55)
%57 = call i32 @umask(i32 0)
store i32 %57, i32* %5
%58 = load i32, i32* %7
%59 = bitcast %union.anon* %8 to %struct.sockaddr*
%60 = load i32, i32* %4
%61 = call i32 @bind(i32 %58, %struct.sockaddr* %59, i32 %60)
store i32 %61, i32* %6
%62 = load i32, i32* %5
%63 = call i32 @umask(i32 %62)
%64 = load i32, i32* %7
store i32 %64, i32* %2
br label %65
65: ; preds = %48, %40
%66 = load i32, i32* %2
ret i32 %66
}

LLVM IR (-O2):
%union.anon = type { %struct.sockaddr_un }
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@LOG_WARNING = global i32 0
@errno = global i32 0
@.str = [18 x i8] c"socket %i dgram 0\00"
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_sock(i8* %0) {
%2 = alloca %union.anon
%3 = bitcast %union.anon* %2 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = getelementptr inbounds %union.anon, %union.anon* %2, i64 0, i32 0
%6 = getelementptr inbounds %union.anon, %union.anon* %2, i64 0, i32 0, i32 1
store i32 %4, i32* %6
%7 = getelementptr inbounds %union.anon, %union.anon* %2, i64 0, i32 0, i32 0
%8 = tail call i32 @strncpy(i8* null, i8* %0, i32 8)
%9 = call i32 @SUN_LEN(%struct.sockaddr_un* %5)
%10 = load i8*, i8** %7
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 64
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = ptrtoint i8* %10 to i64
%15 = trunc i64 %14 to i32
br label %19
16: ; preds = %1
store i8 0, i8* %10
%17 = bitcast %union.anon* %2 to i32*
%18 = load i32, i32* %17
br label %19
19: ; preds = %13, %16
%20 = phi i32 [ %18, %16 ], [ %15, %13 ]
%21 = load i32, i32* @SOCK_DGRAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %19
%25 = bitcast %union.anon* %2 to i32*
%26 = load i32, i32* @LOG_WARNING
%27 = load i32, i32* @errno
%28 = load i32, i32* %25
%29 = call i32 @elog(i32 %26, i32 %27, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %41
30: ; preds = %19
%31 = bitcast %union.anon* %2 to %struct.sockaddr*
%32 = load i32, i32* @F_SETFD
%33 = load i32, i32* @F_GETFD
%34 = call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %33)
%35 = load i32, i32* @FD_CLOEXEC
%36 = or i32 %35, %34
%37 = call i32 (i32, i32, ...) @fcntl(i32 %22, i32 %32, i32 %36)
%38 = call i32 @umask(i32 0)
%39 = call i32 @bind(i32 %22, %struct.sockaddr* %31, i32 %9)
%40 = call i32 @umask(i32 %38)
br label %41
41: ; preds = %30, %24
ret i32 %22
}