#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 */
typedef int /*<<< orphan*/ uid_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
char *strerror(int);
int unix_socket_accept(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ unix_socket_close(int);
int unix_socket_listen(char *);
int main(void) {
  int listenfd, connfd;
  listenfd = unix_socket_listen("foo.sock");
  if (listenfd < 0) {
    printf("Error[%d] when listening...\n", errno);
    return 0;
  }
  printf("Finished listening...\n", errno);
  uid_t uid;
  connfd = unix_socket_accept(listenfd, &uid);
  unix_socket_close(listenfd);
  if (connfd < 0) {
    printf("Error[%d] when accepting...\n", errno);
    return 0;
  }
  printf("Begin to recv/send...\n");
  int i, n, size;
  char rvbuf[2048];
  for (i = 0; i < 10; i++) {
    size = recv(connfd, rvbuf, 804, 0);
    if (size >= 0) {
      printf("Recieved Data[%d]:%c...%c\n", size, rvbuf[0], rvbuf[size - 1]);
    }
    if (size == -1) {
      printf("Error[%d] when recieving Data:%s.\n", errno, strerror(errno));
      break;
    }
  }
  unix_socket_close(connfd);
  printf("Server exited.\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
call   1050 <unix_socket_listen@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ae <main+0x4e>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12df <main+0x17f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1060 <unix_socket_accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <unix_socket_close@plt>
cmpl   $0x0,-0xc(%rbp)
jge    1209 <main+0xa9>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12df <main+0x17f>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    12c9 <main+0x169>
mov    -0xc(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x324,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1273 <main+0x113>
mov    -0x1c(%rbp),%esi
movsbl -0x820(%rbp),%edx
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x820(%rbp,%rax,1),%ecx
ff
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12b6 <main+0x156>
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x824(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x824(%rbp),%esi
mov    %rax,%rdx
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12c9 <main+0x169>
jmp    12bb <main+0x15b>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121e <main+0xbe>
mov    -0xc(%rbp),%edi
call   1030 <unix_socket_close@plt>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x818,%rsp
lea    0xe7c(%rip),%rdi
call   1050 <unix_socket_listen@plt>
test   %eax,%eax
js     1252 <main+0xe2>
mov    %eax,%ebx
lea    0xee7(%rip),%rdi
call   1060 <puts@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1070 <unix_socket_accept@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <unix_socket_close@plt>
test   %r15d,%r15d
js     1264 <main+0xf4>
lea    0xed2(%rip),%rdi
call   1060 <puts@plt>
mov    $0xa,%ebp
lea    0x10(%rsp),%rbx
lea    0xe68(%rip),%r14
jmp    11fe <main+0x8e>
nopl   (%rax)
movsbl 0x10(%rsp),%edx
movslq %eax,%rcx
movsbl -0x1(%rcx,%rbx,1),%ecx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
add    $0xffffffff,%ebp
je     123c <main+0xcc>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x324,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jns    11e0 <main+0x70>
cmp    $0xffffffff,%eax
jne    11f9 <main+0x89>
mov    0x2dc0(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xe2e(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15d,%edi
call   1030 <unix_socket_close@plt>
lea    0xe62(%rip),%rdi
call   1060 <puts@plt>
jmp    127b <main+0x10b>
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
lea    0xda7(%rip),%rdi
jmp    1274 <main+0x104>
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
lea    0xdb2(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"foo.sock\00"
@.str.1 = [29 x i8] c"Error[%d] when listening...\0A\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Finished listening...\0A\00"
@.str.3 = [29 x i8] c"Error[%d] when accepting...\0A\00"
@.str.4 = [23 x i8] c"Begin to recv/send...\0A\00"
@.str.5 = [27 x i8] c"Recieved Data[%d]:%c...%c\0A\00"
@.str.6 = [35 x i8] c"Error[%d] when recieving Data:%s.\0A\00"
@.str.7 = [16 x i8] c"Server exited.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2048 x i8]
store i32 0, i32* %1
%9 = call i32 @unix_socket_listen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = load i32, i32* @errno
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 0, i32* %1
br label %66
15: ; preds = %0
%16 = load i32, i32* @errno
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %2
%19 = call i32 @unix_socket_accept(i32 %18, i32* %4)
store i32 %19, i32* %3
%20 = load i32, i32* %2
%21 = call i32 @unix_socket_close(i32 %20)
%22 = load i32, i32* %3
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i32, i32* @errno
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %25)
store i32 0, i32* %1
br label %66
27: ; preds = %15
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %29
29: ; preds = %59, %27
%30 = load i32, i32* %5
%31 = icmp slt i32 %30, 10
br i1 %31, label %32, label %62
32: ; preds = %29
%33 = load i32, i32* %3
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%35 = call i32 @recv(i32 %33, i8* %34, i32 804, i32 0)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %32
%39 = load i32, i32* %7
%40 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = load i32, i32* %7
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %39, i32 %42, i32 %48)
br label %50
50: ; preds = %38, %32
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i32, i32* @errno
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %54, i8* %56)
br label %62
58: ; preds = %50
br label %59
59: ; preds = %58
%60 = load i32, i32* %5
%61 = add nsw i32 %60, 1
store i32 %61, i32* %5
br label %29
62: ; preds = %53, %29
%63 = load i32, i32* %3
%64 = call i32 @unix_socket_close(i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %66
66: ; preds = %62, %24, %12
%67 = load i32, i32* %1
ret i32 %67
}

LLVM IR (-O2):
@.str = [9 x i8] c"foo.sock\00"
@.str.1 = [29 x i8] c"Error[%d] when listening...\0A\00"
@errno = global i32 0
@.str.3 = [29 x i8] c"Error[%d] when accepting...\0A\00"
@.str.5 = [27 x i8] c"Recieved Data[%d]:%c...%c\0A\00"
@.str.6 = [35 x i8] c"Error[%d] when recieving Data:%s.\0A\00"
@str = [22 x i8] c"Finished listening...\00"
@str.8 = [22 x i8] c"Begin to recv/send...\00"
@str.9 = [15 x i8] c"Server exited.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2048 x i8]
%3 = tail call i32 @unix_socket_listen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %46
8: ; preds = %0
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%10 = bitcast i32* %1 to i8*
%11 = call i32 @unix_socket_accept(i32 %3, i32* %1)
%12 = call i32 @unix_socket_close(i32 %3)
%13 = icmp slt i32 %11, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i32, i32* @errno
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %15)
br label %45
17: ; preds = %8
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.8, i64 0, i64 0))
%19 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
br label %20
20: ; preds = %17, %39
%21 = phi i32 [ 0, %17 ], [ %40, %39 ]
%22 = call i32 @recv(i32 %11, i8* %19, i32 804, i32 0)
%23 = icmp sgt i32 %22, -1
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i8, i8* %19
%26 = sext i8 %25 to i32
%27 = add nsw i32 %22, -1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i32 %22, i32 %26, i32 %31)
br label %39
33: ; preds = %20
%34 = icmp eq i32 %22, -1
br i1 %34, label %35, label %39
35: ; preds = %33
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %36, i8* %37)
br label %42
39: ; preds = %24, %33
%40 = add nuw nsw i32 %21, 1
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %20
42: ; preds = %39, %35
%43 = call i32 @unix_socket_close(i32 %11)
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
br label %45
45: ; preds = %42, %14
br label %46
46: ; preds = %45, %5
ret i32 0
}