#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ Perror(char *);
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int close(int);
int open(char const *, int, int);
int write(int, char *, int);
void gen_tmp_file(const char *filename) {
  int fd;
  char ch = 'a';
  if ((fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
                 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) == -1) {
    Perror("open error");
  }
  for (int i = 0; i < 26; i++) {
    ch = ch + i;
    if (write(fd, &ch, 1) != 1) {
      Perror("write error");
    }
  }
  if (close(fd) == -1) {
    Perror("close error");
  }
}


x86 assembly (clang, -O0):
<gen_tmp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movb   $0x61,-0xd(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e94(%rip),%rax
or     (%rax),%esi
mov    0x2e83(%rip),%rax
or     (%rax),%esi
mov    0x2e42(%rip),%rax
mov    (%rax),%edx
mov    0x2e59(%rip),%rax
or     (%rax),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
mov    0x2e2f(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11b0 <gen_tmp_file+0x70>
lea    0xe55(%rip),%rdi
call   1060 <Perror@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x1a,-0x14(%rbp)
jge    1204 <gen_tmp_file+0xc4>
movsbl -0xd(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %al,-0xd(%rbp)
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11f1 <gen_tmp_file+0xb1>
lea    0xe1f(%rip),%rdi
call   1060 <Perror@plt>
jmp    11f6 <gen_tmp_file+0xb6>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b7 <gen_tmp_file+0x77>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1221 <gen_tmp_file+0xe1>
lea    0xdfb(%rip),%rdi
call   1060 <Perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_tmp_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e55(%rip),%rax
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e8b(%rip),%rax
or     (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    0x2e63(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e48(%rip),%rax
or     (%rax),%edx
mov    0x2e37(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11a4 <gen_tmp_file+0x64>
lea    0xe61(%rip),%rdi
call   1060 <Perror@plt>
xor    %ebx,%ebx
mov    $0x61,%bpl
lea    0xf(%rsp),%r14
lea    0xe56(%rip),%r15
jmp    11c8 <gen_tmp_file+0x88>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
cmp    $0x1a,%ebx
je     11ee <gen_tmp_file+0xae>
add    %ebx,%ebp
mov    %bpl,0xf(%rsp)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11c0 <gen_tmp_file+0x80>
mov    %r15,%rdi
call   1060 <Perror@plt>
jmp    11c0 <gen_tmp_file+0x80>
mov    %r12d,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%eax
je     1208 <gen_tmp_file+0xc8>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <Perror@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [11 x i8] c"open error\00"
@.str.1 = [12 x i8] c"write error\00"
@.str.2 = [12 x i8] c"close error\00"
define void @gen_tmp_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
store i8* %0, i8** %2
store i8 97, i8* %4
%6 = load i8*, i8** %2
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRUSR
%13 = load i32, i32* @S_IWUSR
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IRGRP
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IROTH
%18 = or i32 %16, %17
%19 = call i32 @open(i8* %6, i32 %11, i32 %18)
store i32 %19, i32* %3
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = call i32 @Perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %1
store i32 0, i32* %5
br label %24
24: ; preds = %39, %23
%25 = load i32, i32* %5
%26 = icmp slt i32 %25, 26
br i1 %26, label %27, label %42
27: ; preds = %24
%28 = load i8, i8* %4
%29 = sext i8 %28 to i32
%30 = load i32, i32* %5
%31 = add nsw i32 %29, %30
%32 = trunc i32 %31 to i8
store i8 %32, i8* %4
%33 = load i32, i32* %3
%34 = call i32 @write(i32 %33, i8* %4, i32 1)
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = call i32 @Perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %27
br label %39
39: ; preds = %38
%40 = load i32, i32* %5
%41 = add nsw i32 %40, 1
store i32 %41, i32* %5
br label %24
42: ; preds = %24
%43 = load i32, i32* %3
%44 = call i32 @close(i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @Perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [11 x i8] c"open error\00"
@.str.1 = [12 x i8] c"write error\00"
@.str.2 = [12 x i8] c"close error\00"
define void @gen_tmp_file(i8* nocapture %0) {
%2 = alloca i8
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = tail call i32 @open(i8* %0, i32 %7, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %18, label %17
17: ; preds = %18, %1
br label %23
18: ; preds = %1
%19 = tail call i32 @Perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %17
20: ; preds = %32
%21 = tail call i32 @close(i32 %15)
%22 = icmp eq i32 %21, -1
br i1 %22, label %35, label %37
23: ; preds = %17, %32
%24 = phi i8 [ %27, %32 ], [ 97, %17 ]
%25 = phi i32 [ %33, %32 ], [ 0, %17 ]
%26 = trunc i32 %25 to i8
%27 = add i8 %24, %26
store i8 %27, i8* %2
%28 = call i32 @write(i32 %15, i8* %2, i32 1)
%29 = icmp eq i32 %28, 1
br i1 %29, label %32, label %30
30: ; preds = %23
%31 = tail call i32 @Perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %23, %30
%33 = add nuw nsw i32 %25, 1
%34 = icmp eq i32 %33, 26
br i1 %34, label %20, label %23
35: ; preds = %20
%36 = tail call i32 @Perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %20
ret void
}