#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strlen(char *);
char *strstr(char *, char *);
int get_data(int sockfd, char *buf, int bufsize, char *msg) {
  int bytes;
  char *ptr;
  bzero(buf, bufsize);
  snprintf(buf, bufsize, "%s", msg);
  if (send(sockfd, buf, strlen(buf), 0) == -1) {
    perror("network.c/send");
    return -1;
  }
  bzero(buf, bufsize);
  if ((bytes = recv(sockfd, buf, bufsize - 1, 0)) > 0) {
    if ((ptr = strstr(buf, "\r\n"))) {
      bzero(ptr, 2);
      bytes++;
      bytes++;
    }
  } else if (bytes == 0) {
    printf("Client: Server closed the connection\n");
  } else if (bytes < 0) {
    if (errno == EINTR) {
      printf("Client: timed out\n");
    } else {
      perror("Client: recv");
    }
  }
  if (bytes <= 0) {
    bzero(buf, bufsize);
  }
  return bytes;
}


x86 assembly (clang, -O0):
<get_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <bzero@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
lea    0xe4c(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0xffffffff,%eax
jne    1201 <get_data+0x81>
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <get_data+0x177>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <bzero@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1270 <get_data+0xf0>
mov    -0x10(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   10a0 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     126b <get_data+0xeb>
mov    -0x30(%rbp),%rdi
mov    $0x2,%esi
call   1090 <bzero@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12db <get_data+0x15b>
cmpl   $0x0,-0x24(%rbp)
jne    128b <get_data+0x10b>
lea    0xd94(%rip),%rdi
call   1060 <printf@plt>
jmp    12d6 <get_data+0x156>
cmpl   $0x0,-0x24(%rbp)
jge    12d1 <get_data+0x151>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    0x2d32(%rip),%rcx
cmp    (%rcx),%rax
jne    12c0 <get_data+0x140>
lea    0xd85(%rip),%rdi
call   1060 <printf@plt>
jmp    12cc <get_data+0x14c>
lea    0xd87(%rip),%rdi
call   1080 <perror@plt>
jmp    12d1 <get_data+0x151>
jmp    12d6 <get_data+0x156>
jmp    12db <get_data+0x15b>
cmpl   $0x0,-0x24(%rbp)
jg     12f1 <get_data+0x171>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <bzero@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rsi,%rdi
mov    %edx,%esi
call   1090 <bzero@plt>
lea    0xe5c(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rbp,%rcx
call   1070 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     125f <get_data+0xdf>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1090 <bzero@plt>
lea    -0x1(%r14),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    121a <get_data+0x9a>
lea    0xe17(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <strstr@plt>
test   %rax,%rax
je     1252 <get_data+0xd2>
mov    %rax,%rdi
mov    $0x2,%esi
call   1090 <bzero@plt>
add    $0x2,%ebp
jmp    1252 <get_data+0xd2>
je     123b <get_data+0xbb>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%rax
jne    1272 <get_data+0xf2>
lea    0xde9(%rip),%rdi
jmp    1242 <get_data+0xc2>
lea    0xdf2(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1090 <bzero@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1252 <get_data+0xd2>
lea    0xd9c(%rip),%rdi
call   1080 <perror@plt>
jmp    1247 <get_data+0xc7>

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [15 x i8] c"network.c/send\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
@.str.3 = [38 x i8] c"Client: Server closed the connection\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.4 = [19 x i8] c"Client: timed out\0A\00"
@.str.5 = [13 x i8] c"Client: recv\00"
define i32 @get_data(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = call i32 @bzero(i8* %12, i32 %13)
%15 = load i8*, i8** %7
%16 = load i32, i32* %8
%17 = load i8*, i8** %9
%18 = call i32 @snprintf(i8* %15, i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %6
%20 = load i8*, i8** %7
%21 = load i8*, i8** %7
%22 = call i32 @strlen(i8* %21)
%23 = call i32 @send(i32 %19, i8* %20, i32 %22, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %4
%26 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %77
27: ; preds = %4
%28 = load i8*, i8** %7
%29 = load i32, i32* %8
%30 = call i32 @bzero(i8* %28, i32 %29)
%31 = load i32, i32* %6
%32 = load i8*, i8** %7
%33 = load i32, i32* %8
%34 = sub nsw i32 %33, 1
%35 = call i32 @recv(i32 %31, i8* %32, i32 %34, i32 0)
store i32 %35, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %27
%38 = load i8*, i8** %7
%39 = call i8* @strstr(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8* %39, i8** %11
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %48
41: ; preds = %37
%42 = load i8*, i8** %11
%43 = call i32 @bzero(i8* %42, i32 2)
%44 = load i32, i32* %10
%45 = add nsw i32 %44, 1
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = add nsw i32 %46, 1
store i32 %47, i32* %10
br label %48
48: ; preds = %41, %37
br label %68
49: ; preds = %27
%50 = load i32, i32* %10
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %67
54: ; preds = %49
%55 = load i32, i32* %10
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %54
%58 = load i64, i64* @errno
%59 = load i64, i64* @EINTR
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %65
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %65
65: ; preds = %63, %61
br label %66
66: ; preds = %65, %54
br label %67
67: ; preds = %66, %52
br label %68
68: ; preds = %67, %48
%69 = load i32, i32* %10
%70 = icmp sle i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %68
%72 = load i8*, i8** %7
%73 = load i32, i32* %8
%74 = call i32 @bzero(i8* %72, i32 %73)
br label %75
75: ; preds = %71, %68
%76 = load i32, i32* %10
store i32 %76, i32* %5
br label %77
77: ; preds = %75, %25
%78 = load i32, i32* %5
ret i32 %78
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@.str.1 = [15 x i8] c"network.c/send\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.5 = [13 x i8] c"Client: recv\00"
@str = [18 x i8] c"Client: timed out\00"
@str.6 = [37 x i8] c"Client: Server closed the connection\00"
define i32 @get_data(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = tail call i32 @bzero(i8* %1, i32 %2)
%6 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3)
%7 = tail call i32 @strlen(i8* %1)
%8 = tail call i32 @send(i32 %0, i8* %1, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %37
12: ; preds = %4
%13 = tail call i32 @bzero(i8* %1, i32 %2)
%14 = add nsw i32 %2, -1
%15 = tail call i32 @recv(i32 %0, i8* %1, i32 %14, i32 0)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %12
%18 = tail call i8* @strstr(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %37, label %20
20: ; preds = %17
%21 = tail call i32 @bzero(i8* %18, i32 2)
%22 = add nuw nsw i32 %15, 2
br label %37
23: ; preds = %12
%24 = icmp eq i32 %15, 0
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0))
br label %35
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %35
33: ; preds = %27
%34 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %35
35: ; preds = %25, %31, %33
%36 = tail call i32 @bzero(i8* %1, i32 %2)
br label %37
37: ; preds = %20, %17, %35, %10
%38 = phi i32 [ -1, %10 ], [ %15, %35 ], [ %22, %20 ], [ %15, %17 ]
ret i32 %38
}