#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;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, n;
  struct sockaddr_in serv_addr;
  struct hostent *server;
  char buffer[256];
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("ERROR opening socket");
    exit(1);
  }
  server = gethostbyname("127.0.0.1");
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(7889);
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR connecting");
    exit(1);
  }
  printf("Sending command to quit camTimer on port %d.\r\n", 7889);
  bzero(buffer, 256);
  buffer[0] = 'X';
  buffer[1] = 'X';
  n = write(sockfd, buffer, 2);
  if (n < 0) {
    perror("ERROR writing to socket");
    exit(1);
  }
  bzero(buffer, 256);
  n = read(sockfd, buffer, 255);
  if (n < 0) {
    perror("ERROR reading from socket");
    exit(1);
  }
  printf("Response: %s\n", buffer);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1225 <main+0x55>
lea    0xdea(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xde9(%rip),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    125c <main+0x8c>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
mov    $0x1ed1,%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    $0x0,%rax
jge    12cf <main+0xff>
lea    0xd74(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd6f(%rip),%rdi
mov    $0x1ed1,%esi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
movb   $0x58,-0x130(%rbp)
movb   $0x58,-0x12f(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1338 <main+0x168>
lea    0xd4b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x130(%rbp),%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1380 <main+0x1b0>
lea    0xd1b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x130(%rbp),%rsi
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    0x2e04(%rip),%r15
mov    (%r15),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     12f3 <main+0x123>
mov    %eax,%ebp
lea    0xe0d(%rip),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     12fc <main+0x12c>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbx),%rsi
lea    0x4(%rsp),%rdi
mov    (%rbx),%edx
call   10a0 <memmove@plt>
mov    $0x1ed1,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     1318 <main+0x148>
lea    0xdd9(%rip),%rdi
mov    $0x1ed1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
movw   $0x5858,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1321 <main+0x151>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10c0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     132a <main+0x15a>
lea    0xdd6(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd06(%rip),%rdi
jmp    1331 <main+0x161>
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd15(%rip),%rdi
jmp    1331 <main+0x161>
lea    0xd4c(%rip),%rdi
jmp    1331 <main+0x161>
lea    0xd5b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.3 = [17 x i8] c"ERROR connecting\00"
@.str.4 = [47 x i8] c"Sending command to quit camTimer on port %d.\0D\0A\00"
@.str.5 = [24 x i8] c"ERROR writing to socket\00"
@.str.6 = [26 x i8] c"ERROR reading from socket\00"
@.str.7 = [14 x i8] c"Response: %s\0A\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 %struct.hostent*
%10 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store %struct.hostent* %20, %struct.hostent** %9
%21 = load %struct.hostent*, %struct.hostent** %9
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %19
%28 = bitcast %struct.sockaddr_in* %8 to i8*
%29 = call i32 @bzero(i8* %28, i32 12)
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %30, i32* %31
%32 = load %struct.hostent*, %struct.hostent** %9
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 1
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to i8*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%37 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %36, i32 0, i32 0
%38 = bitcast i32* %37 to i8*
%39 = load %struct.hostent*, %struct.hostent** %9
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @bcopy(i8* %35, i8* %38, i32 %41)
%43 = call i32 @htons(i32 7889)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%47 = call i64 @connect(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %27
%50 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %27
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 7889)
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%55 = call i32 @bzero(i8* %54, i32 256)
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
store i8 88, i8* %56
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 1
store i8 88, i8* %57
%58 = load i32, i32* %6
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%60 = call i32 @write(i32 %58, i8* %59, i32 2)
store i32 %60, i32* %7
%61 = load i32, i32* %7
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %52
%64 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %52
%67 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%68 = call i32 @bzero(i8* %67, i32 256)
%69 = load i32, i32* %6
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%71 = call i32 @read(i32 %69, i8* %70, i32 255)
store i32 %71, i32* %7
%72 = load i32, i32* %7
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %66
%75 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %66
%78 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %78)
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.2 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.3 = [17 x i8] c"ERROR connecting\00"
@.str.4 = [47 x i8] c"Sending command to quit camTimer on port %d.\0D\0A\00"
@.str.5 = [24 x i8] c"ERROR writing to socket\00"
@.str.6 = [26 x i8] c"ERROR reading from socket\00"
@.str.7 = [14 x i8] c"Response: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @exit(i32 0)
unreachable
21: ; preds = %14
%22 = call i32 @bzero(i8* %5, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 1
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to i8*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%29 = bitcast i32* %28 to i8*
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i32 @htons(i32 7889)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i64 @connect(i32 %9, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %21
%38 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %21
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 7889)
%42 = call i32 @bzero(i8* %6, i32 256)
store i8 88, i8* %6
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 1
store i8 88, i8* %43
%44 = call i32 @write(i32 %9, i8* %6, i32 2)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %40
%50 = call i32 @bzero(i8* %6, i32 256)
%51 = call i32 @read(i32 %9, i8* %6, i32 255)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %49
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%58 = call i32 @close(i32 %9)
ret i32 0
}