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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, fd;
  struct sockaddr_in serv_addr;
  int str_len;
  char msg[128];
  if (argc != 3) {
    fprintf(stderr, "Usage: ./%s <ip> <port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  fd = open("data.txt", O_CREAT | O_WRONLY | O_TRUNC);
  if (fd == -1) {
    perror("open() failed");
    exit(EXIT_FAILURE);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket() failed");
    exit(EXIT_FAILURE);
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
  serv_addr.sin_port = htons(atoi(argv[2]));
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    fprintf(stderr, "connect() failed: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  while ((str_len = read(sockfd, msg, sizeof(msg))) != 0)
    write(fd, msg, str_len);
  write(sockfd, "ok", 2);
  close(fd);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     122d <main+0x4d>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde6(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2d63(%rip),%rax
or     (%rax),%esi
mov    0x2daa(%rip),%rax
or     (%rax),%esi
lea    0xdca(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    127b <main+0x9b>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12ba <main+0xda>
lea    0xd89(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1357 <main+0x177>
mov    0x2caa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xb4(%rbp)
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0xb4(%rbp),%edi
mov    %rax,%rdx
lea    0xcfc(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    135c <main+0x17c>
mov    -0x14(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     1393 <main+0x1b3>
mov    -0x18(%rbp),%edi
lea    -0xb0(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
jmp    135c <main+0x17c>
mov    -0x14(%rbp),%edi
lea    0xcb9(%rip),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    1314 <main+0x134>
mov    0x2df6(%rip),%rax
mov    0x2d97(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
lea    0xdfc(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     1329 <main+0x149>
mov    %eax,%r12d
mov    0x2da3(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
je     1337 <main+0x157>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    0x10(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     1345 <main+0x165>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x80,%edx
call   1070 <read@plt>
test   %eax,%eax
je     12e0 <main+0x100>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
test   %eax,%eax
jne    12c0 <main+0xe0>
lea    0xd6f(%rip),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xcd9(%rip),%rsi
jmp    1368 <main+0x188>
lea    0xcf2(%rip),%rdi
call   10b0 <perror@plt>
jmp    136d <main+0x18d>
lea    0xcf2(%rip),%rdi
call   10b0 <perror@plt>
jmp    136d <main+0x18d>
mov    0x2c7c(%rip),%rax
mov    (%rax),%ebx
mov    0x2c63(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xcdd(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    0x2c3c(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Usage: ./%s <ip> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"data.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [14 x i8] c"open() failed\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [16 x i8] c"socket() failed\00"
@.str.4 = [22 x i8] c"connect() failed: %s\0A\00"
@errno = global i32 0
@.str.5 = [3 x i8] c"ok\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_WRONLY
%24 = or i32 %22, %23
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %21
%35 = load i32, i32* @AF_INET
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %34
%44 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @inet_addr(i8* %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%52 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %51, i32 0, i32 0
store i32 %50, i32* %52
%53 = load i8**, i8*** %5
%54 = getelementptr inbounds i8*, i8** %53, i64 2
%55 = load i8*, i8** %54
%56 = call i32 @atoi(i8* %55)
%57 = call i32 @htons(i32 %56)
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %57, i32* %58
%59 = load i32, i32* %6
%60 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%61 = call i32 @connect(i32 %59, %struct.sockaddr* %60, i32 12)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %70
63: ; preds = %43
%64 = load i32, i32* @stderr
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %66)
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %43
br label %71
71: ; preds = %76, %70
%72 = load i32, i32* %6
%73 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 128)
store i32 %74, i32* %9
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %81
76: ; preds = %71
%77 = load i32, i32* %7
%78 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%79 = load i32, i32* %9
%80 = call i32 @write(i32 %77, i8* %78, i32 %79)
br label %71
81: ; preds = %71
%82 = load i32, i32* %6
%83 = call i32 @write(i32 %82, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%84 = load i32, i32* %7
%85 = call i32 @close(i32 %84)
%86 = load i32, i32* %6
%87 = call i32 @close(i32 %86)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"Usage: ./%s <ip> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"data.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [14 x i8] c"open() failed\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [16 x i8] c"socket() failed\00"
@.str.4 = [22 x i8] c"connect() failed: %s\0A\00"
@errno = global i32 0
@.str.5 = [3 x i8] c"ok\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [128 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 3
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %16, %15
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = tail call i32 @socket(i32 %27, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = tail call i32 @exit(i32 %33)
unreachable
35: ; preds = %26
%36 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %37, i32* %38
%39 = getelementptr inbounds i8*, i8** %1, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @inet_addr(i8* %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %41, i32* %42
%43 = getelementptr inbounds i8*, i8** %1, i64 2
%44 = load i8*, i8** %43
%45 = call i32 @atoi(i8* %44)
%46 = call i32 @htons(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %46, i32* %47
%48 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%49 = call i32 @connect(i32 %29, %struct.sockaddr* %48, i32 12)
%50 = icmp eq i32 %49, -1
br i1 %50, label %54, label %51
51: ; preds = %35
%52 = call i32 @read(i32 %29, i8* %6, i32 128)
%53 = icmp eq i32 %52, 0
br i1 %53, label %66, label %61
54: ; preds = %35
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %51, %61
%62 = phi i32 [ %64, %61 ], [ %52, %51 ]
%63 = call i32 @write(i32 %20, i8* %6, i32 %62)
%64 = call i32 @read(i32 %29, i8* %6, i32 128)
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %61
66: ; preds = %61, %51
%67 = call i32 @write(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 2)
%68 = call i32 @close(i32 %20)
%69 = call i32 @close(i32 %29)
ret i32 0
}