#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;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setaddr(struct sockaddr_un *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int serve(void) {
  struct sockaddr_un address;
  setaddr(&address);
  unlink(address.sun_path);
  int fd = socket(PF_UNIX, SOCK_DGRAM, 0);
  if (fd < 0) {
    perror("Couldn't create a unix domain socket\n");
    exit(EXIT_FAILURE);
  }
  if (bind(fd, (struct sockaddr *)&address, sizeof(address)) != 0) {
    perror("Couldn't open control socket");
    exit(EXIT_FAILURE);
  }
  return fd;
}


x86 assembly (clang, -O0):
<serve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
call   1040 <setaddr@plt>
mov    -0x8(%rbp),%edi
call   1030 <unlink@plt>
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b9 <serve+0x59>
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
je     11ee <serve+0x8e>
lea    0xe4b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1040 <setaddr@plt>
mov    0x8(%rsp),%edi
call   1030 <unlink@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11b5 <serve+0x55>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1050 <bind@plt>
test   %rax,%rax
jne    11be <serve+0x5e>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe44(%rip),%rdi
jmp    11c5 <serve+0x65>
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [38 x i8] c"Couldn't create a unix domain socket\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"Couldn't open control socket\00"
define i32 @serve() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i32
%3 = call i32 @setaddr(%struct.sockaddr_un* %1)
%4 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i32 0, i32 0
%5 = load i32, i32* %4
%6 = call i32 @unlink(i32 %5)
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i64 @bind(i32 %17, %struct.sockaddr* %18, i32 4)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = call i32 @exit(i32 %23)
unreachable
25: ; preds = %16
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [38 x i8] c"Couldn't create a unix domain socket\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"Couldn't open control socket\00"
define i32 @serve() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = call i32 @setaddr(%struct.sockaddr_un* %1)
%4 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = call i32 @unlink(i32 %5)
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%17 = call i64 @bind(i32 %9, %struct.sockaddr* %16, i32 4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %15
ret i32 %9
}