#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char const *, char *);
int strlen(char const *);
int /*<<< orphan*/ unlink(char const *);
FILE *writefile(const char *name, int *clptr) {
  int len = strlen(name);
  char *tmp;
  FILE *res;
  if (len >= 3 && !strcmp(name + len - 3, ".gz")) {
    tmp = malloc(len + 20);
    unlink(name);
    sprintf(tmp, "gzip -c > %s", name);
    res = popen(tmp, "w");
    if (res)
      *clptr = 2;
    free(tmp);
  } else {
    res = fopen(name, "w");
    if (res)
      *clptr = 1;
  }
  if (!res)
    fprintf(stderr, "*** lingeling error: can not write %s\n", name);
  return res;
}


x86 assembly (clang, -O0):
<writefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jl     123d <writefile+0xad>
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe34(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    123d <writefile+0xad>
mov    -0x14(%rbp),%edi
add    $0x14,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe03(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     122f <writefile+0x9f>
mov    -0x10(%rbp),%rax
movl   $0x2,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    126b <writefile+0xdb>
mov    -0x8(%rbp),%rdi
lea    0xdc9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1266 <writefile+0xd6>
mov    -0x10(%rbp),%rax
movl   $0x1,(%rax)
jmp    126b <writefile+0xdb>
cmpq   $0x0,-0x28(%rbp)
jne    128f <writefile+0xff>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd89(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writefile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
cmp    $0x3,%eax
jl     11c4 <writefile+0x34>
mov    %eax,%ebx
mov    %eax,%eax
lea    (%r12,%rax,1),%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xe45(%rip),%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11e4 <writefile+0x54>
lea    0xe46(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     123b <writefile+0xab>
mov    %rax,%rbx
movl   $0x1,(%r14)
jmp    1255 <writefile+0xc5>
add    $0x14,%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1040 <unlink@plt>
lea    0xe04(%rip),%rsi
mov    %r15,%rdi
mov    %r12,%rdx
call   10b0 <sprintf@plt>
lea    0xdff(%rip),%rsi
mov    %r15,%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     1233 <writefile+0xa3>
mov    %rax,%rbx
movl   $0x2,(%r14)
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1255 <writefile+0xc5>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
mov    %r12,%rdx
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c".gz\00"
@.str.1 = [13 x i8] c"gzip -c > %s\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [39 x i8] c"*** lingeling error: can not write %s\0A\00"
define i32* @writefile(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
store i8* %0, i8** %3
store i32* %1, i32** %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp sge i32 %10, 3
br i1 %11, label %12, label %38
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = getelementptr inbounds i8, i8* %16, i64 -3
%18 = call i32 @strcmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = icmp ne i32 %18, 0
br i1 %19, label %38, label %20
20: ; preds = %12
%21 = load i32, i32* %5
%22 = add nsw i32 %21, 20
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %3
%25 = call i32 @unlink(i8* %24)
%26 = load i8*, i8** %6
%27 = load i8*, i8** %3
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i8*, i8** %6
%30 = call i32* @popen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %7
%31 = load i32*, i32** %7
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = load i32*, i32** %4
store i32 2, i32* %34
br label %35
35: ; preds = %33, %20
%36 = load i8*, i8** %6
%37 = call i32 @free(i8* %36)
br label %46
38: ; preds = %12, %2
%39 = load i8*, i8** %3
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %40, i32** %7
%41 = load i32*, i32** %7
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = load i32*, i32** %4
store i32 1, i32* %44
br label %45
45: ; preds = %43, %38
br label %46
46: ; preds = %45, %35
%47 = load i32*, i32** %7
%48 = icmp ne i32* %47, null
br i1 %48, label %53, label %49
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %3
%52 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %49, %46
%54 = load i32*, i32** %7
ret i32* %54
}

LLVM IR (-O2):
@.str = [4 x i8] c".gz\00"
@.str.1 = [13 x i8] c"gzip -c > %s\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [39 x i8] c"*** lingeling error: can not write %s\0A\00"
define i32* @writefile(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = icmp sgt i32 %3, 2
br i1 %4, label %5, label %20
5: ; preds = %2
%6 = zext i32 %3 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 -3
%9 = tail call i32 @strcmp(i8* %8, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %5
%12 = add nuw nsw i32 %3, 20
%13 = tail call i8* @malloc(i32 %12)
%14 = tail call i32 @unlink(i8* %0)
%15 = tail call i32 @sprintf(i8* dereferenceable(1) %13, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%16 = tail call i32* @popen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %24
18: ; preds = %11
%19 = tail call i32 @free(i8* %13)
br label %26
20: ; preds = %5, %2
%21 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %26, label %23
23: ; preds = %20
store i32 1, i32* %1
br label %29
24: ; preds = %11
store i32 2, i32* %1
%25 = tail call i32 @free(i8* %13)
br label %29
26: ; preds = %20, %18
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %24, %23, %26
%30 = phi i32* [ null, %26 ], [ %16, %24 ], [ %21, %23 ]
ret i32* %30
}