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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
unsigned int strlen(char *);
unsigned int write(int, char *, unsigned int);
int main(void) {
  FILE *log = fopen("../data/syslog", "r");
  char buffer[1023];
  int sockfd = 0;
  unsigned n = 0;
  char recv_buff[1024];
  struct sockaddr_in serv_addr;
  if (log == NULL) {
    printf("ERROR: log file opening failed!\n");
    return 1;
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("\n Error : Could not create socket \n");
    return 1;
  }
  memset(&serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(4343);
  if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
    printf("\n inet_pton error occured\n");
    return 1;
  }
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    printf("\n Error : Connect Failed \n");
    return 1;
  }
  do {
    char *str = fgets(buffer, 1023, log);
    if (str == NULL)
      continue;
    while ((n += write(sockfd, buffer + n, strlen(buffer) - n)) <
           strlen(buffer))
      ;
    n = 0;
    printf("Sent: %s", buffer);
  } while (!feof(log));
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe27(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x414(%rbp)
movl   $0x0,-0x418(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1222 <main+0x62>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1413 <main+0x253>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jge    1264 <main+0xa4>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1413 <main+0x253>
lea    -0x830(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x828(%rbp)
mov    $0x10f7,%edi
call   1050 <htons@plt>
mov    %eax,-0x82c(%rbp)
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    -0x830(%rbp),%rdx
lea    0xda6(%rip),%rsi
call   10b0 <inet_pton@plt>
cmp    $0x0,%rax
jg     12d9 <main+0x119>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1413 <main+0x253>
mov    -0x414(%rbp),%edi
lea    -0x830(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    1314 <main+0x154>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1413 <main+0x253>
jmp    1319 <main+0x159>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3ff,%esi
call   1090 <fgets@plt>
mov    %rax,-0x838(%rbp)
cmpq   $0x0,-0x838(%rbp)
jne    1348 <main+0x188>
jmp    13e8 <main+0x228>
jmp    134d <main+0x18d>
mov    -0x414(%rbp),%eax
mov    %eax,-0x84c(%rbp)
lea    -0x410(%rbp),%rax
mov    -0x418(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x848(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x84c(%rbp),%edi
mov    -0x848(%rbp),%rsi
mov    %eax,%edx
sub    -0x418(%rbp),%edx
call   1030 <write@plt>
add    -0x418(%rbp),%eax
mov    %eax,-0x83c(%rbp)
mov    %eax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x83c(%rbp),%eax
cmp    %ecx,%eax
jae    13c9 <main+0x209>
jmp    134d <main+0x18d>
movl   $0x0,-0x418(%rbp)
lea    -0x410(%rbp),%rsi
lea    0xcb5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1319 <main+0x159>
mov    -0x414(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
lea    0xe18(%rip),%rdi
lea    0xe20(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     130e <main+0x13e>
mov    %rax,%r14
mov    0x2de1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1317 <main+0x147>
mov    %eax,%r12d
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x10f7,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
lea    0xdb8(%rip),%rsi
mov    %rbx,%rdx
call   10c0 <inet_pton@plt>
test   %rax,%rax
jle    132c <main+0x15c>
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10e0 <connect@plt>
test   %rax,%rax
js     1335 <main+0x165>
lea    0x10(%rsp),%r13
lea    0xd8c(%rip),%r15
jmp    12ac <main+0xdc>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   10b0 <feof@plt>
test   %eax,%eax
jne    1320 <main+0x150>
mov    %r13,%rdi
mov    $0x3ff,%esi
mov    %r14,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     12a0 <main+0xd0>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%eax
lea    (%rsp,%rax,1),%rbp
add    $0x10,%rbp
mov    %r13,%rdi
call   1050 <strlen@plt>
sub    %ebx,%eax
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
add    %eax,%ebx
mov    %r13,%rdi
call   1050 <strlen@plt>
cmp    %eax,%ebx
jb     12d0 <main+0x100>
mov    %r15,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12a0 <main+0xd0>
lea    0xd66(%rip),%rdi
jmp    133c <main+0x16c>
lea    0xd3a(%rip),%rdi
jmp    133c <main+0x16c>
mov    %r12d,%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    1346 <main+0x176>
lea    0xd0b(%rip),%rdi
jmp    133c <main+0x16c>
lea    0xce8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"../data/syslog\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [33 x i8] c"ERROR: log file opening failed!\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [36 x i8] c"\0A Error : Could not create socket \0A\00"
@.str.4 = [10 x i8] c"127.0.0.1\00"
@.str.5 = [27 x i8] c"\0A inet_pton error occured\0A\00"
@.str.6 = [27 x i8] c"\0A Error : Connect Failed \0A\00"
@.str.7 = [9 x i8] c"Sent: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1023 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca i8*
store i32 0, i32* %1
%9 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %2
store i32 0, i32* %4
store i32 0, i32* %5
%10 = load i32*, i32** %2
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %77
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %77
21: ; preds = %14
%22 = call i32 @memset(%struct.sockaddr_in* %7, i32 48, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @htons(i32 4343)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%29 = call i64 @inet_pton(i32 %27, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32* %28)
%30 = icmp sle i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %21
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %77
33: ; preds = %21
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%36 = call i64 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %77
40: ; preds = %33
br label %41
41: ; preds = %69, %40
%42 = getelementptr inbounds [1023 x i8], [1023 x i8]* %3, i64 0, i64 0
%43 = load i32*, i32** %2
%44 = call i8* @fgets(i8* %42, i32 1023, i32* %43)
store i8* %44, i8** %8
%45 = load i8*, i8** %8
%46 = icmp eq i8* %45, null
br i1 %46, label %47, label %48
47: ; preds = %41
br label %69
48: ; preds = %41
br label %49
49: ; preds = %65, %48
%50 = load i32, i32* %4
%51 = getelementptr inbounds [1023 x i8], [1023 x i8]* %3, i64 0, i64 0
%52 = load i32, i32* %5
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = getelementptr inbounds [1023 x i8], [1023 x i8]* %3, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = load i32, i32* %5
%58 = sub i32 %56, %57
%59 = call i32 @write(i32 %50, i8* %54, i32 %58)
%60 = load i32, i32* %5
%61 = add i32 %60, %59
store i32 %61, i32* %5
%62 = getelementptr inbounds [1023 x i8], [1023 x i8]* %3, i64 0, i64 0
%63 = call i32 @strlen(i8* %62)
%64 = icmp ult i32 %61, %63
br i1 %64, label %65, label %66
65: ; preds = %49
br label %49
66: ; preds = %49
store i32 0, i32* %5
%67 = getelementptr inbounds [1023 x i8], [1023 x i8]* %3, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %67)
br label %69
69: ; preds = %66, %47
%70 = load i32*, i32** %2
%71 = call i32 @feof(i32* %70)
%72 = icmp ne i32 %71, 0
%73 = xor i1 %72, true
br i1 %73, label %41, label %74
74: ; preds = %69
%75 = load i32, i32* %4
%76 = call i32 @close(i32 %75)
store i32 0, i32* %1
br label %77
77: ; preds = %74, %38, %31, %19, %12
%78 = load i32, i32* %1
ret i32 %78
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"../data/syslog\00"
@.str.1 = [2 x i8] c"r\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [10 x i8] c"127.0.0.1\00"
@.str.7 = [9 x i8] c"Sent: %s\00"
@str = [26 x i8] c"\0A Error : Connect Failed \00"
@str.8 = [26 x i8] c"\0A inet_pton error occured\00"
@str.9 = [35 x i8] c"\0A Error : Could not create socket \00"
@str.10 = [32 x i8] c"ERROR: log file opening failed!\00"
define i32 @main() {
%1 = alloca [1023 x i8]
%2 = alloca %struct.sockaddr_in
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = getelementptr inbounds [1023 x i8], [1023 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = icmp eq i32* %3, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0))
br label %54
9: ; preds = %0
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.9, i64 0, i64 0))
br label %54
16: ; preds = %9
%17 = call i32 @memset(%struct.sockaddr_in* %2, i32 48, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 4343)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%24 = call i64 @inet_pton(i32 %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32* %23)
%25 = icmp slt i64 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
br label %54
28: ; preds = %16
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i64 @connect(i32 %12, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
br label %54
34: ; preds = %28, %49
%35 = call i8* @fgets(i8* %4, i32 1023, i32* %3)
%36 = icmp eq i8* %35, null
br i1 %36, label %49, label %37
37: ; preds = %34, %37
%38 = phi i32 [ %44, %37 ], [ 0, %34 ]
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds [1023 x i8], [1023 x i8]* %1, i64 0, i64 %39
%41 = call i32 @strlen(i8* %4)
%42 = sub i32 %41, %38
%43 = call i32 @write(i32 %12, i8* %40, i32 %42)
%44 = add i32 %43, %38
%45 = call i32 @strlen(i8* %4)
%46 = icmp ult i32 %44, %45
br i1 %46, label %37, label %47
47: ; preds = %37
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %4)
br label %49
49: ; preds = %34, %47
%50 = call i32 @feof(i32* %3)
%51 = icmp eq i32 %50, 0
br i1 %51, label %34, label %52
52: ; preds = %49
%53 = call i32 @close(i32 %12)
br label %54
54: ; preds = %52, %32, %26, %14, %7
%55 = phi i32 [ 1, %7 ], [ 1, %14 ], [ 1, %26 ], [ 1, %32 ], [ 0, %52 ]
ret i32 %55
}