#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ get_int(int, int *);
int /*<<< orphan*/ internal_logit(char *, int, char *, char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ xmalloc(int);
char *get_str(int sock) {
  int len;
  int res, nread = 0;
  char *str;
  if (!get_int(sock, &len))
    return NULL;
  if (len < 0 || len > 4096) {
    internal_logit("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/ecthiender/mocp-git/refs/"
                   "heads/master/protocol.c",
                   145, "UNKNOWN_FUNC", "Bad string length.");
    return NULL;
  }
  str = (char *)xmalloc(sizeof(char) * (len + 1));
  while (nread < len) {
    res = recv(sock, str + nread, len - nread, 0);
    if (res == -1) {
      internal_logit("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                     "outputs/2022-01-22/02-19-57/repos/ecthiender/mocp-git/"
                     "refs/heads/master/protocol.c",
                     153, "UNKNOWN_FUNC",
                     "recv() failed when getting string: %s", strerror(errno));
      free(str);
      return NULL;
    }
    if (res == 0) {
      internal_logit("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                     "outputs/2022-01-22/02-19-57/repos/ecthiender/mocp-git/"
                     "refs/heads/master/protocol.c",
                     159, "UNKNOWN_FUNC", "Unexpected EOF when getting string");
      free(str);
      return NULL;
    }
    nread += res;
  }
  str[len] = 0;
  return str;
}


x86 assembly (clang, -O0):
<get_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <get_int@plt>
cmp    $0x0,%eax
jne    1194 <get_str+0x34>
movq   $0x0,-0x8(%rbp)
jmp    12cf <get_str+0x16f>
cmpl   $0x0,-0x10(%rbp)
jl     11ab <get_str+0x4b>
cmpl   $0x1000,-0x10(%rbp)
jle    11d9 <get_str+0x79>
lea    0xe4e(%rip),%rdi
mov    $0x91,%esi
lea    0xecd(%rip),%rdx
lea    0xed3(%rip),%rcx
mov    $0x0,%al
call   1070 <internal_logit@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cf <get_str+0x16f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12bb <get_str+0x15b>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x10(%rbp),%edx
sub    -0x18(%rbp),%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    126c <get_str+0x10c>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %eax,%r8d
lea    0xdc4(%rip),%rdi
mov    $0x99,%esi
lea    0xe43(%rip),%rdx
lea    0xe5c(%rip),%rcx
mov    $0x0,%al
call   1070 <internal_logit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cf <get_str+0x16f>
cmpl   $0x0,-0x14(%rbp)
jne    12ad <get_str+0x14d>
lea    0xd83(%rip),%rdi
mov    $0x9f,%esi
lea    0xe02(%rip),%rdx
lea    0xe41(%rip),%rcx
mov    $0x0,%al
call   1070 <internal_logit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12cf <get_str+0x16f>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11f0 <get_str+0x90>
mov    -0x20(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_str>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0xc(%rsp),%rsi
call   1080 <get_int@plt>
test   %eax,%eax
je     125c <get_str+0xfc>
mov    0xc(%rsp),%edi
cmp    $0x1001,%edi
jb     11b1 <get_str+0x51>
lea    0xe71(%rip),%rdi
lea    0xef5(%rip),%rdx
lea    0xefb(%rip),%rcx
xor    %r14d,%r14d
mov    $0x91,%esi
xor    %eax,%eax
call   1070 <internal_logit@plt>
jmp    125f <get_str+0xff>
add    $0x1,%edi
call   1050 <xmalloc@plt>
mov    %rax,%r14
mov    0xc(%rsp),%edx
test   %edx,%edx
jle    11f5 <get_str+0x95>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%r14,%rax,1),%rsi
sub    %eax,%edx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
je     11ff <get_str+0x9f>
cmp    $0xffffffff,%eax
je     1222 <get_str+0xc2>
add    %eax,%ebx
mov    0xc(%rsp),%edx
cmp    %ebx,%edx
jg     11d0 <get_str+0x70>
movslq %edx,%rax
movb   $0x0,(%r14,%rax,1)
jmp    125f <get_str+0xff>
lea    0xdfa(%rip),%rdi
lea    0xe7e(%rip),%rdx
lea    0xebd(%rip),%rcx
mov    $0x9f,%esi
xor    %eax,%eax
call   1070 <internal_logit@plt>
jmp    1254 <get_str+0xf4>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xdc9(%rip),%rdi
lea    0xe4d(%rip),%rdx
lea    0xe66(%rip),%rcx
mov    $0x99,%esi
mov    %eax,%r8d
xor    %eax,%eax
call   1070 <internal_logit@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [139 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ecthiender/mocp-git/refs/heads/master/protocol.c\00"
@.str.1 = [13 x i8] c"UNKNOWN_FUNC\00"
@.str.2 = [19 x i8] c"Bad string length.\00"
@.str.3 = [38 x i8] c"recv() failed when getting string: %s\00"
@errno = global i32 0
@.str.4 = [35 x i8] c"Unexpected EOF when getting string\00"
define i8* @get_str(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %6
%8 = load i32, i32* %3
%9 = call i32 @get_int(i32 %8, i32* %4)
%10 = icmp ne i32 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %1
store i8* null, i8** %2
br label %67
12: ; preds = %1
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 4096
br i1 %17, label %18, label %20
18: ; preds = %15, %12
%19 = call i32 (i8*, i32, i8*, i8*, ...) @internal_logit(i8* getelementptr inbounds ([139 x i8], [139 x i8]* @.str, i64 0, i64 0), i32 145, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %67
20: ; preds = %15
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i64 @xmalloc(i32 %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %7
br label %28
28: ; preds = %57, %20
%29 = load i32, i32* %6
%30 = load i32, i32* %4
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %61
32: ; preds = %28
%33 = load i32, i32* %3
%34 = load i8*, i8** %7
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i32, i32* %4
%39 = load i32, i32* %6
%40 = sub nsw i32 %38, %39
%41 = call i32 @recv(i32 %33, i8* %37, i32 %40, i32 0)
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %50
44: ; preds = %32
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 (i8*, i32, i8*, i8*, ...) @internal_logit(i8* getelementptr inbounds ([139 x i8], [139 x i8]* @.str, i64 0, i64 0), i32 153, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %46)
%48 = load i8*, i8** %7
%49 = call i32 @free(i8* %48)
store i8* null, i8** %2
br label %67
50: ; preds = %32
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = call i32 (i8*, i32, i8*, i8*, ...) @internal_logit(i8* getelementptr inbounds ([139 x i8], [139 x i8]* @.str, i64 0, i64 0), i32 159, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%55 = load i8*, i8** %7
%56 = call i32 @free(i8* %55)
store i8* null, i8** %2
br label %67
57: ; preds = %50
%58 = load i32, i32* %5
%59 = load i32, i32* %6
%60 = add nsw i32 %59, %58
store i32 %60, i32* %6
br label %28
61: ; preds = %28
%62 = load i8*, i8** %7
%63 = load i32, i32* %4
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %7
store i8* %66, i8** %2
br label %67
67: ; preds = %61, %53, %44, %18, %11
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@.str = [139 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ecthiender/mocp-git/refs/heads/master/protocol.c\00"
@.str.1 = [13 x i8] c"UNKNOWN_FUNC\00"
@.str.2 = [19 x i8] c"Bad string length.\00"
@.str.3 = [38 x i8] c"recv() failed when getting string: %s\00"
@errno = global i32 0
@.str.4 = [35 x i8] c"Unexpected EOF when getting string\00"
define i8* @get_str(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @get_int(i32 %0, i32* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %40, label %6
6: ; preds = %1
%7 = load i32, i32* %2
%8 = icmp ugt i32 %7, 4096
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = call i32 (i8*, i32, i8*, i8*, ...) @internal_logit(i8* getelementptr inbounds ([139 x i8], [139 x i8]* @.str, i64 0, i64 0), i32 145, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %40
11: ; preds = %6
%12 = add nuw nsw i32 %7, 1
%13 = call i64 @xmalloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = load i32, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %36
17: ; preds = %11, %32
%18 = phi i32 [ %34, %32 ], [ %15, %11 ]
%19 = phi i32 [ %33, %32 ], [ 0, %11 ]
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = sub nsw i32 %18, %19
%23 = call i32 @recv(i32 %0, i8* %21, i32 %22, i32 0)
switch i32 %23, label %32 [
i32 -1, label %24
i32 0, label %29
]
24: ; preds = %17
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i8*, i32, i8*, i8*, ...) @internal_logit(i8* getelementptr inbounds ([139 x i8], [139 x i8]* @.str, i64 0, i64 0), i32 153, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 @free(i8* %14)
br label %40
29: ; preds = %17
%30 = call i32 (i8*, i32, i8*, i8*, ...) @internal_logit(i8* getelementptr inbounds ([139 x i8], [139 x i8]* @.str, i64 0, i64 0), i32 159, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @free(i8* %14)
br label %40
32: ; preds = %17
%33 = add nsw i32 %23, %19
%34 = load i32, i32* %2
%35 = icmp sgt i32 %34, %33
br i1 %35, label %17, label %36
36: ; preds = %32, %11
%37 = phi i32 [ %15, %11 ], [ %34, %32 ]
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %14, i64 %38
store i8 0, i8* %39
br label %40
40: ; preds = %1, %36, %29, %24, %9
%41 = phi i8* [ null, %9 ], [ null, %24 ], [ null, %29 ], [ %14, %36 ], [ null, %1 ]
ret i8* %41
}