#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 timespec {
  scalar_t__ tv_nsec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ LOG_DEBUG;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ PTHREAD_COND_INITIALIZER;
int /*<<< orphan*/ PTHREAD_MUTEX_INITIALIZER;
int /*<<< orphan*/ _debug(char *, int, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ pthread_cond_timedwait(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/ *,
                                          struct timespec *);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
scalar_t__ time(int /*<<< orphan*/ *);
char *get_ext_iface(void) {
  FILE *input;
  char *device, *gw;
  int i = 1;
  int keep_detecting = 1;
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
  struct timespec timeout;
  device = (char *)malloc(16);
  gw = (char *)malloc(16);
  _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
         "2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/"
         "util.c",
         232, LOG_DEBUG,
         "get_ext_iface(): Autodectecting the external interface from routing "
         "table");
  while (keep_detecting) {
    input = fopen("/proc/net/route", "r");
    while (!feof(input)) {
      fscanf(input, "%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\n", device, gw);
      if (strcmp(gw, "00000000") == 0) {
        free(gw);
        _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
               "outputs/2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/"
               "heads/master/src/util.c",
               239, LOG_INFO,
               "get_ext_iface(): Detected %s as the default interface after "
               "try %d",
               device, i);
        return device;
      }
    }
    fclose(input);
    _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
           "2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/"
           "util.c",
           244, LOG_ERR,
           "get_ext_iface(): Failed to detect the external interface after try "
           "%d (maybe the interface is not up yet?).  Retry limit: %d",
           i, 0);
    timeout.tv_sec = time(NULL) + 1;
    timeout.tv_nsec = 0;
    pthread_mutex_lock(&cond_mutex);
    pthread_cond_timedwait(&cond, &cond_mutex, &timeout);
    pthread_mutex_unlock(&cond_mutex);
    if (0 != 0 && i > 0) {
      keep_detecting = 0;
    }
    i++;
  }
  _debug("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
         "2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/"
         "util.c",
         260, LOG_ERR,
         "get_ext_iface(): Failed to detect the external interface after %d "
         "tries, aborting",
         i);
  exit(1);
  free(device);
  free(gw);
  return NULL;
}


x86 assembly (clang, -O0):
<get_ext_iface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x1,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x10,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x10,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
lea    0xdd6(%rip),%rdi
mov    $0xe8,%esi
lea    0xe54(%rip),%rcx
mov    $0x0,%al
call   1080 <_debug@plt>
cmpl   $0x0,-0x20(%rbp)
je     136e <get_ext_iface+0x19e>
lea    0xe86(%rip),%rdi
lea    0xe8f(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    127c <get_ext_iface+0xac>
jmp    12f1 <get_ext_iface+0x121>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe57(%rip),%rsi
call   1050 <fscanf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12ec <get_ext_iface+0x11c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%r8
mov    -0x1c(%rbp),%r9d
lea    0xd31(%rip),%rdi
mov    $0xef,%esi
lea    0xe3f(%rip),%rcx
mov    $0x0,%al
call   1080 <_debug@plt>
mov    -0x10(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret
jmp    125e <get_ext_iface+0x8e>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edx
mov    -0x1c(%rbp),%r8d
lea    0xcf2(%rip),%rdi
mov    $0xf4,%esi
lea    0xe43(%rip),%rcx
xor    %r9d,%r9d
mov    $0x0,%al
call   1080 <_debug@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x28(%rbp),%rdi
call   10f0 <pthread_mutex_lock@plt>
lea    -0x24(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   10c0 <pthread_cond_timedwait@plt>
lea    -0x28(%rbp),%rdi
call   10a0 <pthread_mutex_unlock@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    123d <get_ext_iface+0x6d>
mov    0x2c43(%rip),%rax
mov    (%rax),%edx
mov    -0x1c(%rbp),%r8d
lea    0xc7e(%rip),%rdi
mov    $0x104,%esi
lea    0xe4c(%rip),%rcx
mov    $0x0,%al
call   1080 <_debug@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

x86 assembly (clang, -O2):
<get_ext_iface>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    $0x10,%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
mov    $0x10,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
lea    0xdef(%rip),%rdi
lea    0xe72(%rip),%rcx
mov    $0xe8,%esi
xor    %eax,%eax
call   1080 <_debug@plt>
mov    $0x1,%ebp
lea    0xea4(%rip),%rdi
lea    0xead(%rip),%rsi
lea    0xea8(%rip),%r15
lea    0xecc(%rip),%r12
call   10d0 <fopen@plt>
mov    %rax,%r13
nopl   (%rax)
mov    %r13,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1290 <get_ext_iface+0xd0>
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1050 <fscanf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1250 <get_ext_iface+0x90>
jmp    131b <get_ext_iface+0x15b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
lea    0xd58(%rip),%rdi
mov    $0xf4,%esi
lea    0xea9(%rip),%rcx
mov    %ebp,%r8d
xor    %r9d,%r9d
xor    %eax,%eax
call   1080 <_debug@plt>
xor    %edi,%edi
call   1090 <time@plt>
add    $0x1,%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %ebp,%r13d
mov    %rsp,%rbp
mov    %rbp,%rdi
call   10e0 <pthread_mutex_lock@plt>
lea    0x4(%rsp),%rdi
mov    %rbp,%rsi
lea    0x8(%rsp),%rdx
call   10c0 <pthread_cond_timedwait@plt>
mov    %rbp,%rdi
mov    %r13d,%ebp
call   10a0 <pthread_mutex_unlock@plt>
add    $0x1,%ebp
lea    0xdc5(%rip),%rdi
lea    0xdce(%rip),%rsi
jmp    1245 <get_ext_iface+0x85>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2c9e(%rip),%rax
mov    (%rax),%edx
lea    0xccd(%rip),%rdi
lea    0xde0(%rip),%rcx
mov    $0xef,%esi
mov    %r14,%r8
mov    %ebp,%r9d
xor    %eax,%eax
call   1080 <_debug@plt>
mov    %r14,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i64 }
@PTHREAD_COND_INITIALIZER = global i32 0
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [138 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/util.c\00"
@LOG_DEBUG = global i32 0
@.str.1 = [74 x i8] c"get_ext_iface(): Autodectecting the external interface from routing table\00"
@.str.2 = [16 x i8] c"/proc/net/route\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [43 x i8] c"%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\0A\00"
@.str.5 = [9 x i8] c"00000000\00"
@LOG_INFO = global i32 0
@.str.6 = [67 x i8] c"get_ext_iface(): Detected %s as the default interface after try %d\00"
@LOG_ERR = global i32 0
@.str.7 = [125 x i8] c"get_ext_iface(): Failed to detect the external interface after try %d (maybe the interface is not up yet?). Retry limit: %d\00"
@.str.8 = [82 x i8] c"get_ext_iface(): Failed to detect the external interface after %d tries, aborting\00"
define i8* @get_ext_iface() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timespec
store i32 1, i32* %4
store i32 1, i32* %5
%9 = load i32, i32* @PTHREAD_COND_INITIALIZER
store i32 %9, i32* %6
%10 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %10, i32* %7
%11 = call i64 @malloc(i32 16)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %2
%13 = call i64 @malloc(i32 16)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %3
%15 = load i32, i32* @LOG_DEBUG
%16 = call i32 (i8*, i32, i32, i8*, ...) @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 232, i32 %15, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %44, %0
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %59
20: ; preds = %17
%21 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %21, i32** %1
br label %22
22: ; preds = %43, %20
%23 = load i32*, i32** %1
%24 = call i32 @feof(i32* %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %44
27: ; preds = %22
%28 = load i32*, i32** %1
%29 = load i8*, i8** %2
%30 = load i8*, i8** %3
%31 = call i32 @fscanf(i32* %28, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %29, i8* %30)
%32 = load i8*, i8** %3
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %27
%36 = load i8*, i8** %3
%37 = call i32 @free(i8* %36)
%38 = load i32, i32* @LOG_INFO
%39 = load i8*, i8** %2
%40 = load i32, i32* %4
%41 = call i32 (i8*, i32, i32, i8*, ...) @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 239, i32 %38, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.6, i64 0, i64 0), i8* %39, i32 %40)
%42 = load i8*, i8** %2
ret i8* %42
43: ; preds = %27
br label %22
44: ; preds = %22
%45 = load i32*, i32** %1
%46 = call i32 @fclose(i32* %45)
%47 = load i32, i32* @LOG_ERR
%48 = load i32, i32* %4
%49 = call i32 (i8*, i32, i32, i8*, ...) @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 244, i32 %47, i8* getelementptr inbounds ([125 x i8], [125 x i8]* @.str.7, i64 0, i64 0), i32 %48, i32 0)
%50 = call i64 @time(i32* null)
%51 = add nsw i64 %50, 1
%52 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 1
store i64 %51, i64* %52
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %8, i32 0, i32 0
store i64 0, i64* %53
%54 = call i32 @pthread_mutex_lock(i32* %7)
%55 = call i32 @pthread_cond_timedwait(i32* %6, i32* %7, %struct.timespec* %8)
%56 = call i32 @pthread_mutex_unlock(i32* %7)
%57 = load i32, i32* %4
%58 = add nsw i32 %57, 1
store i32 %58, i32* %4
br label %17
59: ; preds = %17
%60 = load i32, i32* @LOG_ERR
%61 = load i32, i32* %4
%62 = call i32 (i8*, i32, i32, i8*, ...) @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 260, i32 %60, i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.8, i64 0, i64 0), i32 %61)
%63 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.timespec = type { i64, i64 }
@PTHREAD_COND_INITIALIZER = global i32 0
@PTHREAD_MUTEX_INITIALIZER = global i32 0
@.str = [138 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/sxpert/wifidog-ffw/refs/heads/master/src/util.c\00"
@LOG_DEBUG = global i32 0
@.str.1 = [74 x i8] c"get_ext_iface(): Autodectecting the external interface from routing table\00"
@.str.2 = [16 x i8] c"/proc/net/route\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [43 x i8] c"%s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s\0A\00"
@.str.5 = [9 x i8] c"00000000\00"
@LOG_INFO = global i32 0
@.str.6 = [67 x i8] c"get_ext_iface(): Detected %s as the default interface after try %d\00"
@LOG_ERR = global i32 0
@.str.7 = [125 x i8] c"get_ext_iface(): Failed to detect the external interface after try %d (maybe the interface is not up yet?). Retry limit: %d\00"
define i8* @get_ext_iface() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = bitcast i32* %1 to i8*
%5 = load i32, i32* @PTHREAD_COND_INITIALIZER
store i32 %5, i32* %1
%6 = bitcast i32* %2 to i8*
%7 = load i32, i32* @PTHREAD_MUTEX_INITIALIZER
store i32 %7, i32* %2
%8 = bitcast %struct.timespec* %3 to i8*
%9 = tail call i64 @malloc(i32 16)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i64 @malloc(i32 16)
%12 = inttoptr i64 %11 to i8*
%13 = load i32, i32* @LOG_DEBUG
%14 = tail call i32 (i8*, i32, i32, i8*, ...) @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 232, i32 %13, i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0))
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 1
%16 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
br label %17
17: ; preds = %31, %0
%18 = phi i32 [ 1, %0 ], [ %40, %31 ]
%19 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %23, %17
%21 = call i32 @feof(i32* %19)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = call i32 @fscanf(i32* %19, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %10, i8* %12)
%25 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %20
27: ; preds = %23
%28 = call i32 @free(i8* %12)
%29 = load i32, i32* @LOG_INFO
%30 = call i32 (i8*, i32, i32, i8*, ...) @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 239, i32 %29, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.6, i64 0, i64 0), i8* %10, i32 %18)
ret i8* %10
31: ; preds = %20
%32 = call i32 @fclose(i32* %19)
%33 = load i32, i32* @LOG_ERR
%34 = call i32 (i8*, i32, i32, i8*, ...) @_debug(i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str, i64 0, i64 0), i32 244, i32 %33, i8* getelementptr inbounds ([125 x i8], [125 x i8]* @.str.7, i64 0, i64 0), i32 %18, i32 0)
%35 = call i64 @time(i32* null)
%36 = add nsw i64 %35, 1
store i64 %36, i64* %15
store i64 0, i64* %16
%37 = call i32 @pthread_mutex_lock(i32* %2)
%38 = call i32 @pthread_cond_timedwait(i32* %1, i32* %2, %struct.timespec* %3)
%39 = call i32 @pthread_mutex_unlock(i32* %2)
%40 = add nuw nsw i32 %18, 1
br label %17
}