#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 */
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int read(int, char *, int const);
int /*<<< orphan*/ sa_log(char *, char *, ...);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
char strlen(char *);
int write(int, char *, char);
void client_loop(struct sockaddr *server_addr, char *prog, char *server,
                 char *port_str, int time) {
  int sock;
  int ret;
  const int BUFSIZE = 4096;
  char buf[BUFSIZE];
  while (1) {
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
      sa_log("ERROR: Could not create socket: %s", strerror(errno));
      exit(1);
    }
    ret = connect(sock, (struct sockaddr *)server_addr, sizeof(*server_addr));
    if (ret < 0) {
      sa_log("ERROR: connect to %s:%s failed: %s", server, port_str,
             strerror(errno));
    } else {
      sa_log("OK Connected to %s:%s", server, port_str);
      while (1) {
        char *ping = "ping";
        char pinglen = strlen(ping);
        ret = write(sock, ping, pinglen);
        if (ret != pinglen) {
          sa_log("ERROR: write to %s:%s failed: %s", server, port_str,
                 strerror(errno));
          break;
        }
        ret = read(sock, buf, BUFSIZE);
        if (ret < 0) {
          sa_log("ERROR: read from %s:%s failed: %s", server, port_str,
                 strerror(errno));
          break;
        }
        sa_log("OK received reply = '%s'", buf);
        sleep(time);
      }
    }
    close(sock);
    sleep(5);
  }
}


x86 assembly (clang, -O0):
<client_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1080,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movl   $0x1000,-0x30(%rbp)
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
jmp    11d2 <client_loop+0x32>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1221 <client_loop+0x81>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1070 <sa_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1287 <client_loop+0xe7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1060(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x1058(%rbp)
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1060(%rbp),%rsi
mov    -0x1058(%rbp),%rdx
mov    %rax,%rcx
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1070 <sa_log@plt>
jmp    13b3 <client_loop+0x213>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1070 <sa_log@plt>
jmp    12a2 <client_loop+0x102>
lea    0xdb3(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %al,-0x41(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x40(%rbp),%rsi
movsbl -0x41(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
movsbl -0x41(%rbp),%ecx
cmp    %ecx,%eax
je     1323 <client_loop+0x183>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1070(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x1068(%rbp)
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1070(%rbp),%rsi
mov    -0x1068(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1070 <sa_log@plt>
jmp    13ae <client_loop+0x20e>
mov    -0x28(%rbp),%edi
lea    -0x1050(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    138c <client_loop+0x1ec>
mov    -0x18(%rbp),%rax
mov    %rax,-0x1080(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x1078(%rbp)
mov    0x2c6f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1080(%rbp),%rsi
mov    -0x1078(%rbp),%rdx
mov    %rax,%rcx
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1070 <sa_log@plt>
jmp    13ae <client_loop+0x20e>
lea    0xd11(%rip),%rdi
lea    -0x1050(%rbp),%rsi
mov    $0x0,%al
call   1070 <sa_log@plt>
mov    -0x24(%rbp),%edi
call   10b0 <sleep@plt>
jmp    129d <client_loop+0xfd>
jmp    13b3 <client_loop+0x213>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
jmp    11cd <client_loop+0x2d>

x86 assembly (clang, -O2):
<client_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1028,%rsp
mov    %r8d,0xc(%rsp)
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rdi,0x18(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
jns    1207 <client_loop+0x67>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe0d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <sa_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%r12d
lea    0xe4b(%rip),%r13
mov    %rbp,0x10(%rsp)
jmp    127e <client_loop+0xde>
lea    0xe63(%rip),%r15
mov    %r14,%rbx
mov    0x10(%rsp),%rbp
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <sa_log@plt>
mov    %r12d,%edi
call   1050 <close@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
js     11de <client_loop+0x3e>
mov    %r12d,%edi
mov    0x18(%rsp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
lea    0xd8c(%rip),%r15
test   %eax,%eax
js     1227 <client_loop+0x87>
lea    0xda4(%rip),%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <sa_log@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
movsbl %al,%r14d
mov    %r12d,%edi
mov    %r13,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
lea    0xd91(%rip),%r15
cmp    %r14d,%eax
jne    1227 <client_loop+0x87>
mov    %rbx,%r14
mov    0xc(%rsp),%r15d
lea    0x20(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1218 <client_loop+0x78>
lea    0xd95(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <sa_log@plt>
mov    %r15d,%edi
call   10b0 <sleep@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
movsbl %al,%ebx
mov    %r12d,%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
je     12f0 <client_loop+0x150>
lea    0xd1d(%rip),%r15
jmp    121f <client_loop+0x7f>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"ERROR: Could not create socket: %s\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"ERROR: connect to %s:%s failed: %s\00"
@.str.2 = [22 x i8] c"OK Connected to %s:%s\00"
@.str.3 = [5 x i8] c"ping\00"
@.str.4 = [33 x i8] c"ERROR: write to %s:%s failed: %s\00"
@.str.5 = [34 x i8] c"ERROR: read from %s:%s failed: %s\00"
@.str.6 = [25 x i8] c"OK received reply = '%s'\00"
define void @client_loop(%struct.sockaddr* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca %struct.sockaddr*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8
store %struct.sockaddr* %0, %struct.sockaddr** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store i32 4096, i32* %13
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %14
%18 = alloca i8, i64 4096
br label %19
19: ; preds = %81, %5
br label %20
20: ; preds = %19
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = load i32, i32* %11
%33 = load %struct.sockaddr*, %struct.sockaddr** %6
%34 = call i32 @connect(i32 %32, %struct.sockaddr* %33, i32 4)
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %31
%38 = load i8*, i8** %8
%39 = load i8*, i8** %9
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %39, i8* %41)
br label %81
43: ; preds = %31
%44 = load i8*, i8** %8
%45 = load i8*, i8** %9
%46 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %44, i8* %45)
br label %47
47: ; preds = %76, %43
br label %48
48: ; preds = %47
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %15
%49 = load i8*, i8** %15
%50 = call signext i8 @strlen(i8* %49)
store i8 %50, i8* %16
%51 = load i32, i32* %11
%52 = load i8*, i8** %15
%53 = load i8, i8* %16
%54 = call i32 @write(i32 %51, i8* %52, i8 signext %53)
store i32 %54, i32* %12
%55 = load i32, i32* %12
%56 = load i8, i8* %16
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %55, %57
br i1 %58, label %59, label %65
59: ; preds = %48
%60 = load i8*, i8** %8
%61 = load i8*, i8** %9
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i8* %60, i8* %61, i8* %63)
br label %80
65: ; preds = %48
%66 = load i32, i32* %11
%67 = call i32 @read(i32 %66, i8* %18, i32 4096)
store i32 %67, i32* %12
%68 = load i32, i32* %12
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %76
70: ; preds = %65
%71 = load i8*, i8** %8
%72 = load i8*, i8** %9
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %71, i8* %72, i8* %74)
br label %80
76: ; preds = %65
%77 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %18)
%78 = load i32, i32* %10
%79 = call i32 @sleep(i32 %78)
br label %47
80: ; preds = %70, %59
br label %81
81: ; preds = %80, %37
%82 = load i32, i32* %11
%83 = call i32 @close(i32 %82)
%84 = call i32 @sleep(i32 5)
br label %19
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [35 x i8] c"ERROR: Could not create socket: %s\00"
@errno = global i32 0
@.str.1 = [35 x i8] c"ERROR: connect to %s:%s failed: %s\00"
@.str.2 = [22 x i8] c"OK Connected to %s:%s\00"
@.str.3 = [5 x i8] c"ping\00"
@.str.4 = [33 x i8] c"ERROR: write to %s:%s failed: %s\00"
@.str.5 = [34 x i8] c"ERROR: read from %s:%s failed: %s\00"
@.str.6 = [25 x i8] c"OK received reply = '%s'\00"
define void @client_loop(%struct.sockaddr* %0, i8* nocapture readnone %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca [4096 x i8]
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %37, %5
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %5, %37
%18 = phi i32 [ %46, %37 ], [ %10, %5 ]
%19 = call i32 @connect(i32 %18, %struct.sockaddr* %0, i32 4)
%20 = icmp slt i32 %19, 0
br i1 %20, label %37, label %21
21: ; preds = %17
%22 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %3)
%23 = call signext i8 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @write(i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8 signext %23)
%25 = sext i8 %23 to i32
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %21, %30
%28 = call i32 @read(i32 %18, i8* %7, i32 4096)
%29 = icmp slt i32 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %27
%31 = call i32 (i8*, i8*, ...) @sa_log(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %7)
%32 = call i32 @sleep(i32 %4)
%33 = call signext i8 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @write(i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8 signext %33)
%35 = sext i8 %33 to i32
%36 = icmp eq i32 %34, %35
br i1 %36, label %27, label %37
37: ; preds = %27, %30, %21, %17
%38 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), %17 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), %21 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), %30 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), %27 ]
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, i8*, ...) @sa_log(i8* %38, i8* %2, i8* %3, i8* %40)
%42 = call i32 @close(i32 %18)
%43 = call i32 @sleep(i32 5)
%44 = load i32, i32* @AF_INET
%45 = load i32, i32* @SOCK_STREAM
%46 = call i32 @socket(i32 %44, i32 %45, i32 0)
%47 = icmp slt i32 %46, 0
br i1 %47, label %12, label %17
}