#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int syscall1(int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, err;
  if (argc < 2) {
    printf("touch: needs filename argument\n");
    exit(1);
  }
  fd = open(argv[1], 0x0002);
  if (fd != -1) {
    char data[2];
    if (read(fd, (char *)&data, 1) == 1) {
      lseek(fd, 0, 0);
      write(fd, (char *)&data, 1);
    };
    close(fd);
    exit(0);
  }
  err = (syscall1(501));
  if (err == 13) {
    printf("touch: %s: access denied\n", argv[1]);
    exit(1);
  }
  fd = open(argv[1], 0x0001 | 0x0200);
  if (fd == -1) {
    err = (syscall1(501));
    printf("touch: %s: cannot create file, ", argv[1]);
    switch (err) {
    case 2:
      printf("no such directory\n");
      break;
    case 13:
      printf("access denied\n");
      break;
    default:
      printf("unknown error\n");
      break;
    }
    exit(1);
  }
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x38>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x2,%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     121d <main+0x9d>
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    120e <main+0x8e>
mov    -0x14(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0x1a(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x1f5,%edi
call   1080 <syscall1@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xd,-0x18(%rbp)
jne    1254 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x201,%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12f9 <main+0x179>
mov    $0x1f5,%edi
call   1080 <syscall1@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdab(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
sub    $0x2,%eax
je     12bb <main+0x13b>
jmp    12aa <main+0x12a>
mov    -0x20(%rbp),%eax
sub    $0xd,%eax
je     12ce <main+0x14e>
jmp    12e1 <main+0x161>
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ef <main+0x16f>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12ef <main+0x16f>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     11ae <main+0x1e>
lea    0xec9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    $0x2,%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    11ef <main+0x5f>
mov    $0x1f5,%edi
call   1090 <syscall1@plt>
mov    0x8(%rbx),%rsi
cmp    $0xd,%eax
jne    1227 <main+0x97>
lea    0xe22(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%ebp
lea    0x6(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x1,%eax
jne    1223 <main+0x93>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <lseek@plt>
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
jmp    1281 <main+0xf1>
mov    %rsi,%rdi
mov    $0x201,%esi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xef>
mov    $0x1f5,%edi
call   1090 <syscall1@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
lea    0xdca(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0xd,%ebp
lea    0xdd9(%rip),%rax
lea    0xdf2(%rip),%rcx
cmove  %rax,%rcx
cmp    $0x2,%ebp
lea    0xdd2(%rip),%rdi
cmovne %rcx,%rdi
jmp    119f <main+0xf>
mov    %eax,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"touch: needs filename argument\0A\00"
@.str.1 = [26 x i8] c"touch: %s: access denied\0A\00"
@.str.2 = [32 x i8] c"touch: %s: cannot create file, \00"
@.str.3 = [19 x i8] c"no such directory\0A\00"
@.str.4 = [15 x i8] c"access denied\0A\00"
@.str.5 = [15 x i8] c"unknown error\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @open(i8* %17, i32 2)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %36
21: ; preds = %14
%22 = load i32, i32* %6
%23 = bitcast [2 x i8]* %8 to i8*
%24 = call i32 @read(i32 %22, i8* %23, i32 1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* %6
%28 = call i32 @lseek(i32 %27, i32 0, i32 0)
%29 = load i32, i32* %6
%30 = bitcast [2 x i8]* %8 to i8*
%31 = call i32 @write(i32 %29, i8* %30, i32 1)
br label %32
32: ; preds = %26, %21
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %14
%37 = call i32 @syscall1(i32 501)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = icmp eq i32 %38, 13
br i1 %39, label %40, label %46
40: ; preds = %36
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %36
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load i8*, i8** %48
%50 = call i32 @open(i8* %49, i32 513)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %68
53: ; preds = %46
%54 = call i32 @syscall1(i32 501)
store i32 %54, i32* %7
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %57)
%59 = load i32, i32* %7
switch i32 %59, label %64 [
i32 2, label %60
i32 13, label %62
]
60: ; preds = %53
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %66
62: ; preds = %53
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %66
64: ; preds = %53
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %64, %62, %60
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %46
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [26 x i8] c"touch: %s: access denied\0A\00"
@.str.2 = [32 x i8] c"touch: %s: cannot create file, \00"
@str = [14 x i8] c"access denied\00"
@str.6 = [18 x i8] c"no such directory\00"
@str.7 = [14 x i8] c"unknown error\00"
@str.8 = [31 x i8] c"touch: needs filename argument\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.8, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @open(i8* %11, i32 2)
%13 = icmp eq i32 %12, -1
br i1 %13, label %23, label %14
14: ; preds = %9
%15 = call i32 @read(i32 %12, i8* %4, i32 1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = tail call i32 @lseek(i32 %12, i32 0, i32 0)
%19 = call i32 @write(i32 %12, i8* %4, i32 1)
br label %20
20: ; preds = %17, %14
%21 = tail call i32 @close(i32 %12)
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %9
%24 = tail call i32 @syscall1(i32 501)
%25 = icmp eq i32 %24, 13
%26 = load i8*, i8** %10
br i1 %25, label %27, label %30
27: ; preds = %23
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = tail call i32 @open(i8* %26, i32 513)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %43
33: ; preds = %30
%34 = tail call i32 @syscall1(i32 501)
%35 = load i8*, i8** %10
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = icmp eq i32 %34, 13
%38 = select i1 %37, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0)
%39 = icmp eq i32 %34, 2
%40 = select i1 %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @str.6, i64 0, i64 0), i8* %38
%41 = tail call i32 @puts(i8* dereferenceable(1) %40)
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %30
%44 = tail call i32 @close(i32 %31)
%45 = tail call i32 @exit(i32 0)
unreachable
}