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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int /*<<< orphan*/ *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlink(char *);
int test_af_unix(int flag) {
  int fd;
  struct sockaddr_un addr;
  fd = socket(PF_UNIX, SOCK_STREAM, 0);
  if (fd < 0)
    return -1;
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  if (flag) {
    addr.sun_path[0] = 0;
    strcpy(addr.sun_path + 1, "teapot.sock.xx");
  } else
    strcpy(addr.sun_path, "teapot.sock.xx");
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    close(fd);
    return -1;
  }
  if (listen(fd, 100)) {
    close(fd);
    return -1;
  }
  sleep(10);
  {
    struct sockaddr_un addrs;
    socklen_t addrlen;
    if (!getsockname(fd, (struct sockaddr *)&addrs, &addrlen) &&
        addrs.sun_path[0]) {
      printf("addrs.sun_path:%s\n", addrs.sun_path);
      unlink(addrs.sun_path);
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_af_unix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11dd <test_af_unix+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <test_af_unix+0x148>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1223 <test_af_unix+0x83>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
lea    0xde7(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1233 <test_af_unix+0x93>
mov    -0x20(%rbp),%rdi
lea    0xdd2(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1262 <test_af_unix+0xc2>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <test_af_unix+0x148>
mov    -0xc(%rbp),%edi
mov    $0x64,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     128d <test_af_unix+0xed>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <test_af_unix+0x148>
mov    $0xa,%edi
call   10b0 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0x0,%eax
jne    12d9 <test_af_unix+0x139>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12d9 <test_af_unix+0x139>
mov    -0x30(%rbp),%rsi
lea    0xd44(%rip),%rdi
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_af_unix>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebp
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     123c <test_af_unix+0x9c>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
test   %ebp,%ebp
je     1202 <test_af_unix+0x62>
movb   $0x0,(%rdi)
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
lea    0xdf7(%rip),%rsi
call   1040 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    1235 <test_af_unix+0x95>
mov    %ebx,%edi
mov    $0x64,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     1248 <test_af_unix+0xa8>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0xa,%edi
call   10b0 <sleep@plt>
lea    0x20(%rsp),%rsi
lea    0x1c(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <getsockname@plt>
test   %eax,%eax
jne    1287 <test_af_unix+0xe7>
mov    0x20(%rsp),%rsi
cmpb   $0x0,(%rsi)
je     1287 <test_af_unix+0xe7>
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
mov    0x20(%rsp),%rdi
call   1030 <unlink@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %r14d,%r14d
jmp    123c <test_af_unix+0x9c>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [15 x i8] c"teapot.sock.xx\00"
@.str.1 = [19 x i8] c"addrs.sun_path:%s\0A\00"
define i32 @test_af_unix(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca %struct.sockaddr_un
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* @PF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %70
14: ; preds = %1
%15 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 16)
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %14
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 0, i8* %23
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %32
28: ; preds = %14
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%30 = load i8*, i8** %29
%31 = call i32 @strcpy(i8* %30, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %28, %20
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%35 = call i64 @bind(i32 %33, %struct.sockaddr* %34, i32 16)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %70
40: ; preds = %32
%41 = load i32, i32* %4
%42 = call i64 @listen(i32 %41, i32 100)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* %4
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %2
br label %70
47: ; preds = %40
%48 = call i32 @sleep(i32 10)
%49 = load i32, i32* %4
%50 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%51 = call i32 @getsockname(i32 %49, %struct.sockaddr* %50, i32* %7)
%52 = icmp ne i32 %51, 0
br i1 %52, label %67, label %53
53: ; preds = %47
%54 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = getelementptr inbounds i8, i8* %55, i64 0
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %67
60: ; preds = %53
%61 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%62 = load i8*, i8** %61
%63 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %62)
%64 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = call i32 @unlink(i8* %65)
br label %67
67: ; preds = %60, %53, %47
%68 = load i32, i32* %4
%69 = call i32 @close(i32 %68)
store i32 0, i32* %2
br label %70
70: ; preds = %67, %44, %37, %13
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@.str = [15 x i8] c"teapot.sock.xx\00"
@.str.1 = [19 x i8] c"addrs.sun_path:%s\0A\00"
define i32 @test_af_unix(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca %struct.sockaddr_un
%4 = alloca i32
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = load i32, i32* @PF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %52, label %10
10: ; preds = %1
%11 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 16)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %12, i32* %13
%14 = icmp eq i32 %0, 0
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%16 = load i8*, i8** %15
br i1 %14, label %21, label %17
17: ; preds = %10
store i8 0, i8* %16
%18 = load i8*, i8** %15
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %23
21: ; preds = %10
%22 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i64 @bind(i32 %8, %struct.sockaddr* %24, i32 16)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 @close(i32 %8)
br label %52
29: ; preds = %23
%30 = call i64 @listen(i32 %8, i32 100)
%31 = icmp eq i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 @close(i32 %8)
br label %52
34: ; preds = %29
%35 = call i32 @sleep(i32 10)
%36 = bitcast %struct.sockaddr_un* %3 to i8*
%37 = bitcast i32* %4 to i8*
%38 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%39 = call i32 @getsockname(i32 %8, %struct.sockaddr* %38, i32* %4)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %50
41: ; preds = %34
%42 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%43 = load i8*, i8** %42
%44 = load i8, i8* %43
%45 = icmp eq i8 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %41
%47 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%48 = load i8*, i8** %42
%49 = call i32 @unlink(i8* %48)
br label %50
50: ; preds = %46, %41, %34
%51 = call i32 @close(i32 %8)
br label %52
52: ; preds = %1, %50, %32, %27
%53 = phi i32 [ -1, %27 ], [ -1, %32 ], [ 0, %50 ], [ -1, %1 ]
ret i32 %53
}