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

/* Variables and functions */
int atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ display_error(char *);
int /*<<< orphan*/ display_perror(char *);
scalar_t__ malloc(int);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, unsigned int, int /*<<< orphan*/);
int snprintf(char *, int, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
unsigned int strlen(char *);
char *strstr(char *, char *);
char *working(int sockfd) {
  char buffer[256], *match, *data;
  int length, alloc, missing;
  strcpy(buffer, "GET /json HTTP/1.0\r\n\r\n");
  if (send(sockfd, buffer, strlen(buffer), 0) < 0) {
    display_perror("send");
    return NULL;
  }
  if ((length = recv(sockfd, buffer, sizeof(buffer), 0)) < 0) {
    display_perror("read");
    return NULL;
  }
  buffer[length] = '\0';
  if (!(match = strstr(buffer, "Content-length: "))) {
    display_error("Cannot find Content-length\n");
    return NULL;
  }
  alloc = atol(match + 16) + 1;
  if (!(match = strstr(buffer, "\r\n\r\n"))) {
    display_error("Cannot find start of data\n");
    return NULL;
  }
  if (!(data = (char *)malloc(sizeof(char) * alloc))) {
    display_perror("malloc");
    return NULL;
  }
  length = snprintf(data, alloc, "%s", match + 4);
  missing = alloc - length;
  while (strlen(data) != (unsigned int)alloc - 1) {
    if ((length = recv(sockfd, data + alloc - missing, missing, 0)) < 0)
      display_perror("read");
    data[alloc - missing + length] = '\0';
    missing -= length;
  }
  close(sockfd);
  return data;
}


x86 assembly (clang, -O0):
<working>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0xc(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0x0,%rax
jge    122d <working+0x7d>
lea    0xdfc(%rip),%rdi
call   10a0 <display_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1439 <working+0x289>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x124(%rbp)
cmp    $0x0,%eax
jge    126b <working+0xbb>
lea    0xdc3(%rip),%rdi
call   10a0 <display_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1439 <working+0x289>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
lea    0xd99(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
jne    12b7 <working+0x107>
lea    0xd8d(%rip),%rdi
call   1070 <display_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1439 <working+0x289>
mov    -0x118(%rbp),%rdi
add    $0x10,%rdi
call   10c0 <atol@plt>
add    $0x1,%eax
mov    %eax,-0x128(%rbp)
lea    -0x110(%rbp),%rdi
lea    0xd34(%rip),%rsi
call   10d0 <strstr@plt>
mov    %rax,-0x118(%rbp)
cmp    $0x0,%rax
jne    130d <working+0x15d>
lea    0xd53(%rip),%rdi
call   1070 <display_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    1439 <working+0x289>
movslq -0x128(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
jne    1349 <working+0x199>
lea    0xd32(%rip),%rdi
call   10a0 <display_perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1439 <working+0x289>
mov    -0x120(%rbp),%rdi
mov    -0x128(%rbp),%esi
mov    -0x118(%rbp),%rcx
add    $0x4,%rcx
lea    0xd08(%rip),%rdx
call   1080 <snprintf@plt>
mov    %eax,-0x124(%rbp)
mov    -0x128(%rbp),%eax
sub    -0x124(%rbp),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x128(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     1426 <working+0x276>
mov    -0xc(%rbp),%edi
mov    -0x120(%rbp),%rsi
movslq -0x128(%rbp),%rax
add    %rax,%rsi
movslq -0x12c(%rbp),%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
mov    -0x12c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x124(%rbp)
cmp    $0x0,%eax
jge    13ed <working+0x23d>
lea    0xc34(%rip),%rdi
call   10a0 <display_perror@plt>
mov    -0x120(%rbp),%rax
mov    -0x128(%rbp),%ecx
sub    -0x12c(%rbp),%ecx
add    -0x124(%rbp),%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x124(%rbp),%ecx
mov    -0x12c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x12c(%rbp)
jmp    1385 <working+0x1d5>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
mov    -0x120(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<working>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r13d
lea    0xe35(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %rax,%rax
js     12a8 <working+0xf8>
lea    0x10(%rsp),%rsi
mov    %r13d,%edi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     12b1 <working+0x101>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdff(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10d0 <strstr@plt>
test   %rax,%rax
je     12ba <working+0x10a>
add    $0x10,%rax
mov    %rax,%rdi
call   10c0 <atol@plt>
mov    %eax,%r14d
lea    0xdc7(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10d0 <strstr@plt>
test   %rax,%rax
je     12c3 <working+0x113>
mov    %rax,%rbp
lea    0x1(%r14),%r15d
mov    %r15d,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     12d1 <working+0x121>
mov    %rax,%rbx
add    $0x4,%rbp
lea    0xdf4(%rip),%rdx
mov    %rax,%rdi
mov    %r15d,0xc(%rsp)
mov    %r15d,%esi
mov    %rbp,%rcx
call   1080 <snprintf@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %r14d,%eax
jne    12f4 <working+0x144>
mov    %r13d,%edi
call   1090 <close@plt>
jmp    12df <working+0x12f>
lea    0xd68(%rip),%rdi
jmp    12d8 <working+0x128>
lea    0xd64(%rip),%rdi
jmp    12d8 <working+0x128>
lea    0xd71(%rip),%rdi
jmp    12ca <working+0x11a>
lea    0xd84(%rip),%rdi
call   1070 <display_error@plt>
jmp    12dd <working+0x12d>
lea    0xd91(%rip),%rdi
call   10a0 <display_perror@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%r12
mov    0xc(%rsp),%r15d
movslq %r15d,%rax
sub    %ebp,%r15d
mov    %rbx,%r14
add    %rax,%r14
jmp    1333 <working+0x183>
nopw   0x0(%rax,%rax,1)
mov    0xc(%rsp),%eax
sub    %r15d,%eax
add    %ebp,%eax
cltq
movb   $0x0,(%rbx,%rax,1)
sub    %ebp,%r15d
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    %r12d,%eax
je     129e <working+0xee>
movslq %r15d,%rdx
mov    %r14,%rsi
sub    %rdx,%rsi
mov    %r13d,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1310 <working+0x160>
lea    0xcc9(%rip),%rdi
call   10a0 <display_perror@plt>
jmp    1310 <working+0x160>

LLVM IR (-O0):
@.str = [23 x i8] c"GET /json HTTP/1.0\0D\0A\0D\0A\00"
@.str.1 = [5 x i8] c"send\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [17 x i8] c"Content-length: \00"
@.str.4 = [28 x i8] c"Cannot find Content-length\0A\00"
@.str.5 = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.6 = [27 x i8] c"Cannot find start of data\0A\00"
@.str.7 = [7 x i8] c"malloc\00"
@.str.8 = [3 x i8] c"%s\00"
define i8* @working(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca [256 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%11 = call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* %3
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = call i64 @send(i32 %12, i8* %13, i32 %15, i32 0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %1
%19 = call i32 @display_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %102
20: ; preds = %1
%21 = load i32, i32* %3
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%23 = call i32 @recv(i32 %21, i8* %22, i32 256, i32 0)
store i32 %23, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @display_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %102
27: ; preds = %20
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%32 = call i8* @strstr(i8* %31, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i8* %32, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %36, label %34
34: ; preds = %27
%35 = call i32 @display_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %2
br label %102
36: ; preds = %27
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i64 16
%39 = call i32 @atol(i8* %38)
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
%41 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%42 = call i8* @strstr(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
store i8* %42, i8** %5
%43 = icmp ne i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %36
%45 = call i32 @display_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
store i8* null, i8** %2
br label %102
46: ; preds = %36
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = mul i64 1, %48
%50 = trunc i64 %49 to i32
%51 = call i64 @malloc(i32 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %6
%53 = icmp ne i8* %52, null
br i1 %53, label %56, label %54
54: ; preds = %46
%55 = call i32 @display_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
store i8* null, i8** %2
br label %102
56: ; preds = %46
%57 = load i8*, i8** %6
%58 = load i32, i32* %8
%59 = load i8*, i8** %5
%60 = getelementptr inbounds i8, i8* %59, i64 4
%61 = call i32 @snprintf(i8* %57, i32 %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %60)
store i32 %61, i32* %7
%62 = load i32, i32* %8
%63 = load i32, i32* %7
%64 = sub nsw i32 %62, %63
store i32 %64, i32* %9
br label %65
65: ; preds = %86, %56
%66 = load i8*, i8** %6
%67 = call i32 @strlen(i8* %66)
%68 = load i32, i32* %8
%69 = sub i32 %68, 1
%70 = icmp ne i32 %67, %69
br i1 %70, label %71, label %98
71: ; preds = %65
%72 = load i32, i32* %3
%73 = load i8*, i8** %6
%74 = load i32, i32* %8
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = sub i64 0, %78
%80 = getelementptr inbounds i8, i8* %76, i64 %79
%81 = load i32, i32* %9
%82 = call i32 @recv(i32 %72, i8* %80, i32 %81, i32 0)
store i32 %82, i32* %7
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %71
%85 = call i32 @display_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %86
86: ; preds = %84, %71
%87 = load i8*, i8** %6
%88 = load i32, i32* %8
%89 = load i32, i32* %9
%90 = sub nsw i32 %88, %89
%91 = load i32, i32* %7
%92 = add nsw i32 %90, %91
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds i8, i8* %87, i64 %93
store i8 0, i8* %94
%95 = load i32, i32* %7
%96 = load i32, i32* %9
%97 = sub nsw i32 %96, %95
store i32 %97, i32* %9
br label %65
98: ; preds = %65
%99 = load i32, i32* %3
%100 = call i32 @close(i32 %99)
%101 = load i8*, i8** %6
store i8* %101, i8** %2
br label %102
102: ; preds = %98, %54, %44, %34, %25, %18
%103 = load i8*, i8** %2
ret i8* %103
}

LLVM IR (-O2):
@.str = [23 x i8] c"GET /json HTTP/1.0\0D\0A\0D\0A\00"
@.str.1 = [5 x i8] c"send\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [17 x i8] c"Content-length: \00"
@.str.4 = [28 x i8] c"Cannot find Content-length\0A\00"
@.str.5 = [5 x i8] c"\0D\0A\0D\0A\00"
@.str.6 = [27 x i8] c"Cannot find start of data\0A\00"
@.str.7 = [7 x i8] c"malloc\00"
@.str.8 = [3 x i8] c"%s\00"
define i8* @working(i32 %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @strcpy(i8* %3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @strlen(i8* %3)
%6 = call i64 @send(i32 %0, i8* %3, i32 %5, i32 0)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @display_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %64
10: ; preds = %1
%11 = call i32 @recv(i32 %0, i8* %3, i32 256, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @display_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %64
15: ; preds = %10
%16 = zext i32 %11 to i64
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @display_error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %64
22: ; preds = %15
%23 = getelementptr inbounds i8, i8* %18, i64 16
%24 = call i32 @atol(i8* %23)
%25 = add nsw i32 %24, 1
%26 = call i8* @strstr(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @display_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
br label %64
30: ; preds = %22
%31 = sext i32 %25 to i64
%32 = call i64 @malloc(i32 %25)
%33 = inttoptr i64 %32 to i8*
%34 = icmp eq i64 %32, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @display_perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %64
37: ; preds = %30
%38 = getelementptr inbounds i8, i8* %26, i64 4
%39 = call i32 @snprintf(i8* %33, i32 %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %38)
%40 = call i32 @strlen(i8* %33)
%41 = icmp eq i32 %40, %24
br i1 %41, label %62, label %42
42: ; preds = %37
%43 = sub nsw i32 %25, %39
%44 = getelementptr inbounds i8, i8* %33, i64 %31
br label %45
45: ; preds = %42, %54
%46 = phi i32 [ %43, %42 ], [ %59, %54 ]
%47 = sext i32 %46 to i64
%48 = sub nsw i64 0, %47
%49 = getelementptr inbounds i8, i8* %44, i64 %48
%50 = call i32 @recv(i32 %0, i8* %49, i32 %46, i32 0)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 @display_perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %54
54: ; preds = %52, %45
%55 = sub i32 %25, %46
%56 = add nsw i32 %55, %50
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %33, i64 %57
store i8 0, i8* %58
%59 = sub nsw i32 %46, %50
%60 = call i32 @strlen(i8* %33)
%61 = icmp eq i32 %60, %24
br i1 %61, label %62, label %45
62: ; preds = %54, %37
%63 = call i32 @close(i32 %0)
br label %64
64: ; preds = %62, %35, %28, %20, %13, %8
%65 = phi i8* [ null, %8 ], [ null, %13 ], [ %33, %62 ], [ null, %35 ], [ null, %28 ], [ null, %20 ]
ret i8* %65
}