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

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int unified_sockets__connect(int);
int unified_sockets__open();
int unified_sockets__recv(int, char *, int);
int unified_sockets__send(int, char *, int);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  int sd, ret;
  sd = unified_sockets__open();
  if (sd) {
    ret = unified_sockets__connect(sd);
    if (ret >= 0) {
      unsigned cnt;
      int len;
      char rbuf[1024];
      char sbuf[1024];
      printf("Client sending 'hello server!' to server\n");
      memset(rbuf, 0x00, sizeof(rbuf));
      memset(sbuf, 0x00, sizeof(sbuf));
      strncpy(sbuf, "test!\0", 20);
      len = strlen(sbuf);
      snprintf(sbuf + len, sizeof(sbuf) - len, " instance %s", argv[1]);
      printf("%s\n", sbuf);
      for (cnt = 0; cnt < 100; cnt++) {
        printf("write returns %i\n",
               unified_sockets__send(sd, sbuf, strlen(sbuf)));
        memset(rbuf, 0x00, sizeof(rbuf));
        usleep(10000);
        int count = unified_sockets__recv(sd, rbuf, 1000);
        printf("I'm %s, Got echo of %s from server\n", argv[1], rbuf);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <unified_sockets__open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1346 <main+0x1a6>
mov    -0x14(%rbp),%edi
call   1040 <unified_sockets__connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1341 <main+0x1a1>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x820(%rbp),%rdi
lea    0xe4d(%rip),%rsi
mov    $0x14,%edx
call   1030 <strncpy@plt>
lea    -0x820(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
lea    -0x820(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
movslq -0x20(%rbp),%rcx
mov    $0x400,%eax
sub    %rcx,%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xdc2(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x820(%rbp),%rsi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x64,-0x1c(%rbp)
jae    133c <main+0x19c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x834(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x820(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x834(%rbp),%edi
mov    -0x830(%rbp),%rsi
mov    %eax,%edx
call   10a0 <unified_sockets__send@plt>
mov    %eax,%esi
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    $0x2710,%edi
call   10c0 <usleep@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3e8,%edx
call   1090 <unified_sockets__recv@plt>
mov    %eax,-0x824(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x420(%rbp),%rdx
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1289 <main+0xe9>
jmp    1341 <main+0x1a1>
jmp    1346 <main+0x1a6>
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rsi,%r14
xor    %eax,%eax
call   10c0 <unified_sockets__open@plt>
test   %eax,%eax
je     12d3 <main+0x123>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <unified_sockets__connect@plt>
test   %eax,%eax
js     12d3 <main+0x123>
lea    0xe5f(%rip),%rdi
call   1050 <puts@plt>
lea    0x400(%rsp),%rbx
mov    $0x400,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
lea    0xdda(%rip),%rsi
mov    %r15,%rdi
mov    $0x14,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
sub    %eax,%r12d
mov    0x8(%r14),%rcx
lea    0xdb8(%rip),%rdx
mov    %r12d,%esi
call   1080 <snprintf@plt>
mov    %r15,%rdi
call   1050 <puts@plt>
mov    $0x64,%r12d
lea    0xdba(%rip),%r13
nopl   0x0(%rax)
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   10b0 <unified_sockets__send@plt>
lea    0xd89(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    $0x2710,%edi
call   10d0 <usleep@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3e8,%edx
call   10a0 <unified_sockets__recv@plt>
mov    0x8(%r14),%rsi
mov    %r13,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
add    $0xffffffff,%r12d
jne    1270 <main+0xc0>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Client sending 'hello server!' to server\0A\00"
@.str.1 = [7 x i8] c"test!\00\00"
@.str.2 = [13 x i8] c" instance %s\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [18 x i8] c"write returns %i\0A\00"
@.str.5 = [36 x i8] c"I'm %s, Got echo of %s from server\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 i32
%10 = alloca [1024 x i8]
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = call i32 (...) @unified_sockets__open()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %71
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @unified_sockets__connect(i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %70
21: ; preds = %16
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%24 = call i32 @memset(i8* %23, i32 0, i32 1024)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%26 = call i32 @memset(i8* %25, i32 0, i32 1024)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%28 = call i32 @strncpy(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 20)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
store i32 %30, i32* %9
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = sub i64 1024, %36
%38 = trunc i64 %37 to i32
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = call i32 @snprintf(i8* %34, i32 %38, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %41)
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %43)
store i32 0, i32* %8
br label %45
45: ; preds = %66, %21
%46 = load i32, i32* %8
%47 = icmp ult i32 %46, 100
br i1 %47, label %48, label %69
48: ; preds = %45
%49 = load i32, i32* %6
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = call i32 @unified_sockets__send(i32 %49, i8* %50, i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %53)
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%56 = call i32 @memset(i8* %55, i32 0, i32 1024)
%57 = call i32 @usleep(i32 10000)
%58 = load i32, i32* %6
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%60 = call i32 @unified_sockets__recv(i32 %58, i8* %59, i32 1000)
store i32 %60, i32* %12
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %63, i8* %64)
br label %66
66: ; preds = %48
%67 = load i32, i32* %8
%68 = add i32 %67, 1
store i32 %68, i32* %8
br label %45
69: ; preds = %45
br label %70
70: ; preds = %69, %16
br label %71
71: ; preds = %70, %2
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"test!\00\00"
@.str.2 = [13 x i8] c" instance %s\00"
@.str.4 = [18 x i8] c"write returns %i\0A\00"
@.str.5 = [36 x i8] c"I'm %s, Got echo of %s from server\0A\00"
@str = [41 x i8] c"Client sending 'hello server!' to server\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = tail call i32 (...) @unified_sockets__open()
%6 = icmp eq i32 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %2
%8 = tail call i32 @unified_sockets__connect(i32 %5)
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %38
10: ; preds = %7
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%14 = call i32 @memset(i8* %11, i32 0, i32 1024)
%15 = call i32 @memset(i8* %12, i32 0, i32 1024)
%16 = call i32 @strncpy(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 20)
%17 = call i32 @strlen(i8* %12)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %18
%20 = sub i32 1024, %17
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @snprintf(i8* %19, i32 %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = call i32 @puts(i8* %12)
br label %25
25: ; preds = %10, %25
%26 = phi i32 [ 0, %10 ], [ %35, %25 ]
%27 = call i32 @strlen(i8* %12)
%28 = call i32 @unified_sockets__send(i32 %5, i8* %12, i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = call i32 @memset(i8* %11, i32 0, i32 1024)
%31 = call i32 @usleep(i32 10000)
%32 = call i32 @unified_sockets__recv(i32 %5, i8* %11, i32 1000)
%33 = load i8*, i8** %21
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %33, i8* %11)
%35 = add nuw nsw i32 %26, 1
%36 = icmp eq i32 %35, 100
br i1 %36, label %37, label %25
37: ; preds = %25
br label %38
38: ; preds = %7, %37, %2
ret i32 0
}