#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 */
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
char *is_ipv6(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  ssize_t n;
  char *line = NULL;
  char *ip_addr;
  size_t size = 32;
  FILE *fp;
  const char *filename = argv[1];
  fp = fopen(filename, "r");
  if (!fp) {
    fprintf(stderr, "Error in opening the file!\n");
    exit(1);
  }
  while ((n = getline(&line, &size, fp)) != -1) {
    if (line[n - 1] == '\n') {
      line[n - 1] = '\0';
      ip_addr = (char *)malloc(n - 2);
      strncpy(ip_addr, line, n - 2);
    } else {
      ip_addr = (char *)malloc(n);
      strncpy(ip_addr, line, n);
    }
    printf("Is '%s' valid IPv6? \n%s\n", ip_addr, is_ipv6(ip_addr));
  }
  fclose(fp);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x20,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1200 <main+0x70>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1205 <main+0x75>
mov    -0x38(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <getline@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12b9 <main+0x129>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1274 <main+0xe4>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%edi
sub    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x2,%edx
call   1030 <strncpy@plt>
jmp    1290 <main+0x100>
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <is_ipv6@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xd6f(%rip),%rdi
call   1050 <printf@plt>
jmp    1205 <main+0x75>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x20,0x10(%rsp)
mov    0x8(%rsi),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1276 <main+0xe6>
mov    %rax,%r13
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     125d <main+0xcd>
mov    %eax,%ebp
lea    0xe32(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    1244 <main+0xb4>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
call   10b0 <is_ipv6@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <getline@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     125d <main+0xcd>
mov    0x8(%rsp),%rax
movslq %ebp,%rcx
cmpb   $0xa,-0x1(%rcx,%rax,1)
jne    1200 <main+0x70>
movb   $0x0,-0x1(%rax,%rcx,1)
add    $0xfffffffe,%ebp
jmp    1200 <main+0x70>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xd7c(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Error in opening the file!\0A\00"
@.str.2 = [25 x i8] c"Is '%s' valid IPv6? \0A%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i64 32, i64* %9
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %10
%17 = load i32*, i32** %10
%18 = icmp ne i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
br label %24
24: ; preds = %60, %23
%25 = load i32*, i32** %10
%26 = call i32 @getline(i8** %7, i64* %9, i32* %25)
store i32 %26, i32* %6
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %65
28: ; preds = %24
%29 = load i8*, i8** %7
%30 = load i32, i32* %6
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %37, label %52
37: ; preds = %28
%38 = load i8*, i8** %7
%39 = load i32, i32* %6
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
store i8 0, i8* %42
%43 = load i32, i32* %6
%44 = sub nsw i32 %43, 2
%45 = call i64 @malloc(i32 %44)
%46 = inttoptr i64 %45 to i8*
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = load i8*, i8** %7
%49 = load i32, i32* %6
%50 = sub nsw i32 %49, 2
%51 = call i32 @strncpy(i8* %47, i8* %48, i32 %50)
br label %60
52: ; preds = %28
%53 = load i32, i32* %6
%54 = call i64 @malloc(i32 %53)
%55 = inttoptr i64 %54 to i8*
store i8* %55, i8** %8
%56 = load i8*, i8** %8
%57 = load i8*, i8** %7
%58 = load i32, i32* %6
%59 = call i32 @strncpy(i8* %56, i8* %57, i32 %58)
br label %60
60: ; preds = %52, %37
%61 = load i8*, i8** %8
%62 = load i8*, i8** %8
%63 = call i8* @is_ipv6(i8* %62)
%64 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %61, i8* %63)
br label %24
65: ; preds = %24
%66 = load i32*, i32** %10
%67 = call i32 @fclose(i32* %66)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Error in opening the file!\0A\00"
@.str.2 = [25 x i8] c"Is '%s' valid IPv6? \0A%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i64* %4 to i8*
store i64 32, i64* %4
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i32 @getline(i8** %3, i64* %4, i32* %9)
%13 = icmp eq i32 %12, -1
br i1 %13, label %43, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11, %37
%19 = phi i32 [ %41, %37 ], [ %12, %11 ]
%20 = load i8*, i8** %3
%21 = add nsw i32 %19, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = icmp eq i8 %24, 10
br i1 %25, label %26, label %32
26: ; preds = %18
store i8 0, i8* %23
%27 = add nsw i32 %19, -2
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
%30 = load i8*, i8** %3
%31 = call i32 @strncpy(i8* %29, i8* %30, i32 %27)
br label %37
32: ; preds = %18
%33 = call i64 @malloc(i32 %19)
%34 = inttoptr i64 %33 to i8*
%35 = load i8*, i8** %3
%36 = call i32 @strncpy(i8* %34, i8* %35, i32 %19)
br label %37
37: ; preds = %32, %26
%38 = phi i8* [ %29, %26 ], [ %34, %32 ]
%39 = call i8* @is_ipv6(i8* %38)
%40 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %38, i8* %39)
%41 = call i32 @getline(i8** %3, i64* %4, i32* %9)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %18
43: ; preds = %37, %11
%44 = call i32 @fclose(i32* %9)
ret i32 0
}