#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_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SUN_LEN(struct sockaddr_un *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
    exit(-1);
  }
  sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
  unlink(argv[1]);
  memset(&addr1, 0, sizeof(addr1));
  addr1.sun_family = AF_LOCAL;
  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, returned len = %d\n", addr2.sun_path, len);
  return 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     11e6 <main+0x46>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <unlink@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2dc2(%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   10b0 <SUN_LEN@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1080 <bind@plt>
movl   $0x10,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1090 <getsockname@plt>
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    0xd8b(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1258 <main+0xb8>
mov    0x2e2b(%rip),%r15
mov    (%r15),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@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   10b0 <SUN_LEN@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1080 <bind@plt>
movl   $0x10,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   1090 <getsockname@plt>
mov    0x18(%rsp),%rsi
mov    0x4(%rsp),%edx
lea    0xdd0(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd95(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <pathname>\0A\00"
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [36 x i8] c"bound name = %s, returned 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 %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i32, i32* @AF_LOCAL
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @unlink(i8* %25)
%27 = call i32 @memset(%struct.sockaddr_un* %8, i32 0, i32 16)
%28 = load i32, i32* @AF_LOCAL
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %8, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @strncpy(i8* %31, i8* %34, i32 7)
%36 = load i32, i32* %6
%37 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%38 = call i32 @SUN_LEN(%struct.sockaddr_un* %8)
%39 = call i32 @bind(i32 %36, %struct.sockaddr* %37, i32 %38)
store i32 16, i32* %7
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%42 = call i32 @getsockname(i32 %40, %struct.sockaddr* %41, i32* %7)
%43 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%44 = load i8*, i8** %43
%45 = load i32, i32* %7
%46 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %44, i32 %45)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <pathname>\0A\00"
@AF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [36 x i8] c"bound name = %s, returned 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 %15, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_LOCAL
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @unlink(i8* %20)
%22 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 16)
%23 = load i32, i32* @AF_LOCAL
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = load i8*, i8** %19
%28 = call i32 @strncpy(i8* %26, i8* %27, i32 7)
%29 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%30 = call i32 @SUN_LEN(%struct.sockaddr_un* %4)
%31 = call i32 @bind(i32 %18, %struct.sockaddr* %29, i32 %30)
store i32 16, i32* %3
%32 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%33 = call i32 @getsockname(i32 %18, %struct.sockaddr* %32, i32* %3)
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i64 0, i32 0
%35 = load i8*, i8** %34
%36 = load i32, i32* %3
%37 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %35, i32 %36)
ret i32 0
}