#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 */

/* Variables and functions */
int errno;
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int send(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int);
char *strerror(int);
scalar_t__ time(int /*<<< orphan*/);
int /*<<< orphan*/ unix_socket_close(int);
int unix_socket_conn(char *);
int main(void) {
  srand((int)time(0));
  int connf;
  connf = unix_socket_conn("foo.sock");
  if (connf < 0) {
    printf("Error[%d] when connecting...", errno);
    return 0;
  }
  printf("Begin to recv/send...\n");
  int i, n, size;
  char rvbuf[4096];
  for (i = 0; i < 10; i++) {
    memset(rvbuf, 'a', 2048);
    rvbuf[2047] = 'b';
    size = send(connf, rvbuf, 2048, 0);
    if (size >= 0) {
      printf("Data[%d] Sended:%c.\n", size, rvbuf[0]);
    }
    if (size == -1) {
      printf("Error[%d] when Sending Data:%s.\n", errno, strerror(errno));
      break;
    }
    sleep(1);
  }
  unix_socket_close(connf);
  printf("Client exited.\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1080 <srand@plt>
lea    0xe49(%rip),%rdi
call   1070 <unix_socket_conn@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ec <main+0x5c>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d4 <main+0x144>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0xa,-0xc(%rbp)
jge    12be <main+0x12e>
lea    -0x1020(%rbp),%rdi
mov    $0x61,%esi
mov    $0x800,%edx
call   1060 <memset@plt>
movb   $0x62,-0x821(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1263 <main+0xd3>
mov    -0x14(%rbp),%esi
movsbl -0x1020(%rbp),%edx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    12a6 <main+0x116>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1024(%rbp),%esi
mov    %rax,%rdx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12be <main+0x12e>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1201 <main+0x71>
mov    -0x8(%rbp),%edi
call   1030 <unix_socket_close@plt>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
lea    0xe3e(%rip),%rdi
call   1080 <unix_socket_conn@plt>
test   %eax,%eax
js     127d <main+0xdd>
mov    %eax,%r15d
lea    0xe83(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%ebp
mov    %rsp,%rbx
lea    0xe39(%rip),%r14
jmp    120f <main+0x6f>
nop
movsbl (%rsp),%edx
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
add    $0xffffffff,%ebp
je     1267 <main+0xc7>
mov    %rbx,%rdi
mov    $0x61,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
movb   $0x62,0x7ff(%rsp)
62
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %eax,%eax
jns    11f0 <main+0x50>
cmp    $0xffffffff,%eax
jne    1200 <main+0x60>
mov    0x2d95(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10b0 <strerror@plt>
lea    0xde0(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%edi
call   1030 <unix_socket_close@plt>
lea    0xdfc(%rip),%rdi
call   1040 <puts@plt>
jmp    1294 <main+0xf4>
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
lea    0xd7c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x1008,%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 connecting...\00"
@errno = global i32 0
@.str.2 = [23 x i8] c"Begin to recv/send...\0A\00"
@.str.3 = [21 x i8] c"Data[%d] Sended:%c.\0A\00"
@.str.4 = [33 x i8] c"Error[%d] when Sending Data:%s.\0A\00"
@.str.5 = [16 x i8] c"Client exited.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i8]
store i32 0, i32* %1
%7 = call i64 @time(i32 0)
%8 = trunc i64 %7 to i32
%9 = call i32 @srand(i32 %8)
%10 = call i32 @unix_socket_conn(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i32, i32* @errno
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 0, i32* %1
br label %53
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %18
18: ; preds = %46, %16
%19 = load i32, i32* %3
%20 = icmp slt i32 %19, 10
br i1 %20, label %21, label %49
21: ; preds = %18
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i8 signext 97, i32 2048)
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 2047
store i8 98, i8* %24
%25 = load i32, i32* %2
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%27 = call i32 @send(i32 %25, i8* %26, i32 2048, i32 0)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %21
%31 = load i32, i32* %5
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %34)
br label %36
36: ; preds = %30, %21
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* @errno
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %40, i8* %42)
br label %49
44: ; preds = %36
%45 = call i32 @sleep(i32 1)
br label %46
46: ; preds = %44
%47 = load i32, i32* %3
%48 = add nsw i32 %47, 1
store i32 %48, i32* %3
br label %18
49: ; preds = %39, %18
%50 = load i32, i32* %2
%51 = call i32 @unix_socket_close(i32 %50)
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %49, %13
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@.str = [9 x i8] c"foo.sock\00"
@.str.1 = [29 x i8] c"Error[%d] when connecting...\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"Data[%d] Sended:%c.\0A\00"
@.str.4 = [33 x i8] c"Error[%d] when Sending Data:%s.\0A\00"
@str = [22 x i8] c"Begin to recv/send...\00"
@str.6 = [15 x i8] c"Client exited.\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = tail call i64 @time(i32 0)
%3 = trunc i64 %2 to i32
%4 = tail call i32 @srand(i32 %3)
%5 = tail call i32 @unix_socket_conn(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %36
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 2047
br label %14
14: ; preds = %10, %29
%15 = phi i32 [ 0, %10 ], [ %31, %29 ]
%16 = call i32 @memset(i8* %12, i8 signext 97, i32 2048)
store i8 98, i8* %13
%17 = call i32 @send(i32 %5, i8* %12, i32 2048, i32 0)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i8, i8* %12
%21 = sext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %17, i32 %21)
br label %29
23: ; preds = %14
%24 = icmp eq i32 %17, -1
br i1 %24, label %25, label %29
25: ; preds = %23
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %26, i8* %27)
br label %33
29: ; preds = %19, %23
%30 = call i32 @sleep(i32 1)
%31 = add nuw nsw i32 %15, 1
%32 = icmp eq i32 %31, 10
br i1 %32, label %33, label %14
33: ; preds = %29, %25
%34 = call i32 @unix_socket_close(i32 %5)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
br label %36
36: ; preds = %33, %7
ret i32 0
}