#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*/ uint8_t;
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ servport;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
long cg_read_key_long(char const *, char *, char *);
long cg_read_long(char const *, char *);
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, int /*<<< orphan*/ *, struct addrinfo **);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned short);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ values_close(long, long, int);
int tcp_client(const char *cgroup, unsigned short port) {
  const char server[] = "localhost";
  struct addrinfo *ai;
  char servport[6];
  int retries = 0x10;
  int sk, ret;
  snprintf(servport, sizeof(servport), "%hd", port);
  ret = getaddrinfo(server, servport, NULL, &ai);
  if (ret)
    return ret;
  sk = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  if (sk < 0)
    goto free_ainfo;
  ret = connect(sk, ai->ai_addr, ai->ai_addrlen);
  if (ret < 0)
    goto close_sk;
  ret = 1;
  while (retries--) {
    uint8_t buf[0x100000];
    long current, sock;
    if (read(sk, buf, sizeof(buf)) <= 0)
      goto close_sk;
    current = cg_read_long(cgroup, "memory.current");
    sock = cg_read_key_long(cgroup, "memory.stat", "sock ");
    if (current < 0 || sock < 0)
      goto close_sk;
    if (current < sock)
      goto close_sk;
    if (values_close(current, sock, 10)) {
      ret = 0;
      break;
    }
  }
close_sk:
  close(sk);
free_ainfo:
  freeaddrinfo(ai);
  return ret;
}


x86 assembly (clang, -O0):
<tcp_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x400050,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0xe43(%rip),%rax
mov    %rax,-0x1c(%rbp)
mov    0xe40(%rip),%ax
mov    %ax,-0x14(%rbp)
movl   $0x10,-0x34(%rbp)
lea    -0x2e(%rbp),%rdi
mov    $0x6,%esi
lea    0xe27(%rip),%rdx
movzwl -0x12(%rbp),%ecx
call   1030 <snprintf@plt>
lea    -0x1c(%rbp),%rdi
lea    -0x2e(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x28(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1219 <tcp_client+0x79>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1368 <tcp_client+0x1c8>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1245 <tcp_client+0xa5>
jmp    1359 <tcp_client+0x1b9>
mov    -0x38(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1090 <connect@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    126c <tcp_client+0xcc>
jmp    1351 <tcp_client+0x1b1>
movl   $0x1,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x34(%rbp)
cmp    $0x0,%eax
je     134c <tcp_client+0x1ac>
mov    -0x38(%rbp),%edi
lea    -0x400040(%rbp),%rsi
mov    $0x400000,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     12aa <tcp_client+0x10a>
jmp    1351 <tcp_client+0x1b1>
mov    -0x10(%rbp),%rdi
lea    0xd59(%rip),%rsi
call   1040 <cg_read_long@plt>
mov    %rax,-0x400048(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xd51(%rip),%rsi
lea    0xd56(%rip),%rdx
call   1070 <cg_read_key_long@plt>
mov    %rax,-0x400050(%rbp)
cmpq   $0x0,-0x400048(%rbp)
jl     12fb <tcp_client+0x15b>
cmpq   $0x0,-0x400050(%rbp)
jge    1300 <tcp_client+0x160>
jmp    1351 <tcp_client+0x1b1>
mov    -0x400048(%rbp),%rax
cmp    -0x400050(%rbp),%rax
jge    1319 <tcp_client+0x179>
jmp    1351 <tcp_client+0x1b1>
mov    -0x400048(%rbp),%rdi
mov    -0x400050(%rbp),%rsi
mov    $0xa,%edx
call   1080 <values_close@plt>
cmp    $0x0,%rax
je     1347 <tcp_client+0x1a7>
movl   $0x0,-0x3c(%rbp)
jmp    134c <tcp_client+0x1ac>
jmp    1273 <tcp_client+0xd3>
jmp    1351 <tcp_client+0x1b1>
mov    -0x38(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x400050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400028,%rsp
mov    %esi,%ecx
mov    %rdi,%r13
movabs $0x736f686c61636f6c,%rax
68 6f 73
mov    %rax,0x10(%rsp)
movw   $0x74,0x18(%rsp)
lea    0xe37(%rip),%rdx
lea    0x1a(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x6,%esi
call   1030 <snprintf@plt>
xor    %ebp,%ebp
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rcx
mov    %rbx,%rsi
xor    %edx,%edx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    12d9 <tcp_client+0x139>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     12cd <tcp_client+0x12d>
mov    %eax,%r14d
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %r14d,%edi
call   1090 <connect@plt>
test   %eax,%eax
js     12bf <tcp_client+0x11f>
mov    $0xfffffff0,%ebx
lea    0xde2(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
lea    0x20(%rsp),%rsi
mov    $0x400000,%edx
call   1060 <read@plt>
mov    $0x1,%ebp
test   %rax,%rax
jle    12c5 <tcp_client+0x125>
mov    %r13,%rdi
lea    0xd98(%rip),%rsi
call   1040 <cg_read_long@plt>
mov    %rax,%r15
mov    %r13,%rdi
lea    0xd95(%rip),%rsi
mov    %r12,%rdx
call   1070 <cg_read_key_long@plt>
test   %r15,%r15
js     12c5 <tcp_client+0x125>
test   %rax,%rax
js     12c5 <tcp_client+0x125>
cmp    %rax,%r15
jl     12c5 <tcp_client+0x125>
mov    %r15,%rdi
mov    %rax,%rsi
mov    $0xa,%edx
call   1080 <values_close@plt>
test   %rax,%rax
jne    12c3 <tcp_client+0x123>
inc    %ebx
jne    1250 <tcp_client+0xb0>
mov    $0x1,%ebp
jmp    12c5 <tcp_client+0x125>
mov    %eax,%ebp
jmp    12c5 <tcp_client+0x125>
xor    %ebp,%ebp
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x400028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@__const.tcp_client.server = [10 x i8] c"localhost\00"
@.str = [4 x i8] c"%hd\00"
@.str.1 = [15 x i8] c"memory.current\00"
@.str.2 = [12 x i8] c"memory.stat\00"
@.str.3 = [6 x i8] c"sock \00"
define i32 @tcp_client(i8* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i16
%6 = alloca [10 x i8]
%7 = alloca %struct.addrinfo*
%8 = alloca [6 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1048576 x i32]
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %4
store i16 %1, i16* %5
%15 = bitcast [10 x i8]* %6 to i8*
store i32 16, i32* %9
%16 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%17 = load i16, i16* %5
%18 = call i32 @snprintf(i8* %16, i32 6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i16 zeroext %17)
%19 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%20 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%21 = call i32 @getaddrinfo(i8* %19, i8* %20, i32* null, %struct.addrinfo** %7)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = load i32, i32* %11
store i32 %25, i32* %3
br label %94
26: ; preds = %2
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 4
%29 = load i32, i32* %28
%30 = load %struct.addrinfo*, %struct.addrinfo** %7
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 3
%32 = load i32, i32* %31
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @socket(i32 %29, i32 %32, i32 %35)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %26
br label %90
40: ; preds = %26
%41 = load i32, i32* %10
%42 = load %struct.addrinfo*, %struct.addrinfo** %7
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %7
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @connect(i32 %41, i32 %44, i32 %47)
store i32 %48, i32* %11
%49 = load i32, i32* %11
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %40
br label %87
52: ; preds = %40
store i32 1, i32* %11
br label %53
53: ; preds = %85, %52
%54 = load i32, i32* %9
%55 = add nsw i32 %54, -1
store i32 %55, i32* %9
%56 = icmp ne i32 %54, 0
br i1 %56, label %57, label %86
57: ; preds = %53
%58 = load i32, i32* %10
%59 = getelementptr inbounds [1048576 x i32], [1048576 x i32]* %12, i64 0, i64 0
%60 = call i64 @read(i32 %58, i32* %59, i32 4194304)
%61 = icmp sle i64 %60, 0
br i1 %61, label %62, label %63
62: ; preds = %57
br label %87
63: ; preds = %57
%64 = load i8*, i8** %4
%65 = call i64 @cg_read_long(i8* %64, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i64 %65, i64* %13
%66 = load i8*, i8** %4
%67 = call i64 @cg_read_key_long(i8* %66, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i64 %67, i64* %14
%68 = load i64, i64* %13
%69 = icmp slt i64 %68, 0
br i1 %69, label %73, label %70
70: ; preds = %63
%71 = load i64, i64* %14
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %70, %63
br label %87
74: ; preds = %70
%75 = load i64, i64* %13
%76 = load i64, i64* %14
%77 = icmp slt i64 %75, %76
br i1 %77, label %78, label %79
78: ; preds = %74
br label %87
79: ; preds = %74
%80 = load i64, i64* %13
%81 = load i64, i64* %14
%82 = call i64 @values_close(i64 %80, i64 %81, i32 10)
%83 = icmp ne i64 %82, 0
br i1 %83, label %84, label %85
84: ; preds = %79
store i32 0, i32* %11
br label %86
85: ; preds = %79
br label %53
86: ; preds = %84, %53
br label %87
87: ; preds = %86, %78, %73, %62, %51
%88 = load i32, i32* %10
%89 = call i32 @close(i32 %88)
br label %90
90: ; preds = %87, %39
%91 = load %struct.addrinfo*, %struct.addrinfo** %7
%92 = call i32 @freeaddrinfo(%struct.addrinfo* %91)
%93 = load i32, i32* %11
store i32 %93, i32* %3
br label %94
94: ; preds = %90, %24
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@__const.tcp_client.server = [10 x i8] c"localhost\00"
@.str = [4 x i8] c"%hd\00"
@.str.1 = [15 x i8] c"memory.current\00"
@.str.2 = [12 x i8] c"memory.stat\00"
@.str.3 = [6 x i8] c"sock \00"
define i32 @tcp_client(i8* %0, i16 zeroext %1) {
%3 = alloca [10 x i8]
%4 = alloca %struct.addrinfo*
%5 = alloca [6 x i8]
%6 = alloca [1048576 x i32]
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.addrinfo** %4 to i8*
%9 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i16 zeroext %1)
%11 = call i32 @getaddrinfo(i8* %7, i8* %9, i32* null, %struct.addrinfo** %4)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %61
13: ; preds = %2
%14 = load %struct.addrinfo*, %struct.addrinfo** %4
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 4
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 3
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %16, i32 %18, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %57, label %23
23: ; preds = %13
%24 = load %struct.addrinfo*, %struct.addrinfo** %4
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @connect(i32 %21, i32 %26, i32 %28)
%30 = icmp slt i32 %29, 0
br i1 %30, label %54, label %31
31: ; preds = %23
%32 = bitcast [1048576 x i32]* %6 to i8*
%33 = getelementptr inbounds [1048576 x i32], [1048576 x i32]* %6, i64 0, i64 0
br label %34
34: ; preds = %31, %51
%35 = phi i32 [ 15, %31 ], [ %52, %51 ]
%36 = call i64 @read(i32 %21, i32* %33, i32 4194304)
%37 = icmp slt i64 %36, 1
br i1 %37, label %49, label %38
38: ; preds = %34
%39 = call i64 @cg_read_long(i8* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%40 = call i64 @cg_read_key_long(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp slt i64 %39, 0
%42 = icmp slt i64 %40, 0
%43 = select i1 %41, i1 true, i1 %42
%44 = icmp slt i64 %39, %40
%45 = select i1 %43, i1 true, i1 %44
br i1 %45, label %49, label %46
46: ; preds = %38
%47 = call i64 @values_close(i64 %39, i64 %40, i32 10)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %34, %38, %46
%50 = phi i32 [ 0, %46 ], [ 1, %38 ], [ 1, %34 ]
br label %54
51: ; preds = %46
%52 = add nsw i32 %35, -1
%53 = icmp eq i32 %35, 0
br i1 %53, label %54, label %34
54: ; preds = %51, %49, %23
%55 = phi i32 [ %29, %23 ], [ %50, %49 ], [ 1, %51 ]
%56 = call i32 @close(i32 %21)
br label %57
57: ; preds = %13, %54
%58 = phi i32 [ 0, %13 ], [ %55, %54 ]
%59 = load %struct.addrinfo*, %struct.addrinfo** %4
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
br label %61
61: ; preds = %2, %57
%62 = phi i32 [ %58, %57 ], [ %11, %2 ]
ret i32 %62
}