#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*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strip_line(char *);
FILE *open_file(char *filename) {
  FILE *f;
  char buf[1024];
  if ((f = fopen(filename, "r")) == NULL) {
    printf("failed to open file %s\n", filename);
    return NULL;
  }
  if (fgets(buf, 1024, f) == NULL) {
    fclose(f);
    return NULL;
  }
  strip_line(buf);
  if (!strcmp(buf, "version=1")) {
    printf("skipping %s, already converted\n", filename);
    fclose(f);
    return NULL;
  }
  rewind(f);
  return f;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11ba <open_file+0x4a>
mov    -0x10(%rbp),%rsi
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    124e <open_file+0xde>
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11ef <open_file+0x7f>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    124e <open_file+0xde>
lea    -0x420(%rbp),%rdi
call   1080 <strip_line@plt>
lea    -0x420(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    123d <open_file+0xcd>
mov    -0x10(%rbp),%rsi
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    124e <open_file+0xde>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%r14
lea    0xe8c(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11c4 <open_file+0x64>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11e4 <open_file+0x84>
mov    %rsp,%rdi
call   1070 <strip_line@plt>
movabs $0x3d6e6f6973726576,%rax
6f 6e 3d
xor    (%rsp),%rax
movzwl 0x8(%rsp),%ecx
xor    $0x31,%rcx
or     %rax,%rcx
je     11d5 <open_file+0x75>
mov    %rbx,%rdi
call   1050 <rewind@plt>
jmp    11ee <open_file+0x8e>
lea    0xe37(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    11ec <open_file+0x8c>
lea    0xe48(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"failed to open file %s\0A\00"
@.str.2 = [10 x i8] c"version=1\00"
@.str.3 = [32 x i8] c"skipping %s, already converted\0A\00"
define i32* @open_file(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %10)
store i32* null, i32** %2
br label %35
12: ; preds = %1
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%14 = load i32*, i32** %4
%15 = call i32* @fgets(i8* %13, i32 1024, i32* %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32*, i32** %4
%19 = call i32 @fclose(i32* %18)
store i32* null, i32** %2
br label %35
20: ; preds = %12
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = call i32 @strip_line(i8* %21)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%24 = call i32 @strcmp(i8* %23, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp ne i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
store i32* null, i32** %2
br label %35
31: ; preds = %20
%32 = load i32*, i32** %4
%33 = call i32 @rewind(i32* %32)
%34 = load i32*, i32** %4
store i32* %34, i32** %2
br label %35
35: ; preds = %31, %26, %17, %9
%36 = load i32*, i32** %2
ret i32* %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"failed to open file %s\0A\00"
@.str.2 = [10 x i8] c"version=1\00"
@.str.3 = [32 x i8] c"skipping %s, already converted\0A\00"
define noalias i32* @open_file(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
8: ; preds = %1
%9 = call i32* @fgets(i8* %3, i32 1024, i32* %4)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = call i32 @fclose(i32* %4)
br label %22
13: ; preds = %8
%14 = call i32 @strip_line(i8* %3)
%15 = call i32 @bcmp(i8* dereferenceable(10) %3, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i64 10)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%19 = call i32 @fclose(i32* %4)
br label %22
20: ; preds = %13
%21 = call i32 @rewind(i32* %4)
br label %22
22: ; preds = %20, %17, %11, %6
%23 = phi i32* [ null, %6 ], [ null, %11 ], [ %4, %20 ], [ null, %17 ]
ret i32* %23
}