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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
char *strerror(int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
void talk_with_server(int sockfd) {
  int num_bytes = 0;
  char buf[1024];
  memset(buf, 0, sizeof(buf));
  strncpy(buf, "Hello, this is client!", sizeof(buf) - 1);
  num_bytes = write(sockfd, buf, strlen(buf));
  if (num_bytes != strlen(buf)) {
    fprintf(stderr, "Error: write()!\n\tErrNo: %d\n\tErrMsg:%s\n", errno,
            strerror(errno));
    exit(errno);
  }
  close(sockfd);
  sleep(5);
  memset(buf, 0, sizeof(buf));
  num_bytes = read(sockfd, buf, sizeof(buf) - 1);
  if (-1 == num_bytes) {
    fprintf(stderr, "Error: read()!\n\tErrNo: %d\n\tErrMsg:%s\n", errno,
            strerror(errno));
    exit(errno);
  }
  printf("Read: %s\n", buf);
  exit(0);
}


x86 assembly (clang, -O0):
<talk_with_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    -0x410(%rbp),%rdi
lea    0xe1a(%rip),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x424(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x424(%rbp),%edi
mov    -0x420(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x414(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x414(%rbp),%eax
cmp    %ecx,%eax
je     12a4 <talk_with_server+0xf4>
mov    0x2d8a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x42c(%rbp),%edi
mov    -0x428(%rbp),%edx
mov    %rax,%rcx
lea    0xd86(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   10d0 <sleep@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1343 <talk_with_server+0x193>
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x434(%rbp)
mov    0x2cd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x430(%rbp)
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x434(%rbp),%edi
mov    -0x430(%rbp),%edx
mov    %rax,%rcx
lea    0xd0e(%rip),%rsi
call   10a0 <fprintf@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x410(%rbp),%rsi
lea    0xd13(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<talk_with_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%r14d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
lea    0xe29(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebp
jne    1263 <talk_with_server+0xb3>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0x5,%edi
call   10d0 <sleep@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
cmp    $0xffffffff,%eax
jne    1298 <talk_with_server+0xe8>
mov    0x2d99(%rip),%rax
mov    (%rax),%ebx
mov    0x2d88(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xddd(%rip),%rsi
jmp    1284 <talk_with_server+0xd4>
mov    0x2d76(%rip),%rax
mov    (%rax),%ebx
mov    0x2d65(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   10c0 <strerror@plt>
lea    0xd93(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   10a0 <fprintf@plt>
mov    (%r14),%edi
call   10b0 <exit@plt>
lea    0xdc5(%rip),%rdi
mov    %rsp,%rsi
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"Hello, this is client!\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error: write()!\0A\09ErrNo: %d\0A\09ErrMsg:%s\0A\00"
@errno = global i32 0
@.str.2 = [38 x i8] c"Error: read()!\0A\09ErrNo: %d\0A\09ErrMsg:%s\0A\00"
@.str.3 = [10 x i8] c"Read: %s\0A\00"
define void @talk_with_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 %0, i32* %2
store i32 0, i32* %3
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i32 @memset(i8* %5, i32 0, i32 1024)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 @strncpy(i8* %7, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 1023)
%9 = load i32, i32* %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i32 @strlen(i8* %11)
%13 = call i32 @write(i32 %9, i8* %10, i32 %12)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%16 = call i32 @strlen(i8* %15)
%17 = icmp ne i32 %14, %16
br i1 %17, label %18, label %26
18: ; preds = %1
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %22)
%24 = load i32, i32* @errno
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %1
%27 = load i32, i32* %2
%28 = call i32 @close(i32 %27)
%29 = call i32 @sleep(i32 5)
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 1024)
%32 = load i32, i32* %2
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 1023)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp eq i32 -1, %35
br i1 %36, label %37, label %45
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = load i32, i32* @errno
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %39, i8* %41)
%43 = load i32, i32* @errno
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %26
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%47 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [23 x i8] c"Hello, this is client!\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error: write()!\0A\09ErrNo: %d\0A\09ErrMsg:%s\0A\00"
@errno = global i32 0
@.str.2 = [38 x i8] c"Error: read()!\0A\09ErrNo: %d\0A\09ErrMsg:%s\0A\00"
@.str.3 = [10 x i8] c"Read: %s\0A\00"
define void @talk_with_server(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 1024)
%5 = call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 1023)
%6 = call i32 @strlen(i8* %3)
%7 = call i32 @write(i32 %0, i8* %3, i32 %6)
%8 = call i32 @strlen(i8* %3)
%9 = icmp eq i32 %7, %8
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %12, i8* %13)
%15 = load i32, i32* @errno
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = call i32 @close(i32 %0)
%19 = call i32 @sleep(i32 5)
%20 = call i32 @memset(i8* %3, i32 0, i32 1024)
%21 = call i32 @read(i32 %0, i8* %3, i32 1023)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %25, i8* %26)
%28 = load i32, i32* @errno
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %17
%31 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%32 = call i32 @exit(i32 0)
unreachable
}