#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 ENAMETOOLONG;
int PATH_MAX;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t snprintf(char *, int, char *, char const *, char const *);
int do_touch2(const char *dir, const char *fname) {
  FILE *fp;
  size_t res;
  char path[PATH_MAX];
  res = snprintf(path, PATH_MAX, "%s/%s", dir, fname);
  if (res >= PATH_MAX)
    return -ENAMETOOLONG;
  fp = fopen(path, "w");
  if (fp == NULL)
    return -errno;
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<do_touch2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x38(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe7c(%rip),%rdx
call   1040 <snprintf@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e58(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jb     11be <do_touch2+0x8e>
xor    %eax,%eax
mov    0x2e23(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    120e <do_touch2+0xde>
mov    -0x48(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f7 <do_touch2+0xc7>
xor    %eax,%eax
mov    0x2df2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    120e <do_touch2+0xde>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_touch2>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rsi,%r8
mov    %rdi,%rcx
mov    0x2eac(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe9e(%rip),%rdx
mov    %rbx,%rdi
call   1040 <snprintf@plt>
movslq (%r14),%rcx
cmp    %rcx,%rax
jae    1192 <do_touch2+0x62>
lea    0xe8d(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     119b <do_touch2+0x6b>
mov    %rax,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11a6 <do_touch2+0x76>
mov    0x2e37(%rip),%rcx
jmp    11a2 <do_touch2+0x72>
mov    0x2e36(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@ENAMETOOLONG = global i32 0
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
define i32 @do_touch2(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = load i32, i32* @PATH_MAX
%16 = load i8*, i8** %4
%17 = load i8*, i8** %5
%18 = call i64 @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %17)
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = load i32, i32* @PATH_MAX
%21 = sext i32 %20 to i64
%22 = icmp uge i64 %19, %21
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = load i32, i32* @ENAMETOOLONG
%25 = sub nsw i32 0, %24
store i32 %25, i32* %3
store i32 1, i32* %10
br label %36
26: ; preds = %2
%27 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %6
%28 = load i32*, i32** %6
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
store i32 1, i32* %10
br label %36
33: ; preds = %26
%34 = load i32*, i32** %6
%35 = call i32 @fclose(i32* %34)
store i32 0, i32* %3
store i32 1, i32* %10
br label %36
36: ; preds = %33, %30, %23
%37 = load i8*, i8** %8
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@ENAMETOOLONG = global i32 0
@.str.1 = [2 x i8] c"w\00"
@errno = global i32 0
define i32 @do_touch2(i8* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i64 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%7 = load i32, i32* @PATH_MAX
%8 = sext i32 %7 to i64
%9 = icmp ult i64 %6, %8
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32, i32* @ENAMETOOLONG
%12 = sub nsw i32 0, %11
br label %21
13: ; preds = %2
%14 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %21
19: ; preds = %13
%20 = call i32 @fclose(i32* %14)
br label %21
21: ; preds = %19, %16, %10
%22 = phi i32 [ %12, %10 ], [ %18, %16 ], [ 0, %19 ]
ret i32 %22
}