#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 {
  int sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
main() {
  int sock1;
  sock1 = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in serv;
  serv.sin_port = htons(2080);
  printf("%x %x\n", 2080, htons(2080));
  serv.sin_family = AF_INET;
  serv.sin_addr.s_addr = inet_addr("127.0.0.1");
  printf("client connecting\n");
  connect(sock1, (struct sockaddr *)&serv, sizeof(serv));
  char buf[50];
  FILE *fp = fopen("test.txt", "w");
  while (1) {
    read(sock1, buf, 50);
    if (strcmp(buf, "quit1234") == 0) {
      break;
    }
    fprintf(fp, "%s", buf);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    $0x820,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    $0x820,%edi
call   1040 <htons@plt>
mov    %eax,%edx
lea    0xe15(%rip),%rdi
mov    $0x820,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xdfd(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
lea    0xdec(%rip),%rdi
lea    0xdee(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x32,%edx
call   1060 <read@plt>
lea    -0x50(%rbp),%rdi
lea    0xdcb(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1278 <main+0xd8>
jmp    1291 <main+0xf1>
mov    -0x58(%rbp),%rdi
lea    -0x50(%rbp),%rdx
lea    0xdb1(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1248 <main+0xa8>
mov    -0x58(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    0x2e27(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
mov    $0x820,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    $0x820,%edi
call   1050 <htons@plt>
lea    0xe0c(%rip),%rdi
mov    $0x820,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xdf8(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
lea    0xe06(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
lea    0xdd6(%rip),%rdi
lea    0xdd8(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x32,%edx
call   1080 <read@plt>
lea    0xdb6(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     12ab <main+0xfb>
lea    0x10(%rsp),%rbp
lea    0xd9d(%rip),%r15
nop
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <fputs@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x32,%edx
call   1080 <read@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10a0 <strcmp@plt>
test   %rax,%rax
jne    1280 <main+0xd0>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"%x %x\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [19 x i8] c"client connecting\0A\00"
@.str.3 = [9 x i8] c"test.txt\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [9 x i8] c"quit1234\00"
@.str.6 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [50 x i8]
%5 = alloca i32*
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = call i32 @htons(i32 2080)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %9, i32* %10
%11 = call i32 @htons(i32 2080)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 2080, i32 %11)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* %2
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @connect(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %22, i32** %5
br label %23
23: ; preds = %0, %31
%24 = load i32, i32* %2
%25 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 50)
%27 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
br label %35
31: ; preds = %23
%32 = load i32*, i32** %5
%33 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%34 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %33)
br label %23
35: ; preds = %30
%36 = load i32*, i32** %5
%37 = call i32 @fclose(i32* %36)
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"%x %x\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [9 x i8] c"test.txt\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [9 x i8] c"quit1234\00"
@str = [18 x i8] c"client connecting\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [50 x i8]
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = tail call i32 @htons(i32 2080)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 2080)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 2080, i32 %9)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i32 @connect(i32 %5, %struct.sockaddr* %16, i32 12)
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%19 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @read(i32 %5, i8* %18, i32 50)
%21 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %0, %23
%24 = call i32 @fputs(i8* %18, i32* %19)
%25 = call i32 @read(i32 %5, i8* %18, i32 50)
%26 = call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %23
28: ; preds = %23, %0
%29 = call i32 @fclose(i32* %19)
ret i32 0
}