#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 */
struct stat {
  int st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int S_IFDIR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unlink(char *);
int touch(char *f) {
  FILE *fp;
  int c, err = 0;
  struct stat sb;
  if (!stat(f, &sb) && (sb.st_mode & S_IFDIR))
    return 0;
  if (NULL != (fp = fopen(f, "r+b"))) {
    fseek(fp, 0L, 2);
    if (ftell(fp) == 0L) {
      fclose(fp);
      unlink(f);
      if (NULL == (fp = fopen(f, "w"))) {
        fprintf(stderr, "touch:  can't create %s", f);
        err = 1;
      }
      fclose(fp);
    } else {
      fseek(fp, 0L, 0);
      c = fgetc(fp);
      fseek(fp, 0L, 0);
      fputc(c, fp);
      fclose(fp);
    }
  } else {
    if (NULL == (fp = fopen(f, "w"))) {
      fprintf(stderr, "touch:  can't create %s", f);
      err = 1;
    }
    fclose(fp);
  }
  return err;
}


x86 assembly (clang, -O0):
<touch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%eax
jne    11da <touch+0x4a>
mov    -0x28(%rbp),%eax
mov    0x2e0d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11da <touch+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    1318 <touch+0x188>
mov    -0x10(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     12c7 <touch+0x137>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x2,%edx
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
cmp    $0x0,%rax
jne    1283 <touch+0xf3>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x10(%rbp),%rdi
lea    0xdc6(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1275 <touch+0xe5>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd9d(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12c2 <touch+0x132>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <fseek@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <fputc@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1312 <touch+0x182>
mov    -0x10(%rbp),%rdi
lea    0xd32(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1309 <touch+0x179>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd09(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<touch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    %rsp,%rsi
call   1090 <stat@plt>
test   %eax,%eax
je     120a <touch+0x7a>
lea    0xe53(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     122d <touch+0x9d>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2,%edx
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
je     121d <touch+0x8d>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1050 <fgetc@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <fseek@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
jmp    1265 <touch+0xd5>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
xor    %r14d,%r14d
test   %eax,(%rsp)
jne    126d <touch+0xdd>
jmp    11a6 <touch+0x16>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
lea    0xdd0(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbx
xor    %r14d,%r14d
test   %rax,%rax
jne    1265 <touch+0xd5>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%r14d
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFDIR = global i32 0
@.str = [4 x i8] c"r+b\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"touch: can't create %s\00"
define i32 @touch(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
store i8* %0, i8** %3
store i32 0, i32* %6
%8 = load i8*, i8** %3
%9 = call i32 @stat(i8* %8, %struct.stat* %7)
%10 = icmp ne i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%13 = load i32, i32* %12
%14 = load i32, i32* @S_IFDIR
%15 = and i32 %13, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 0, i32* %2
br label %69
18: ; preds = %11, %1
%19 = load i8*, i8** %3
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %4
%21 = icmp ne i32* null, %20
br i1 %21, label %22, label %56
22: ; preds = %18
%23 = load i32*, i32** %4
%24 = call i32 @fseek(i32* %23, i64 0, i32 2)
%25 = load i32*, i32** %4
%26 = call i64 @ftell(i32* %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %22
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load i8*, i8** %3
%32 = call i32 @unlink(i8* %31)
%33 = load i8*, i8** %3
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %34, i32** %4
%35 = icmp eq i32* null, %34
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %3
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %38)
store i32 1, i32* %6
br label %40
40: ; preds = %36, %28
%41 = load i32*, i32** %4
%42 = call i32 @fclose(i32* %41)
br label %55
43: ; preds = %22
%44 = load i32*, i32** %4
%45 = call i32 @fseek(i32* %44, i64 0, i32 0)
%46 = load i32*, i32** %4
%47 = call i32 @fgetc(i32* %46)
store i32 %47, i32* %5
%48 = load i32*, i32** %4
%49 = call i32 @fseek(i32* %48, i64 0, i32 0)
%50 = load i32, i32* %5
%51 = load i32*, i32** %4
%52 = call i32 @fputc(i32 %50, i32* %51)
%53 = load i32*, i32** %4
%54 = call i32 @fclose(i32* %53)
br label %55
55: ; preds = %43, %40
br label %67
56: ; preds = %18
%57 = load i8*, i8** %3
%58 = call i32* @fopen(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %58, i32** %4
%59 = icmp eq i32* null, %58
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %3
%63 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %62)
store i32 1, i32* %6
br label %64
64: ; preds = %60, %56
%65 = load i32*, i32** %4
%66 = call i32 @fclose(i32* %65)
br label %67
67: ; preds = %64, %55
%68 = load i32, i32* %6
store i32 %68, i32* %2
br label %69
69: ; preds = %67, %17
%70 = load i32, i32* %2
ret i32 %70
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFDIR = global i32 0
@.str = [4 x i8] c"r+b\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"touch: can't create %s\00"
define i32 @touch(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @S_IFDIR
%10 = and i32 %9, %8
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %45
12: ; preds = %6, %1
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %36, label %15
15: ; preds = %12
%16 = tail call i32 @fseek(i32* %13, i64 0, i32 2)
%17 = tail call i64 @ftell(i32* %13)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = tail call i32 @fclose(i32* %13)
%21 = tail call i32 @unlink(i8* %0)
%22 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %27
27: ; preds = %24, %19
%28 = phi i32 [ 1, %24 ], [ 0, %19 ]
%29 = tail call i32 @fclose(i32* %22)
br label %45
30: ; preds = %15
%31 = tail call i32 @fseek(i32* %13, i64 0, i32 0)
%32 = tail call i32 @fgetc(i32* %13)
%33 = tail call i32 @fseek(i32* %13, i64 0, i32 0)
%34 = tail call i32 @fputc(i32 %32, i32* %13)
%35 = tail call i32 @fclose(i32* %13)
br label %45
36: ; preds = %12
%37 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %42
42: ; preds = %39, %36
%43 = phi i32 [ 1, %39 ], [ 0, %36 ]
%44 = tail call i32 @fclose(i32* %37)
br label %45
45: ; preds = %42, %30, %27, %6
%46 = phi i32 [ 0, %6 ], [ %28, %27 ], [ 0, %30 ], [ %43, %42 ]
ret i32 %46
}