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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
scalar_t__ strncmp(char *, char *, int);
char *find_default_interface(void) {
  FILE *fh = fopen("/proc/net/route", "r");
  char buf[128], *ptr = NULL;
  if (fh == NULL) {
    perror("fopen");
    return NULL;
  }
  while (ptr == NULL && fgets(buf, 128, fh) != NULL) {
    ptr = strchr(buf, '\t');
    if (ptr == NULL)
      continue;
    *ptr++ = '\0';
    ptr = strncmp(ptr, "00000000", 8) == 0 ? buf : NULL;
  }
  if (ferror(fh) != 0) {
    fprintf(stderr, "Error: read error.\n");
    ptr = NULL;
  }
  fclose(fh);
  return ptr;
}


x86 assembly (clang, -O0):
<find_default_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    0xe6e(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x98(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d1 <find_default_interface+0x51>
lea    0xe53(%rip),%rdi
call   10a0 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1301 <find_default_interface+0x181>
jmp    11d6 <find_default_interface+0x56>
xor    %eax,%eax
cmpq   $0x0,-0x98(%rbp)
mov    %al,-0x99(%rbp)
jne    120e <find_default_interface+0x8e>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x99(%rbp)
mov    -0x99(%rbp),%al
test   $0x1,%al
jne    1221 <find_default_interface+0xa1>
jmp    12ba <find_default_interface+0x13a>
lea    -0x90(%rbp),%rdi
mov    $0x9,%esi
call   1060 <strchr@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    124c <find_default_interface+0xcc>
jmp    11d6 <find_default_interface+0x56>
mov    -0x98(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x98(%rbp)
movb   $0x0,(%rax)
mov    -0x98(%rbp),%rdi
lea    0xda6(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1299 <find_default_interface+0x119>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa8(%rbp)
jmp    12a7 <find_default_interface+0x127>
xor    %eax,%eax
mov    %rax,-0xa8(%rbp)
jmp    12a7 <find_default_interface+0x127>
mov    -0xa8(%rbp),%rax
mov    %rax,-0x98(%rbp)
jmp    11d6 <find_default_interface+0x56>
mov    -0x10(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     12ed <find_default_interface+0x16d>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
call   1080 <fprintf@plt>
movq   $0x0,-0x98(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x98(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_default_interface>:
push   %r15
push   %r14
push   %rbx
sub    $0x80,%rsp
lea    0xe6d(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1253 <find_default_interface+0xd3>
mov    %rax,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1215 <find_default_interface+0x95>
lea    0xe4e(%rip),%r15
jmp    11e5 <find_default_interface+0x65>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1215 <find_default_interface+0x95>
mov    %rbx,%rdi
mov    $0x9,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11d0 <find_default_interface+0x50>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
mov    %r15,%rsi
mov    $0x8,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11d0 <find_default_interface+0x50>
jmp    1217 <find_default_interface+0x97>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     123b <find_default_interface+0xbb>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1080 <fprintf@plt>
xor    %ebx,%ebx
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdb8(%rip),%rdi
call   10a0 <perror@plt>
xor    %ebx,%ebx
jmp    1243 <find_default_interface+0xc3>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [9 x i8] c"00000000\00"
@stderr = global i32 0
@.str.4 = [20 x i8] c"Error: read error.\0A\00"
define i8* @find_default_interface() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
store i8* null, i8** %4
%6 = load i32*, i32** %2
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %49
10: ; preds = %0
br label %11
11: ; preds = %36, %26, %10
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %19
14: ; preds = %11
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %2
%17 = call i32* @fgets(i8* %15, i32 128, i32* %16)
%18 = icmp ne i32* %17, null
br label %19
19: ; preds = %14, %11
%20 = phi i1 [ false, %11 ], [ %18, %14 ]
br i1 %20, label %21, label %38
21: ; preds = %19
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%23 = call i8* @strchr(i8* %22, i8 signext 9)
store i8* %23, i8** %4
%24 = load i8*, i8** %4
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %27
26: ; preds = %21
br label %11
27: ; preds = %21
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %4
store i8 0, i8* %28
%30 = load i8*, i8** %4
%31 = call i64 @strncmp(i8* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
br label %36
35: ; preds = %27
br label %36
36: ; preds = %35, %33
%37 = phi i8* [ %34, %33 ], [ null, %35 ]
store i8* %37, i8** %4
br label %11
38: ; preds = %19
%39 = load i32*, i32** %2
%40 = call i64 @ferror(i32* %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i8* null, i8** %4
br label %45
45: ; preds = %42, %38
%46 = load i32*, i32** %2
%47 = call i32 @fclose(i32* %46)
%48 = load i8*, i8** %4
store i8* %48, i8** %1
br label %49
49: ; preds = %45, %8
%50 = load i8*, i8** %1
ret i8* %50
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/net/route\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [9 x i8] c"00000000\00"
@stderr = global i32 0
@.str.4 = [20 x i8] c"Error: read error.\0A\00"
define i8* @find_default_interface() {
%1 = alloca [128 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = icmp eq i32* %2, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %3, i32 128, i32* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %20, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %30
10: ; preds = %5, %13
%11 = call i8* @strchr(i8* %3, i8 signext 9)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %16, %10
%14 = call i32* @fgets(i8* %3, i32 128, i32* %2)
%15 = icmp eq i32* %14, null
br i1 %15, label %20, label %10
16: ; preds = %10
%17 = getelementptr inbounds i8, i8* %11, i64 1
store i8 0, i8* %11
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 8)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %13
20: ; preds = %13, %16, %5
%21 = phi i8* [ null, %5 ], [ null, %13 ], [ %3, %16 ]
%22 = call i64 @ferror(i32* %2)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %24, %20
%28 = phi i8* [ null, %24 ], [ %21, %20 ]
%29 = call i32 @fclose(i32* %2)
br label %30
30: ; preds = %27, %8
%31 = phi i8* [ null, %8 ], [ %28, %27 ]
ret i8* %31
}