#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
scalar_t__ S_ISUID;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ read(int, char *, int);
int stat(char *, struct stat *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sfd, dfd;
  struct stat statbuf;
  char buf[1024];
  int n;
  if (stat(argv[0], &statbuf) == -1) {
    perror("fail to stat");
    exit(1);
  }
  if (statbuf.st_mode & S_ISUID != 0)
    printf("set user id\n");
  else {
    printf("can’t write to root.txt\n");
    exit(1);
  }
  if ((sfd = open("test.txt", O_RDONLY)) == -1) {
    perror("fail to open");
    exit(1);
  }
  if ((dfd = open("root.txt", O_WRONLY)) == -1) {
    perror("fail to open");
    exit(1);
  }
  while (n = read(sfd, buf, sizeof(char)) > 0) {
    if (write(dfd, buf, sizeof(char)) == -1) {
      perror("fail to write");
      exit(1);
    }
  }
  if (n < 0) {
    perror("fail to read");
    exit(1);
  }
  printf("done\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b8 <main+0x48>
lea    0xe57(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%eax
mov    0x2e1e(%rip),%rcx
cmpq   $0x0,(%rcx)
setne  %cl
and    $0x1,%cl
movzbl %cl,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11eb <main+0x7b>
lea    0xe2c(%rip),%rdi
call   1040 <printf@plt>
jmp    1201 <main+0x91>
lea    0xe28(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
lea    0xe24(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1238 <main+0xc8>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
lea    0xe03(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    126f <main+0xff>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1274 <main+0x104>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x424(%rbp)
test   $0x1,%al
jne    12aa <main+0x13a>
jmp    12e2 <main+0x172>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    12dd <main+0x16d>
lea    0xd86(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1274 <main+0x104>
cmpl   $0x0,-0x424(%rbp)
jge    1305 <main+0x195>
lea    0xd6c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd63(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    (%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     1262 <main+0xf2>
mov    0x2e48(%rip),%rax
xor    %ecx,%ecx
cmpq   $0x0,(%rax)
setne  %cl
test   %ecx,0x8(%rsp)
je     126b <main+0xfb>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0xe9>
mov    %eax,%r14d
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
lea    0xe5a(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1259 <main+0xe9>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    123f <main+0xcf>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1200 <main+0x90>
lea    0xdf3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb6(%rip),%rdi
jmp    1230 <main+0xc0>
lea    0xd97(%rip),%rdi
jmp    1230 <main+0xc0>
lea    0xdbf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [13 x i8] c"fail to stat\00"
@S_ISUID = global i64 0
@.str.1 = [13 x i8] c"set user id\0A\00"
@.str.2 = [27 x i8] c"can\E2\80\99t write to root.txt\0A\00"
@.str.3 = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.4 = [13 x i8] c"fail to open\00"
@.str.5 = [9 x i8] c"root.txt\00"
@O_WRONLY = global i32 0
@.str.6 = [14 x i8] c"fail to write\00"
@.str.7 = [13 x i8] c"fail to read\00"
@.str.8 = [6 x i8] c"done\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 %struct.stat
%9 = alloca [1024 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @stat(i8* %13, %struct.stat* %8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i64, i64* @S_ISUID
%23 = icmp ne i64 %22, 0
%24 = zext i1 %23 to i32
%25 = and i32 %21, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %32
29: ; preds = %19
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %27
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %33)
store i32 %34, i32* %6
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = load i32, i32* @O_WRONLY
%41 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %40)
store i32 %41, i32* %7
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
br label %47
47: ; preds = %61, %46
%48 = load i32, i32* %6
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%50 = call i64 @read(i32 %48, i8* %49, i32 1)
%51 = icmp sgt i64 %50, 0
%52 = zext i1 %51 to i32
store i32 %52, i32* %10
br i1 %51, label %53, label %62
53: ; preds = %47
%54 = load i32, i32* %7
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%56 = call i32 @write(i32 %54, i8* %55, i32 1)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %53
br label %47
62: ; preds = %47
%63 = load i32, i32* %10
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %62
%69 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [13 x i8] c"fail to stat\00"
@S_ISUID = global i64 0
@.str.3 = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.4 = [13 x i8] c"fail to open\00"
@.str.5 = [9 x i8] c"root.txt\00"
@O_WRONLY = global i32 0
@.str.6 = [14 x i8] c"fail to write\00"
@str = [26 x i8] c"can\E2\80\99t write to root.txt\00"
@str.9 = [12 x i8] c"set user id\00"
@str.10 = [5 x i8] c"done\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = load i8*, i8** %1
%8 = call i32 @stat(i8* %7, %struct.stat* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i64, i64* @S_ISUID
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
%19 = and i32 %15, %18
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %13
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.9, i64 0, i64 0))
%23 = load i32, i32* @O_RDONLY
%24 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %29, label %32
26: ; preds = %13
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i32, i32* @O_WRONLY
%34 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %32, %42
%40 = call i64 @read(i32 %24, i8* %6, i32 1)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = call i32 @write(i32 %34, i8* %6, i32 1)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %39
45: ; preds = %42
%46 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %39
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.10, i64 0, i64 0))
ret i32 0
}