#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*/ cmd;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EEXIST;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int file_exists(char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
char *strdup(char *);
int strlen(char *);
int generate_keyfile(char *keyfile) {
  FILE *fp;
  char *keyfn = NULL;
  char cmd[2048] = {0};
  if (file_exists(keyfile))
    return -EEXIST;
  keyfn = strdup(keyfile);
  if (memcmp(keyfn + (strlen(keyfn) - 4), ".pub", 4) == 0)
    keyfn[strlen(keyfn) - 4] = 0;
  snprintf(cmd, sizeof(cmd), "ssh-keygen -t rsa -f %s", keyfn);
  fp = popen(cmd, "w");
  fputc(13, fp);
  fclose(fp);
  free(keyfn);
  return file_exists(keyfile);
}


x86 assembly (clang, -O0):
<generate_keyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1080 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1040 <file_exists@plt>
cmp    $0x0,%eax
je     11ef <generate_keyfile+0x4f>
xor    %eax,%eax
mov    0x2dfb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12c0 <generate_keyfile+0x120>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x828(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x828(%rbp),%rdi
sub    $0x4,%eax
cltq
add    %rax,%rdi
lea    0xdda(%rip),%rsi
mov    $0x4,%edx
call   10a0 <memcmp@plt>
cmp    $0x0,%rax
jne    1261 <generate_keyfile+0xc1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x830(%rbp),%rax
sub    $0x4,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x820(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x800,%esi
lea    0xd8d(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   10b0 <popen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0xd,%edi
call   1090 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <file_exists@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_keyfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %ebx,%ebx
mov    $0x800,%edx
xor    %esi,%esi
call   1080 <memset@plt>
mov    %r14,%rdi
call   1040 <file_exists@plt>
test   %eax,%eax
je     11dc <generate_keyfile+0x3c>
mov    0x2e0b(%rip),%rax
sub    (%rax),%ebx
jmp    1272 <generate_keyfile+0xd2>
mov    %r14,%rdi
call   10c0 <strdup@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rax,%r12,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe00(%rip),%rsi
mov    $0x4,%edx
call   10a0 <memcmp@plt>
test   %rax,%rax
jne    121f <generate_keyfile+0x7f>
mov    %r12,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x4(%rax,%r12,1)
lea    0xddf(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x800,%esi
mov    %r12,%rcx
call   1070 <snprintf@plt>
lea    0xddd(%rip),%rsi
mov    %r15,%rdi
call   10b0 <popen@plt>
mov    %rax,%rbx
mov    $0xd,%edi
mov    %rax,%rsi
call   1090 <fputc@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <file_exists@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EEXIST = global i32 0
@.str = [5 x i8] c".pub\00"
@.str.1 = [24 x i8] c"ssh-keygen -t rsa -f %s\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @generate_keyfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [2048 x i8]
store i8* %0, i8** %3
store i8* null, i8** %5
%7 = bitcast [2048 x i8]* %6 to i8*
%8 = load i8*, i8** %3
%9 = call i32 @file_exists(i8* %8)
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @EEXIST
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %46
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = call i8* @strdup(i8* %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = load i8*, i8** %5
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %17, i64 %21
%23 = call i64 @memcmp(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %14
%26 = load i8*, i8** %5
%27 = load i8*, i8** %5
%28 = call i32 @strlen(i8* %27)
%29 = sub nsw i32 %28, 4
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %25, %14
%33 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%34 = load i8*, i8** %5
%35 = call i32 @snprintf(i8* %33, i32 2048, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%37 = call i32* @popen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %37, i32** %4
%38 = load i32*, i32** %4
%39 = call i32 @fputc(i32 13, i32* %38)
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
%44 = load i8*, i8** %3
%45 = call i32 @file_exists(i8* %44)
store i32 %45, i32* %2
br label %46
46: ; preds = %32, %11
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@EEXIST = global i32 0
@.str = [5 x i8] c".pub\00"
@.str.1 = [24 x i8] c"ssh-keygen -t rsa -f %s\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @generate_keyfile(i8* %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @file_exists(i8* %0)
%5 = icmp eq i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i32, i32* @EEXIST
%8 = sub nsw i32 0, %7
br label %29
9: ; preds = %1
%10 = tail call i8* @strdup(i8* %0)
%11 = tail call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, -4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
%15 = tail call i64 @memcmp(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %9
%18 = tail call i32 @strlen(i8* %10)
%19 = add nsw i32 %18, -4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %10, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %17, %9
%23 = call i32 @snprintf(i8* %3, i32 2048, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%24 = call i32* @popen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @fputc(i32 13, i32* %24)
%26 = call i32 @fclose(i32* %24)
%27 = call i32 @free(i8* %10)
%28 = call i32 @file_exists(i8* %0)
br label %29
29: ; preds = %22, %6
%30 = phi i32 [ %8, %6 ], [ %28, %22 ]
ret i32 %30
}