#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 */
scalar_t__ asprintf(char **, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int get_game_data(char *, char *, int);
char *ltr_int_get_default_file_name(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ system(char *);
bool update_gamedata() {
  char *tgt_data = ltr_int_get_default_file_name("tir_firmware/gamedata.txt");
  FILE *test = fopen(tgt_data, "r");
  if (test == NULL) {
    free(tgt_data);
    printf("Extract the data first, update afterwards.\n");
    return false;
  }
  fclose(test);
  char *src_data = ltr_int_get_default_file_name("tir_firmware/sgl.dat0");
  char *cmd = "wget --user-agent=\"NaturalPoint TrackIR 5.2.200\" -O \"%s\" "
              "\"http://www.naturalpoint.com/update/files/"
              "getfile.cgi?file_id=SGL&category=0\"";
  char *full_cmd;
  if (asprintf(&full_cmd, cmd, src_data) < 0) {
    free(src_data);
    printf("Couldn't create command to download the gamedata update.\n");
    return false;
  }
  printf("Going to execute '%s'.\n", full_cmd);
  if (system(full_cmd) != 0) {
    free(full_cmd);
    free(src_data);
    printf("Couldn't download the gamedata update.\n");
    return false;
  }
  free(full_cmd);
  bool res = false;
  if ((res = get_game_data(src_data, tgt_data, true))) {
    printf("Game data updated successfully.\n");
  } else {
    printf("Problem updating game data.\n");
  }
  free(src_data);
  free(tgt_data);
  return res;
}


x86 assembly (clang, -O0):
<update_gamedata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe71(%rip),%rdi
call   1040 <ltr_int_get_default_file_name@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11da <update_gamedata+0x5a>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f4 <update_gamedata+0x174>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xe5e(%rip),%rdi
call   1040 <ltr_int_get_default_file_name@plt>
mov    %rax,-0x20(%rbp)
lea    0xe64(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
call   1090 <asprintf@plt>
cmp    $0x0,%rax
jge    123c <update_gamedata+0xbc>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f4 <update_gamedata+0x174>
mov    -0x30(%rbp),%rsi
lea    0xed8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <system@plt>
cmp    $0x0,%rax
je     128d <update_gamedata+0x10d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
lea    0xebd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f4 <update_gamedata+0x174>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1080 <get_game_data@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     12ce <update_gamedata+0x14e>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12dc <update_gamedata+0x15c>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_gamedata>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xe62(%rip),%rdi
call   1040 <ltr_int_get_default_file_name@plt>
mov    %rax,%r15
lea    0xe6d(%rip),%rsi
mov    %rax,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122c <update_gamedata+0x9c>
mov    %rax,%rdi
call   1060 <fclose@plt>
lea    0xe53(%rip),%rdi
call   1040 <ltr_int_get_default_file_name@plt>
mov    %rax,%r14
lea    0xe5a(%rip),%rsi
mov    %rsp,%rdi
mov    %rax,%rdx
call   10a0 <asprintf@plt>
test   %rax,%rax
js     123d <update_gamedata+0xad>
mov    (%rsp),%rsi
lea    0xec6(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <printf@plt>
mov    (%rsp),%rdi
call   1070 <system@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   1030 <free@plt>
test   %rbx,%rbx
je     1255 <update_gamedata+0xc5>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xee8(%rip),%rdi
call   1050 <puts@plt>
jmp    1290 <update_gamedata+0x100>
mov    %r15,%rdi
call   1030 <free@plt>
lea    0xf32(%rip),%rdi
jmp    124c <update_gamedata+0xbc>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xee8(%rip),%rdi
call   1050 <puts@plt>
xor    %ebp,%ebp
jmp    1290 <update_gamedata+0x100>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1090 <get_game_data@plt>
mov    %eax,%ebp
test   %eax,%eax
lea    0xe61(%rip),%rax
lea    0xe76(%rip),%rdi
cmove  %rax,%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"tir_firmware/gamedata.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [44 x i8] c"Extract the data first, update afterwards.\0A\00"
@.str.3 = [22 x i8] c"tir_firmware/sgl.dat0\00"
@.str.4 = [135 x i8] c"wget --user-agent=\22NaturalPoint TrackIR 5.2.200\22 -O \22%s\22 \22http://www.naturalpoint.com/update/files/getfile.cgi?file_id=SGL&category=0\22\00"
@.str.5 = [58 x i8] c"Couldn't create command to download the gamedata update.\0A\00"
@.str.6 = [24 x i8] c"Going to execute '%s'.\0A\00"
@.str.7 = [40 x i8] c"Couldn't download the gamedata update.\0A\00"
@.str.8 = [33 x i8] c"Game data updated successfully.\0A\00"
@.str.9 = [29 x i8] c"Problem updating game data.\0A\00"
define i32 @update_gamedata() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = call i8* @ltr_int_get_default_file_name(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %3
%11 = load i32*, i32** %3
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = load i8*, i8** %2
%15 = call i32 @free(i8* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %58
17: ; preds = %0
%18 = load i32*, i32** %3
%19 = call i32 @fclose(i32* %18)
%20 = call i8* @ltr_int_get_default_file_name(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i8* %20, i8** %4
store i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i8** %5
%21 = load i8*, i8** %5
%22 = load i8*, i8** %4
%23 = call i64 @asprintf(i8** %6, i8* %21, i8* %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %58
29: ; preds = %17
%30 = load i8*, i8** %6
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %6
%33 = call i64 @system(i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
%38 = load i8*, i8** %4
%39 = call i32 @free(i8* %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %1
br label %58
41: ; preds = %29
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
store i32 0, i32* %7
%44 = load i8*, i8** %4
%45 = load i8*, i8** %2
%46 = call i32 @get_game_data(i8* %44, i8* %45, i32 1)
store i32 %46, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0))
br label %52
50: ; preds = %41
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
br label %52
52: ; preds = %50, %48
%53 = load i8*, i8** %4
%54 = call i32 @free(i8* %53)
%55 = load i8*, i8** %2
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %7
store i32 %57, i32* %1
br label %58
58: ; preds = %52, %35, %25, %13
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@.str = [26 x i8] c"tir_firmware/gamedata.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [22 x i8] c"tir_firmware/sgl.dat0\00"
@.str.4 = [135 x i8] c"wget --user-agent=\22NaturalPoint TrackIR 5.2.200\22 -O \22%s\22 \22http://www.naturalpoint.com/update/files/getfile.cgi?file_id=SGL&category=0\22\00"
@.str.6 = [24 x i8] c"Going to execute '%s'.\0A\00"
@str = [28 x i8] c"Problem updating game data.\00"
@str.10 = [32 x i8] c"Game data updated successfully.\00"
@str.11 = [39 x i8] c"Couldn't download the gamedata update.\00"
@str.12 = [57 x i8] c"Couldn't create command to download the gamedata update.\00"
@str.13 = [43 x i8] c"Extract the data first, update afterwards.\00"
define i32 @update_gamedata() {
%1 = alloca i8*
%2 = tail call i8* @ltr_int_get_default_file_name(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @free(i8* %2)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.13, i64 0, i64 0))
br label %37
8: ; preds = %0
%9 = tail call i32 @fclose(i32* %3)
%10 = tail call i8* @ltr_int_get_default_file_name(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%11 = bitcast i8** %1 to i8*
%12 = call i64 @asprintf(i8** %1, i8* getelementptr inbounds ([135 x i8], [135 x i8]* @.str.4, i64 0, i64 0), i8* %10)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = call i32 @free(i8* %10)
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.12, i64 0, i64 0))
br label %35
17: ; preds = %8
%18 = load i8*, i8** %1
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %18)
%20 = load i8*, i8** %1
%21 = call i64 @system(i8* %20)
%22 = icmp eq i64 %21, 0
%23 = load i8*, i8** %1
%24 = call i32 @free(i8* %23)
br i1 %22, label %28, label %25
25: ; preds = %17
%26 = call i32 @free(i8* %10)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.11, i64 0, i64 0))
br label %35
28: ; preds = %17
%29 = call i32 @get_game_data(i8* %10, i8* %2, i32 1)
%30 = icmp eq i32 %29, 0
%31 = select i1 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @str.10, i64 0, i64 0)
%32 = call i32 @puts(i8* dereferenceable(1) %31)
%33 = call i32 @free(i8* %10)
%34 = call i32 @free(i8* %2)
br label %35
35: ; preds = %28, %25, %14
%36 = phi i32 [ 0, %14 ], [ 0, %25 ], [ %29, %28 ]
br label %37
37: ; preds = %35, %5
%38 = phi i32 [ 0, %5 ], [ %36, %35 ]
ret i32 %38
}