#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;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ memset(int /*<<< orphan*/, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int opensocket(const char *name) {
  int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  struct sockaddr_un addr = {.sun_family = AF_UNIX};
  memset(addr.sun_path, 0, 108);
  strncpy(addr.sun_path, name, 108 - 1);
  unlink(addr.sun_path);
  if (fd < 0)
    die("cannot create socket: %m\n");
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    die("cannot bind %s: %m\n", addr.sun_path);
  return fd;
}


x86 assembly (clang, -O0):
<opensocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
xor    %esi,%esi
mov    $0x6c,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x6b,%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%edi
call   1030 <unlink@plt>
cmpl   $0x0,-0xc(%rbp)
jge    11db <opensocket+0x7b>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1207 <opensocket+0xa7>
mov    -0x18(%rbp),%esi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opensocket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e75(%rip),%r15
mov    (%r15),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
movl   $0x0,0x8(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
xor    %edi,%edi
xor    %esi,%esi
mov    $0x6c,%edx
call   1050 <memset@plt>
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x6b,%edx
call   1040 <strncpy@plt>
xor    %edi,%edi
call   1030 <unlink@plt>
test   %ebx,%ebx
jns    11cd <opensocket+0x6d>
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %rax,%rax
jns    11f5 <opensocket+0x95>
mov    0x8(%rsp),%esi
lea    0xe2c(%rip),%rdi
xor    %eax,%eax
call   1070 <die@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [26 x i8] c"cannot create socket: %m\0A\00"
@.str.1 = [20 x i8] c"cannot bind %s: %m\0A\00"
define i32 @opensocket(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
store i8* %0, i8** %2
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_DGRAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
%10 = load i32, i32* @AF_UNIX
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @memset(i32 %12, i32 0, i32 108)
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i8*, i8** %2
%17 = call i32 @strncpy(i32 %15, i8* %16, i32 107)
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @unlink(i32 %19)
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %1
%26 = load i32, i32* %3
%27 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%28 = call i64 @bind(i32 %26, %struct.sockaddr* %27, i32 8)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %30, %25
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [26 x i8] c"cannot create socket: %m\0A\00"
@.str.1 = [20 x i8] c"cannot bind %s: %m\0A\00"
define i32 @opensocket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = load i32, i32* @AF_UNIX
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_un* %2 to i8*
%7 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
%9 = load i32, i32* @AF_UNIX
store i32 %9, i32* %8
%10 = tail call i32 @memset(i32 0, i32 0, i32 108)
%11 = tail call i32 @strncpy(i32 0, i8* %0, i32 107)
%12 = tail call i32 @unlink(i32 0)
%13 = icmp slt i32 %5, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %1
%17 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%18 = call i64 @bind(i32 %5, %struct.sockaddr* %17, i32 8)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %7
%22 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %16
ret i32 %5
}