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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, size_t);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
size_t write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  int cfd;
  int ret;
  struct sockaddr_un sa;
  struct sockaddr_un cli_a;
  socklen_t cli_addr_size;
  size_t nwrite;
  fd = socket(AF_UNIX, SOCK_STREAM, 0);
  sa.sun_family = AF_UNIX;
  strncpy(sa.sun_path, "/tmp/socket", sizeof("/tmp/socket"));
  ret = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
  if (ret == -1) {
    perror("bind");
    exit(1);
  }
  if (listen(fd, 50) == -1) {
    perror("listen");
    exit(1);
  }
  cli_addr_size = sizeof(cli_a);
  cfd = accept(fd, (struct sockaddr *)&cli_a, &cli_addr_size);
  if (cfd == -1) {
    perror("accept");
    exit(1);
  }
  nwrite = write(cfd, "Hi", sizeof("Hi"));
  printf("written nwrite=%d\n", nwrite);
  while (nwrite < sizeof("Hi")) {
    nwrite += write(cfd, "Hi", sizeof("Hi"));
  }
  printf("written nwrite=%d\n", nwrite);
  sleep(10);
  close(cfd);
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%edi
lea    0xe08(%rip),%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1236 <main+0x86>
lea    0xde5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x32,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1262 <main+0xb2>
lea    0xdbe(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x8,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    129c <main+0xec>
lea    0xd8b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
lea    0xd79(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rsi
lea    0xd63(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x3,-0x40(%rbp)
jae    12f0 <main+0x140>
mov    -0x18(%rbp),%edi
lea    0xd46(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    12c4 <main+0x114>
mov    -0x40(%rbp),%rsi
lea    0xd27(%rip),%rdi
call   1050 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e27(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe1d(%rip),%rsi
mov    $0xc,%edx
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     12cd <main+0x11d>
mov    %r14d,%edi
mov    $0x32,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     12d6 <main+0x126>
movl   $0x8,0x4(%rsp)
lea    0x10(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10a0 <accept@plt>
cmp    $0xffffffff,%eax
je     12df <main+0x12f>
mov    %eax,%ebp
lea    0xdd5(%rip),%rsi
mov    %eax,%edi
mov    $0x3,%edx
call   1040 <write@plt>
mov    %rax,%rbx
lea    0xdc2(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
cmp    $0x2,%rbx
ja     1298 <main+0xe8>
lea    0xdaa(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x3,%edx
call   1040 <write@plt>
add    %rax,%rbx
cmp    $0x3,%rbx
jb     1280 <main+0xd0>
lea    0xd83(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd38(%rip),%rdi
jmp    12e6 <main+0x136>
lea    0xd34(%rip),%rdi
jmp    12e6 <main+0x136>
lea    0xd32(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"/tmp/socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [3 x i8] c"Hi\00"
@.str.5 = [19 x i8] c"written nwrite=%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 i32
%9 = alloca %struct.sockaddr_un
%10 = alloca %struct.sockaddr_un
%11 = alloca i32
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strncpy(i32 %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 12)
%21 = load i32, i32* %6
%22 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 8)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %2
%30 = load i32, i32* %6
%31 = call i32 @listen(i32 %30, i32 50)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
store i32 8, i32* %11
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_un* %10 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %11)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %36
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %36
%46 = load i32, i32* %7
%47 = call i64 @write(i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
store i64 %47, i64* %12
%48 = load i64, i64* %12
%49 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i64 %48)
br label %50
50: ; preds = %53, %45
%51 = load i64, i64* %12
%52 = icmp ult i64 %51, 3
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32, i32* %7
%55 = call i64 @write(i32 %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
%56 = load i64, i64* %12
%57 = add i64 %56, %55
store i64 %57, i64* %12
br label %50
58: ; preds = %50
%59 = load i64, i64* %12
%60 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i64 %59)
%61 = call i32 @sleep(i32 10)
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"/tmp/socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [7 x i8] c"accept\00"
@.str.4 = [3 x i8] c"Hi\00"
@.str.5 = [19 x i8] c"written nwrite=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = bitcast %struct.sockaddr_un* %3 to i8*
%7 = bitcast %struct.sockaddr_un* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = load i32, i32* @AF_UNIX
%13 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 @strncpy(i32 undef, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 12)
%15 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%16 = call i32 @bind(i32 %11, %struct.sockaddr* %15, i32 8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = call i32 @listen(i32 %11, i32 50)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %21
store i32 8, i32* %5
%28 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%29 = call i32 @accept(i32 %11, %struct.sockaddr* %28, i32* %5)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = call i64 @write(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
%36 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i64 %35)
%37 = icmp ult i64 %35, 3
br i1 %37, label %38, label %43
38: ; preds = %34, %38
%39 = phi i64 [ %41, %38 ], [ %35, %34 ]
%40 = call i64 @write(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 3)
%41 = add i64 %40, %39
%42 = icmp ult i64 %41, 3
br i1 %42, label %38, label %43
43: ; preds = %38, %34
%44 = phi i64 [ %35, %34 ], [ %41, %38 ]
%45 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i64 %44)
%46 = call i32 @sleep(i32 10)
%47 = call i32 @close(i32 %29)
%48 = call i32 @close(i32 %11)
ret i32 0
}