#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__ chdir(char *);
int /*<<< orphan*/ ep0_thread(int *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *);
int init_device(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setPluginDir(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd;
  char feed[80];
  FILE *config;
  config = fopen("PURE.cfg", "rt");
  if (config != NULL) {
    if (fscanf(config, "PLUGIN_DIR=%s", feed) == 1) {
      printf("Configuration loaded successfully.\n");
      setPluginDir(feed);
    }
  } else {
    printf("Failed to find configuration file; default values set.\n");
    setPluginDir("./plugins/");
  }
  if (chdir("/dev/gadget") < 0) {
    perror("can't chdir /dev/gadget");
    return 1;
  }
  if (argc != 2) {
    perror("Please specify a plugin to load.\n");
    exit(1);
  }
  fd = init_device(argv[1]);
  if (fd < 0)
    return 1;
  fprintf(stderr, "/dev/gadget/%s ep0 configured\n", "musb-hdrc");
  fflush(stderr);
  (void)ep0_thread(&fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
lea    0xe32(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x78(%rbp)
cmpq   $0x0,-0x78(%rbp)
je     1222 <main+0x72>
mov    -0x78(%rbp),%rdi
lea    -0x70(%rbp),%rdx
lea    0xe12(%rip),%rsi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    121d <main+0x6d>
lea    0xe0b(%rip),%rdi
call   1050 <printf@plt>
lea    -0x70(%rbp),%rdi
call   10a0 <setPluginDir@plt>
jmp    123a <main+0x8a>
lea    0xe15(%rip),%rdi
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
call   10a0 <setPluginDir@plt>
lea    0xe4c(%rip),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
jge    1268 <main+0xb8>
lea    0xe2a(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e8 <main+0x138>
cmpl   $0x2,-0x8(%rbp)
je     1288 <main+0xd8>
lea    0xe20(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <init_device@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12ae <main+0xfe>
movl   $0x1,-0x4(%rbp)
jmp    12e8 <main+0x138>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
lea    0xe15(%rip),%rdx
call   1080 <fprintf@plt>
mov    0x2d0f(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
lea    -0x14(%rbp),%rdi
call   1070 <ep0_thread@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe3e(%rip),%rdi
lea    0xe40(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     11ff <main+0x4f>
lea    0xe32(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
cmp    $0x1,%eax
jne    1217 <main+0x67>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rdi
jmp    1212 <main+0x62>
lea    0xe82(%rip),%rdi
call   1030 <puts@plt>
lea    0xe08(%rip),%rdi
call   10a0 <setPluginDir@plt>
lea    0xe13(%rip),%rdi
call   1050 <chdir@plt>
test   %rax,%rax
js     126f <main+0xbf>
cmp    $0x2,%ebp
jne    1287 <main+0xd7>
mov    0x8(%rbx),%rdi
call   1040 <init_device@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     127b <main+0xcb>
mov    0x2d9b(%rip),%rbx
mov    (%rbx),%edi
lea    0xe11(%rip),%rsi
lea    0xe29(%rip),%rdx
call   1080 <fprintf@plt>
mov    (%rbx),%edi
call   1090 <fflush@plt>
lea    0xc(%rsp),%rdi
call   1070 <ep0_thread@plt>
xor    %eax,%eax
jmp    1280 <main+0xd0>
lea    0xdaf(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdaf(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"PURE.cfg\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [14 x i8] c"PLUGIN_DIR=%s\00"
@.str.3 = [36 x i8] c"Configuration loaded successfully.\0A\00"
@.str.4 = [56 x i8] c"Failed to find configuration file; default values set.\0A\00"
@.str.5 = [11 x i8] c"./plugins/\00"
@.str.6 = [12 x i8] c"/dev/gadget\00"
@.str.7 = [24 x i8] c"can't chdir /dev/gadget\00"
@.str.8 = [34 x i8] c"Please specify a plugin to load.\0A\00"
@stderr = global i32 0
@.str.9 = [31 x i8] c"/dev/gadget/%s ep0 configured\0A\00"
@.str.10 = [10 x i8] c"musb-hdrc\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [80 x i8]
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %8
%10 = load i32*, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %22
12: ; preds = %2
%13 = load i32*, i32** %8
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%15 = call i32 @fscanf(i32* %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%20 = call i32 @setPluginDir(i8* %19)
br label %21
21: ; preds = %17, %12
br label %25
22: ; preds = %2
%23 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @setPluginDir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %25
25: ; preds = %22, %21
%26 = call i64 @chdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %50
30: ; preds = %25
%31 = load i32, i32* %4
%32 = icmp ne i32 %31, 2
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = call i32 @init_device(i8* %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %36
store i32 1, i32* %3
br label %50
44: ; preds = %36
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0))
%47 = load i32, i32* @stderr
%48 = call i32 @fflush(i32 %47)
%49 = call i32 @ep0_thread(i32* %6)
store i32 0, i32* %3
br label %50
50: ; preds = %44, %43, %28
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [9 x i8] c"PURE.cfg\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [14 x i8] c"PLUGIN_DIR=%s\00"
@.str.5 = [11 x i8] c"./plugins/\00"
@.str.6 = [12 x i8] c"/dev/gadget\00"
@.str.7 = [24 x i8] c"can't chdir /dev/gadget\00"
@.str.8 = [34 x i8] c"Please specify a plugin to load.\0A\00"
@stderr = global i32 0
@.str.9 = [31 x i8] c"/dev/gadget/%s ep0 configured\0A\00"
@.str.10 = [10 x i8] c"musb-hdrc\00"
@str = [55 x i8] c"Failed to find configuration file; default values set.\00"
@str.11 = [35 x i8] c"Configuration loaded successfully.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [80 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.11, i64 0, i64 0))
%14 = call i32 @setPluginDir(i8* %6)
br label %18
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @setPluginDir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %18
18: ; preds = %9, %12, %15
%19 = call i64 @chdir(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %39
23: ; preds = %18
%24 = icmp eq i32 %0, 2
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %23
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @init_device(i8* %30)
store i32 %31, i32* %3
%32 = icmp slt i32 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = call i32 @fflush(i32 %36)
%38 = call i32 @ep0_thread(i32* %3)
br label %39
39: ; preds = %28, %33, %21
%40 = phi i32 [ 1, %21 ], [ 0, %33 ], [ 1, %28 ]
ret i32 %40
}