#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ close(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ test_crypto(int);
int main(int argc, char **argv) {
  int fd = -1;
  char *filename;
  char error_str[100];
  filename = (argv[1] == NULL) ? "/dev/crypto" : argv[1];
  fd = open(filename, O_RDWR, 0);
  if (fd < 0) {
    sprintf(error_str, "open %s", filename);
    perror(error_str);
    return 1;
  }
  if (test_crypto(fd)) {
    return 1;
  }
  if (close(fd)) {
    perror("close(fd)");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1192 <main+0x42>
lea    0xe7a(%rip),%rax
mov    %rax,-0x98(%rbp)
jmp    11a1 <main+0x51>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fc <main+0xac>
lea    -0x90(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe2d(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x90(%rbp),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xfb>
mov    -0x14(%rbp),%edi
call   1040 <test_crypto@plt>
cmp    $0x0,%rax
je     121a <main+0xca>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xfb>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     1244 <main+0xf4>
lea    0xde1(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xfb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x8(%rsi),%rax
test   %rax,%rax
lea    0xe9c(%rip),%rbx
cmovne %rax,%rbx
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11ad <main+0x5d>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <test_crypto@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    11a6 <main+0x56>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    11c7 <main+0x77>
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe58(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1070 <sprintf@plt>
mov    %rbp,%rdi
jmp    11ce <main+0x7e>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
jmp    11a6 <main+0x56>

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/crypto\00"
@O_RDWR = global i32 0
@.str.1 = [8 x i8] c"open %s\00"
@.str.2 = [10 x i8] c"close(fd)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
br label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
br label %18
18: ; preds = %14, %13
%19 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %13 ], [ %17, %14 ]
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21, i32 0)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%27 = load i8*, i8** %7
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%30 = call i32 @perror(i8* %29)
store i32 1, i32* %3
br label %43
31: ; preds = %18
%32 = load i32, i32* %6
%33 = call i64 @test_crypto(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
store i32 1, i32* %3
br label %43
36: ; preds = %31
%37 = load i32, i32* %6
%38 = call i64 @close(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %43
42: ; preds = %36
store i32 0, i32* %3
br label %43
43: ; preds = %42, %40, %35, %25
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/crypto\00"
@O_RDWR = global i32 0
@.str.1 = [8 x i8] c"open %s\00"
@.str.2 = [10 x i8] c"close(fd)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, null
%8 = select i1 %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %6
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%14 = call i32 @perror(i8* %4)
br label %23
15: ; preds = %2
%16 = tail call i64 @test_crypto(i32 %10)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = tail call i64 @close(i32 %10)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %18, %15, %21, %12
%24 = phi i32 [ 1, %12 ], [ 1, %21 ], [ 1, %15 ], [ 0, %18 ]
ret i32 %24
}