#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*/ socklen_t;
typedef int /*<<< orphan*/ sockaddr;
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ mktemp(char *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ unlink(int /*<<< orphan*/);
int main() {
  struct sockaddr_un sockaddr;
  socklen_t socklen;
  char *sockname;
  mode_t mask;
  int status;
  int fd;
  sockname = strdup("proot-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXXXXXX");
  if (sockname == NULL)
    return 125;
  (void)mktemp(sockname);
  if (sockname[0] == '\0')
    return 125;
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  bzero(&sockaddr, sizeof(sockaddr));
  sockaddr.sun_family = AF_UNIX;
  assert(strlen(sockname) == sizeof(sockaddr.sun_path));
  memcpy(sockaddr.sun_path, sockname, sizeof(sockaddr.sun_path));
  chdir("/tmp");
  (void)unlink(sockaddr.sun_path);
  status = bind(fd, (const struct sockaddr *)&sockaddr, sizeof(sockaddr));
  if (status < 0) {
    perror("bind");
    exit(EXIT_FAILURE);
  }
  (void)unlink(sockaddr.sun_path);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe2a(%rip),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <main+0x36>
movl   $0x7d,-0x4(%rbp)
jmp    1304 <main+0x144>
mov    -0x20(%rbp),%rdi
call   10e0 <mktemp@plt>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    121b <main+0x5b>
movl   $0x7d,-0x4(%rbp)
jmp    1304 <main+0x144>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    125b <main+0x9b>
lea    0xe25(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cltq
cmp    $0x4,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    0xdcb(%rip),%rdi
call   1050 <chdir@plt>
mov    -0x10(%rbp),%edi
call   1030 <unlink@plt>
mov    -0x2c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12ee <main+0x12e>
lea    0xd9e(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%edi
call   1030 <unlink@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe32(%rip),%rdi
call   10c0 <strdup@plt>
test   %rax,%rax
je     11e8 <main+0x28>
mov    %rax,%rbx
mov    %rax,%rdi
call   10e0 <mktemp@plt>
cmpb   $0x0,(%rbx)
jne    11f8 <main+0x38>
mov    $0x7d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2de9(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jns    121f <main+0x5f>
lea    0xe50(%rip),%rdi
jmp    128f <main+0xcf>
mov    %eax,%r15d
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   10a0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
cmp    $0x4,%eax
sete   %dil
call   1060 <assert@plt>
mov    (%rsp),%ebp
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    0xe0c(%rip),%rdi
call   1050 <chdir@plt>
mov    %ebp,%edi
call   1030 <unlink@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
test   %eax,%eax
jns    12a2 <main+0xe2>
lea    0xdea(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    (%rsp),%edi
call   1030 <unlink@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [109 x i8] c"proot-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXXXXXX\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [5 x i8] c"bind\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i8* @strdup(i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %4
%9 = load i8*, i8** %4
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 125, i32* %1
br label %64
12: ; preds = %0
%13 = load i8*, i8** %4
%14 = call i32 @mktemp(i8* %13)
%15 = load i8*, i8** %4
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %12
store i32 125, i32* %1
br label %64
21: ; preds = %12
%22 = load i32, i32* @AF_UNIX
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %21
%32 = call i32 @bzero(%struct.sockaddr_un* %2, i32 8)
%33 = load i32, i32* @AF_UNIX
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i8*, i8** %4
%36 = call i32 @strlen(i8* %35)
%37 = sext i32 %36 to i64
%38 = icmp eq i64 %37, 4
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i8*, i8** %4
%44 = call i32 @memcpy(i32 %42, i8* %43, i32 4)
%45 = call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%46 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @unlink(i32 %47)
%49 = load i32, i32* %7
%50 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%51 = call i32 @bind(i32 %49, %struct.sockaddr* %50, i32 8)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %31
%55 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %31
%59 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @unlink(i32 %60)
%62 = load i32, i32* @EXIT_SUCCESS
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %20, %11
%65 = load i32, i32* %1
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [109 x i8] c"proot-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXXXXXX\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [5 x i8] c"/tmp\00"
@.str.3 = [5 x i8] c"bind\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = bitcast %struct.sockaddr_un* %1 to i8*
%3 = tail call dereferenceable_or_null(109) i8* @strdup(i8* getelementptr inbounds ([109 x i8], [109 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %43, label %5
5: ; preds = %0
%6 = tail call i32 @mktemp(i8* %3)
%7 = load i8, i8* %3
%8 = icmp eq i8 %7, 0
br i1 %8, label %43, label %9
9: ; preds = %5
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %9
%19 = call i32 @bzero(%struct.sockaddr_un* %1, i32 8)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %20, i32* %21
%22 = tail call i32 @strlen(i8* %3)
%23 = icmp eq i32 %22, 4
%24 = zext i1 %23 to i32
%25 = tail call i32 @assert(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%27 = load i32, i32* %26
%28 = tail call i32 @memcpy(i32 %27, i8* %3, i32 4)
%29 = tail call i32 @chdir(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @unlink(i32 %27)
%31 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%32 = call i32 @bind(i32 %12, %struct.sockaddr* %31, i32 8)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %18
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %18
%39 = load i32, i32* %26
%40 = call i32 @unlink(i32 %39)
%41 = load i32, i32* @EXIT_SUCCESS
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %5, %0
ret i32 125
}