#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;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ send_msg;
typedef int /*<<< orphan*/ recv_msg;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ bzero(void *, int);
scalar_t__ connect(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
void dg_cli(FILE *fp, int sockfd, struct sockaddr *servaddr,
            socklen_t servlen) {
  int n;
  char recv_msg[256];
  char send_msg[256];
  if (connect(sockfd, servaddr, servlen) < 0) {
    perror("connect");
    exit(-1);
  }
  while (fgets(send_msg, sizeof(send_msg), fp) != NULL) {
    if (write(sockfd, send_msg, strlen(send_msg)) < 0) {
      perror("write");
      exit(-1);
    }
    printf("[client] send_msg = %s.\n", send_msg);
    bzero((void *)recv_msg, sizeof(recv_msg));
    if (read(sockfd, recv_msg, sizeof(recv_msg)) < 0) {
      perror("read");
      exit(-1);
    }
    printf("[client] recv_msg = %s.\n", recv_msg);
  }
}


x86 assembly (clang, -O0):
<dg_cli>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    11d8 <dg_cli+0x48>
lea    0xe37(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    11dd <dg_cli+0x4d>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12c3 <dg_cli+0x133>
mov    -0xc(%rbp),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1253 <dg_cli+0xc3>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x220(%rbp),%rsi
lea    0xdad(%rip),%rdi
call   1050 <printf@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jge    12ab <dg_cli+0x11b>
lea    0xd8b(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x120(%rbp),%rsi
lea    0xd73(%rip),%rdi
call   1050 <printf@plt>
jmp    11dd <dg_cli+0x4d>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dg_cli>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
mov    %rdx,%rsi
mov    %ecx,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     1286 <dg_cli+0xf6>
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1255 <dg_cli+0xc5>
mov    %rsp,%rbx
lea    0xe30(%rip),%r15
lea    0x100(%rsp),%r12
lea    0xe3f(%rip),%r13
nopl   (%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
js     1267 <dg_cli+0xd7>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %r12,%rdi
mov    $0x100,%esi
call   1090 <bzero@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x100,%edx
call   1060 <read@plt>
test   %rax,%rax
js     1270 <dg_cli+0xe0>
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    11f0 <dg_cli+0x60>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9a(%rip),%rdi
jmp    1277 <dg_cli+0xe7>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xd73(%rip),%rdi
jmp    1277 <dg_cli+0xe7>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"connect\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [25 x i8] c"[client] send_msg = %s.\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [25 x i8] c"[client] recv_msg = %s.\0A\00"
define void @dg_cli(i32* %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.sockaddr*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
%11 = alloca [256 x i8]
store i32* %0, i32** %5
store i32 %1, i32* %6
store %struct.sockaddr* %2, %struct.sockaddr** %7
store i32 %3, i32* %8
%12 = load i32, i32* %6
%13 = load %struct.sockaddr*, %struct.sockaddr** %7
%14 = load i32, i32* %8
%15 = call i64 @connect(i32 %12, %struct.sockaddr* %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %4
br label %21
21: ; preds = %48, %20
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%23 = load i32*, i32** %5
%24 = call i32* @fgets(i8* %22, i32 256, i32* %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %51
26: ; preds = %21
%27 = load i32, i32* %6
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i64 @write(i32 %27, i8* %28, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %26
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%38 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%40 = call i32 @bzero(i8* %39, i32 256)
%41 = load i32, i32* %6
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%43 = call i64 @read(i32 %41, i8* %42, i32 256)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %36
%49 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%50 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %49)
br label %21
51: ; preds = %21
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"connect\00"
@.str.1 = [6 x i8] c"write\00"
@.str.2 = [25 x i8] c"[client] send_msg = %s.\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [25 x i8] c"[client] recv_msg = %s.\0A\00"
define void @dg_cli(i32* nocapture %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca [256 x i8]
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%9 = tail call i64 @connect(i32 %1, %struct.sockaddr* %2, i32 %3)
%10 = icmp slt i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %4
%12 = call i32* @fgets(i8* %8, i32 256, i32* %0)
%13 = icmp eq i32* %12, null
br i1 %13, label %36, label %17
14: ; preds = %4
%15 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %11, %32
%18 = call i32 @strlen(i8* %8)
%19 = call i64 @write(i32 %1, i8* %8, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %17
%25 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%26 = call i32 @bzero(i8* %7, i32 256)
%27 = call i64 @read(i32 %1, i8* %7, i32 256)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %24
%33 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%34 = call i32* @fgets(i8* %8, i32 256, i32* %0)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %17
36: ; preds = %32, %11
ret void
}