#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ servername;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int line_parser(char **, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ sock_read(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int refill(const char *host, char **s1, char **s2) {
  int sock;
  struct sockaddr_in servername;
  servername.sin_family = AF_INET;
  servername.sin_port = htons(1477);
  struct hostent *hp = gethostbyname(host);
  bcopy(hp->h_addr, &(servername.sin_addr.s_addr), hp->h_length);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("Cannot create socket");
    return 0;
  }
  int r = connect(sock, (struct sockaddr *)&servername, sizeof(servername));
  if (r < 0) {
    printf("%d\n", errno);
    perror("connect (client)");
    return 0;
  }
  int lines_read = 0;
  if (sock_read(sock))
    lines_read = line_parser(s1, s2);
  close(sock);
  return lines_read;
}


x86 assembly (clang, -O0):
<refill>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x5c5,%edi
call   1030 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1070 <bcopy@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    122e <refill+0x8e>
lea    0xde3(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <refill+0x110>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1279 <refill+0xd9>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
lea    0xdb1(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <refill+0x110>
movl   $0x0,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <sock_read@plt>
cmp    $0x0,%rax
je     12a2 <refill+0x102>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10b0 <line_parser@plt>
mov    %eax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<refill>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e2e(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x5c5,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1060 <gethostbyname@plt>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x8(%rsp),%rsi
call   1070 <bcopy@plt>
mov    0x0(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1247 <refill+0xa7>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     1255 <refill+0xb5>
mov    %ebp,%edi
call   1080 <sock_read@plt>
test   %rax,%rax
je     1231 <refill+0x91>
mov    %r15,%rdi
mov    %r14,%rsi
call   10b0 <line_parser@plt>
mov    %eax,%ebx
jmp    1233 <refill+0x93>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb2(%rip),%rdi
call   1090 <perror@plt>
jmp    123a <refill+0x9a>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
lea    0xdb0(%rip),%rdi
call   1040 <printf@plt>
lea    0xda8(%rip),%rdi
call   1090 <perror@plt>
xor    %ebx,%ebx
jmp    123a <refill+0x9a>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"connect (client)\00"
define i32 @refill(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.hostent*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 1477)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i8*, i8** %5
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %10
%19 = load %struct.hostent*, %struct.hostent** %10
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = load %struct.hostent*, %struct.hostent** %10
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @bcopy(i32 %21, i32* %23, i32 %26)
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %3
%34 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %57
35: ; preds = %3
%36 = load i32, i32* %8
%37 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%38 = call i32 @connect(i32 %36, %struct.sockaddr* %37, i32 12)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = load i32, i32* @errno
%43 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %4
br label %57
45: ; preds = %35
store i32 0, i32* %12
%46 = load i32, i32* %8
%47 = call i64 @sock_read(i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i8**, i8*** %6
%51 = load i8**, i8*** %7
%52 = call i32 @line_parser(i8** %50, i8** %51)
store i32 %52, i32* %12
br label %53
53: ; preds = %49, %45
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %12
store i32 %56, i32* %4
br label %57
57: ; preds = %53, %41, %33
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot create socket\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"connect (client)\00"
define i32 @refill(i8* %0, i8** %1, i8** %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 1477)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %8, i32* %9
%10 = tail call %struct.hostent* @gethostbyname(i8* %0)
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 1
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @bcopy(i32 %12, i32* %13, i32 %15)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %39
23: ; preds = %3
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i32 @connect(i32 %19, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %39
31: ; preds = %23
%32 = call i64 @sock_read(i32 %19)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 @line_parser(i8** %1, i8** %2)
br label %36
36: ; preds = %34, %31
%37 = phi i32 [ %35, %34 ], [ 0, %31 ]
%38 = call i32 @close(i32 %19)
br label %39
39: ; preds = %27, %36, %21
%40 = phi i32 [ 0, %21 ], [ 0, %27 ], [ %37, %36 ]
ret i32 %40
}