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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *argv[]) {
  int sockfd;
  socklen_t len;
  struct sockaddr_un addr1, addr2;
  if (argc != 2) {
    printf("usage: unixbind <pathname>");
  }
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  unlink(argv[1]);
  bzero(&addr1, sizeof(addr1));
  addr1.sun_family = AF_UNIX;
  strncpy(addr1.sun_path, argv[1], sizeof(addr1.sun_path) - 1);
  bind(sockfd, (struct sockaddr *)&addr1, SUN_LEN(&addr1));
  len = sizeof(addr2);
  getsockname(sockfd, (struct sockaddr *)&addr2, &len);
  printf("bound name = %s,return len = %d\n", addr2.sun_path, len);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11be <main+0x2e>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <unlink@plt>
lea    -0x28(%rbp),%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x28(%rbp),%rdi
call   10a0 <SUN_LEN@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1060 <bind@plt>
movl   $0x10,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1080 <getsockname@plt>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11b0 <main+0x20>
lea    0xe57(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e31(%rip),%r15
mov    (%r15),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0x10,%esi
call   1070 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rsp),%rdi
mov    0x8(%rbx),%rsi
mov    $0x7,%edx
call   1040 <strncpy@plt>
mov    %r14,%rdi
call   10a0 <SUN_LEN@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1060 <bind@plt>
movl   $0x10,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1080 <getsockname@plt>
mov    0x18(%rsp),%rsi
mov    0x4(%rsp),%edx
lea    0xddd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"usage: unixbind <pathname>\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [33 x i8] c"bound name = %s,return len = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_un
%9 = alloca %struct.sockaddr_un
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* @AF_UNIX
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @unlink(i8* %20)
%22 = call i32 @bzero(%struct.sockaddr_un* %8, i32 16)
%23 = load i32, i32* @AF_UNIX
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @strncpy(i8* %26, i8* %29, i32 7)
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%33 = call i32 @SUN_LEN(%struct.sockaddr_un* %8)
%34 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 %33)
store i32 16, i32* %7
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%37 = call i32 @getsockname(i32 %35, %struct.sockaddr* %36, i32* %7)
%38 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = load i32, i32* %7
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %39, i32 %40)
%42 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"usage: unixbind <pathname>\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [33 x i8] c"bound name = %s,return len = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca %struct.sockaddr_un
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_un* %4 to i8*
%8 = bitcast %struct.sockaddr_un* %5 to i8*
%9 = icmp eq i32 %0, 2
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @unlink(i8* %17)
%19 = call i32 @bzero(%struct.sockaddr_un* %4, i32 16)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = load i8*, i8** %16
%25 = tail call i32 @strncpy(i8* %23, i8* %24, i32 7)
%26 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%27 = call i32 @SUN_LEN(%struct.sockaddr_un* %4)
%28 = call i32 @bind(i32 %15, %struct.sockaddr* %26, i32 %27)
store i32 16, i32* %3
%29 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%30 = call i32 @getsockname(i32 %15, %struct.sockaddr* %29, i32* %3)
%31 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 0
%32 = load i8*, i8** %31
%33 = load i32, i32* %3
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 %33)
%35 = call i32 @exit(i32 0)
unreachable
}