#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*/ name;
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int close(int);
int creat(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, int);
int create_file(const char *dir, int n, mode_t perm) {
  int ret;
  char name[32];
  snprintf(name, sizeof(name), "%s/file%d", dir, n);
  ret = creat(name, perm);
  if (ret < 0) {
    perror("creat");
    return ret;
  }
  ret = close(ret);
  if (ret < 0) {
    perror("close");
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    $0x20,%esi
lea    0xe96(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <creat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119f <create_file+0x5f>
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <create_file+0x92>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cb <create_file+0x8b>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d2 <create_file+0x92>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_file>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ebx
mov    %esi,%r8d
mov    %rdi,%rcx
lea    0xeaa(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1060 <creat@plt>
mov    %eax,%ebx
test   %eax,%eax
js     118d <create_file+0x4d>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
test   %eax,%eax
js     1196 <create_file+0x56>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe76(%rip),%rdi
jmp    119f <create_file+0x5f>
lea    0xe73(%rip),%rdi
mov    %eax,%ebx
call   1050 <perror@plt>
jmp    1183 <create_file+0x43>

LLVM IR (-O0):
@.str = [10 x i8] c"%s/file%d\00"
@.str.1 = [6 x i8] c"creat\00"
@.str.2 = [6 x i8] c"close\00"
define i32 @create_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = call i32 @snprintf(i8* %10, i32 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12)
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%15 = load i32, i32* %7
%16 = call i32 @creat(i8* %14, i32 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* %8
store i32 %21, i32* %4
br label %31
22: ; preds = %3
%23 = load i32, i32* %8
%24 = call i32 @close(i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %31
30: ; preds = %22
store i32 0, i32* %4
br label %31
31: ; preds = %30, %27, %19
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s/file%d\00"
@.str.1 = [6 x i8] c"creat\00"
@.str.2 = [6 x i8] c"close\00"
define i32 @create_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 32, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%7 = call i32 @creat(i8* %5, i32 %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = call i32 @close(i32 %7)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9, %3
%13 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %3 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), %9 ]
%14 = phi i32 [ %7, %3 ], [ %10, %9 ]
%15 = call i32 @perror(i8* %13)
br label %16
16: ; preds = %12, %9
%17 = phi i32 [ 0, %9 ], [ %14, %12 ]
ret i32 %17
}