#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*/ EXIT_FAILURE;
int /*<<< orphan*/ cardfaces(char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
void getcard(char *dbfile, int cardnum, char *cardfr, char *cardbk) {
  char card[1500];
  FILE *fp;
  int i;
  if (!(fp = fopen(dbfile, "r"))) {
    printf("Fail to open database %s.\n", dbfile);
    exit(EXIT_FAILURE);
  }
  for (i = 0; i <= cardnum; i++)
    fgets(card, 1500, fp);
  fclose(fp);
  cardfaces(card, cardfr, cardbk);
  return;
}


x86 assembly (clang, -O0):
<getcard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x610,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x608(%rbp)
cmp    $0x0,%rax
jne    11b9 <getcard+0x59>
mov    -0x8(%rbp),%rsi
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x0,-0x60c(%rbp)
mov    -0x60c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jg     11fe <getcard+0x9e>
lea    -0x600(%rbp),%rdi
mov    -0x608(%rbp),%rdx
mov    $0x5dc,%esi
call   1050 <fgets@plt>
mov    -0x60c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x60c(%rbp)
jmp    11c3 <getcard+0x63>
mov    -0x608(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x600(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <cardfaces@plt>
add    $0x610,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcard>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x5e0,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r12
lea    0xe7f(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11db <getcard+0x7b>
mov    %rax,%rbx
test   %ebp,%ebp
js     11b5 <getcard+0x55>
add    $0x1,%ebp
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x5dc,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
add    $0xffffffff,%ebp
jne    11a0 <getcard+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rsp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1080 <cardfaces@plt>
add    $0x5e0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe20(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Fail to open database %s.\0A\00"
@EXIT_FAILURE = global i32 0
define void @getcard(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca [1500 x i8]
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %10
%14 = icmp ne i32* %13, null
br i1 %14, label %20, label %15
15: ; preds = %4
%16 = load i8*, i8** %5
%17 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %4
store i32 0, i32* %11
br label %21
21: ; preds = %29, %20
%22 = load i32, i32* %11
%23 = load i32, i32* %6
%24 = icmp sle i32 %22, %23
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = getelementptr inbounds [1500 x i8], [1500 x i8]* %9, i64 0, i64 0
%27 = load i32*, i32** %10
%28 = call i32 @fgets(i8* %26, i32 1500, i32* %27)
br label %29
29: ; preds = %25
%30 = load i32, i32* %11
%31 = add nsw i32 %30, 1
store i32 %31, i32* %11
br label %21
32: ; preds = %21
%33 = load i32*, i32** %10
%34 = call i32 @fclose(i32* %33)
%35 = getelementptr inbounds [1500 x i8], [1500 x i8]* %9, i64 0, i64 0
%36 = load i8*, i8** %7
%37 = load i8*, i8** %8
%38 = call i32 @cardfaces(i8* %35, i8* %36, i8* %37)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [27 x i8] c"Fail to open database %s.\0A\00"
@EXIT_FAILURE = global i32 0
define void @getcard(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca [1500 x i8]
%6 = getelementptr inbounds [1500 x i8], [1500 x i8]* %5, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = icmp slt i32 %1, 0
br i1 %10, label %20, label %15
11: ; preds = %4
%12 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %9, %15
%16 = phi i32 [ %18, %15 ], [ 0, %9 ]
%17 = call i32 @fgets(i8* %6, i32 1500, i32* %7)
%18 = add nuw i32 %16, 1
%19 = icmp eq i32 %16, %1
br i1 %19, label %20, label %15
20: ; preds = %15, %9
%21 = call i32 @fclose(i32* %7)
%22 = call i32 @cardfaces(i8* %6, i8* %2, i8* %3)
ret void
}