#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 filesystem {
  int dummy;
};

/* Variables and functions */
struct filesystem *fsopen(char *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ storesetting(char *, char *);
int main(int argc, char *argv[]) {
  char *filename;
  struct filesystem *f;
  char fullpath[1024];
  char cwd[800];
  if (argc != 2) {
    printf("Usage: %s <filename>\n", argv[0]);
    return 10;
  }
  filename = argv[1];
  f = fsopen(filename);
  if (!f) {
    printf("Unable to open %s\n", filename);
    return 10;
  }
  if (filename[0] == '/') {
    storesetting("imagefile", filename);
  } else {
    getcwd(cwd, 800);
    sprintf(fullpath, "%s/%s", cwd, filename);
    storesetting("imagefile", fullpath);
  }
  storesetting("cwd", "/");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x740,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1192 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe7f(%rip),%rdi
call   1040 <printf@plt>
movl   $0xa,-0x4(%rbp)
jmp    1253 <main+0x103>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fsopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d2 <main+0x82>
mov    -0x18(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1040 <printf@plt>
movl   $0xa,-0x4(%rbp)
jmp    1253 <main+0x103>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11f7 <main+0xa7>
mov    -0x18(%rbp),%rsi
lea    0xe3c(%rip),%rdi
call   1060 <storesetting@plt>
jmp    1239 <main+0xe9>
lea    -0x740(%rbp),%rdi
mov    $0x320,%esi
call   1050 <getcwd@plt>
lea    -0x420(%rbp),%rdi
lea    -0x740(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe12(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x420(%rbp),%rsi
lea    0xdf5(%rip),%rdi
call   1060 <storesetting@plt>
lea    0xdf9(%rip),%rdi
lea    0xdf6(%rip),%rsi
call   1060 <storesetting@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x740,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x720,%rsp
cmp    $0x2,%edi
jne    1183 <main+0x33>
mov    0x8(%rsi),%rbx
mov    %rbx,%rdi
call   1030 <fsopen@plt>
test   %rax,%rax
je     118f <main+0x3f>
cmpb   $0x2f,(%rbx)
jne    11a5 <main+0x55>
lea    0xeab(%rip),%rdi
mov    %rbx,%rsi
jmp    11dc <main+0x8c>
mov    (%rsi),%rsi
lea    0xe73(%rip),%rdi
jmp    1199 <main+0x49>
lea    0xe80(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    $0xa,%eax
jmp    11f6 <main+0xa6>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x320,%esi
call   1050 <getcwd@plt>
lea    0xe77(%rip),%rsi
lea    0x320(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1070 <sprintf@plt>
lea    0xe50(%rip),%rdi
mov    %r15,%rsi
call   1060 <storesetting@plt>
lea    0xe51(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1060 <storesetting@plt>
xor    %eax,%eax
add    $0x720,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.filesystem = type { i32 }
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [10 x i8] c"imagefile\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [4 x i8] c"cwd\00"
@.str.5 = [2 x i8] c"/\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca %struct.filesystem*
%8 = alloca [1024 x i8]
%9 = alloca [800 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 10, i32* %3
br label %48
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = call %struct.filesystem* @fsopen(i8* %21)
store %struct.filesystem* %22, %struct.filesystem** %7
%23 = load %struct.filesystem*, %struct.filesystem** %7
%24 = icmp ne %struct.filesystem* %23, null
br i1 %24, label %28, label %25
25: ; preds = %17
%26 = load i8*, i8** %6
%27 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 10, i32* %3
br label %48
28: ; preds = %17
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 47
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i8*, i8** %6
%36 = call i32 @storesetting(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %46
37: ; preds = %28
%38 = getelementptr inbounds [800 x i8], [800 x i8]* %9, i64 0, i64 0
%39 = call i32 @getcwd(i8* %38, i32 800)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%41 = getelementptr inbounds [800 x i8], [800 x i8]* %9, i64 0, i64 0
%42 = load i8*, i8** %6
%43 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %41, i8* %42)
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%45 = call i32 @storesetting(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %37, %34
%47 = call i32 @storesetting(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %3
br label %48
48: ; preds = %46, %25, %12
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.filesystem = type { i32 }
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@.str.1 = [19 x i8] c"Unable to open %s\0A\00"
@.str.2 = [10 x i8] c"imagefile\00"
@.str.3 = [6 x i8] c"%s/%s\00"
@.str.4 = [4 x i8] c"cwd\00"
@.str.5 = [2 x i8] c"/\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [800 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [800 x i8], [800 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %29
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call %struct.filesystem* @fsopen(i8* %13)
%15 = icmp eq %struct.filesystem* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %29
18: ; preds = %11
%19 = load i8, i8* %13
%20 = icmp eq i8 %19, 47
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @storesetting(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %27
23: ; preds = %18
%24 = call i32 @getcwd(i8* %6, i32 800)
%25 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %6, i8* %13)
%26 = call i32 @storesetting(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %27
27: ; preds = %23, %21
%28 = call i32 @storesetting(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %16, %8
%30 = phi i32 [ 10, %8 ], [ 0, %27 ], [ 10, %16 ]
ret i32 %30
}