#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 passwd {
  char *pw_gecos;
  int /*<<< orphan*/ pw_name;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ geteuid();
scalar_t__ getpid();
struct passwd *getpwuid(int /*<<< orphan*/);
scalar_t__ getuid();
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, long, int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
char *strerror(int);
int main(int argc, char *argv[]) {
  struct passwd *pwd = 0;
  char cmd[256];
  FILE *fp;
  if ((pwd = getpwuid(geteuid())) == NULL) {
    fprintf(stderr, "Err.: %d getpwuid() - %s\n", errno, strerror(errno));
    exit(EXIT_FAILURE);
  }
  sprintf(cmd, "mailx -s 'Un messaggio dal processo %ld' %s", (long)getpid(),
          pwd->pw_name);
  if ((fp = fopen(cmd, "w")) == NULL) {
    fprintf(stderr, "Err.: %d fopen() - %s\n", errno, strerror(errno));
    exit(EXIT_FAILURE);
  }
  fprintf(fp, "Questo e' il comando %s \n", argv[0]);
  fprintf(fp, "Sto lavorando per %s\n", pwd->pw_gecos);
  if (getuid() != 0) {
    fprintf(fp, "Dovresti essere root\n");
  } else
    fprintf(fp, "Sei root\n");
  fprintf(fp, "Grazie, PID: %ld\n", (long)getpid());
  if (pclose(fp) == -1) {
    fprintf(stderr, "errore\n");
    exit(EXIT_FAILURE);
  } else
    printf("Messaggio a studente 1");
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1247 <main+0x97>
mov    0x2dec(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x138(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x138(%rbp),%rdi
mov    -0x12c(%rbp),%edx
mov    %rax,%rcx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x140(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x140(%rbp),%rdi
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%ecx
lea    0xda6(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
jne    12f7 <main+0x147>
mov    0x2d3c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x150(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x144(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x150(%rbp),%rdi
mov    -0x144(%rbp),%edx
mov    %rax,%rcx
lea    0xd66(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x128(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd53(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x128(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xd51(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
je     135a <main+0x1aa>
mov    -0x128(%rbp),%rdi
lea    0xd41(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    136f <main+0x1bf>
mov    -0x128(%rbp),%rdi
lea    0xd3d(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x128(%rbp),%rax
mov    %rax,-0x158(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x158(%rbp),%rdi
mov    %rax,%rdx
lea    0xd1a(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x128(%rbp),%rdi
call   1070 <pclose@plt>
cmp    $0xffffffff,%eax
jne    13d7 <main+0x227>
mov    0x2c28(%rip),%rax
mov    (%rax),%rdi
lea    0xcff(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c00(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xceb(%rip),%rdi
call   1060 <printf@plt>
mov    0x2bde(%rip),%rax
mov    (%rax),%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r14
xor    %eax,%eax
call   1080 <geteuid@plt>
mov    %eax,%edi
call   1040 <getpwuid@plt>
test   %rax,%rax
je     12cb <main+0x10b>
mov    %rax,%r15
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x8(%r15),%ecx
lea    0xe1e(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rax,%rdx
call   10b0 <sprintf@plt>
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12ee <main+0x12e>
mov    %rax,%rbx
mov    (%r14),%rdx
lea    0xe30(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    (%r15),%rdx
lea    0xe36(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     1267 <main+0xa7>
lea    0xe2f(%rip),%rdi
mov    $0x15,%esi
jmp    1273 <main+0xb3>
lea    0xe37(%rip),%rdi
mov    $0x9,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10d0 <fwrite@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe21(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <pclose@plt>
cmp    $0xffffffff,%eax
je     1320 <main+0x160>
lea    0xe1a(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0e(%rip),%rax
mov    (%rax),%rbx
mov    0x2cfc(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10e0 <strerror@plt>
lea    0xd14(%rip),%rsi
jmp    130f <main+0x14f>
mov    0x2ceb(%rip),%rax
mov    (%rax),%rbx
mov    0x2cd9(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10e0 <strerror@plt>
lea    0xd39(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1340 <main+0x180>
mov    0x2cb9(%rip),%rax
mov    (%rax),%rcx
lea    0xd90(%rip),%rdi
mov    $0x7,%esi
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i8*, i32 }
@stderr = global i32* null
@.str = [26 x i8] c"Err.: %d getpwuid() - %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"mailx -s 'Un messaggio dal processo %ld' %s\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [23 x i8] c"Err.: %d fopen() - %s\0A\00"
@.str.4 = [26 x i8] c"Questo e' il comando %s \0A\00"
@.str.5 = [22 x i8] c"Sto lavorando per %s\0A\00"
@.str.6 = [22 x i8] c"Dovresti essere root\0A\00"
@.str.7 = [10 x i8] c"Sei root\0A\00"
@.str.8 = [18 x i8] c"Grazie, PID: %ld\0A\00"
@.str.9 = [8 x i8] c"errore\0A\00"
@.str.10 = [23 x i8] c"Messaggio a studente 1\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca [256 x i8]
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.passwd* null, %struct.passwd** %6
%9 = call i32 (...) @geteuid()
%10 = call %struct.passwd* @getpwuid(i32 %9)
store %struct.passwd* %10, %struct.passwd** %6
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32*, i32** @stderr
%14 = load i32, i32* @errno
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %14, i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%22 = call i64 (...) @getpid()
%23 = load %struct.passwd*, %struct.passwd** %6
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %23, i32 0, i32 1
%25 = load i32, i32* %24
%26 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %22, i32 %25)
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %28, i32** %8
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %38
30: ; preds = %20
%31 = load i32*, i32** @stderr
%32 = load i32, i32* @errno
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %32, i8* %34)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %20
%39 = load i32*, i32** %8
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 0
%42 = load i8*, i8** %41
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i32*, i32** %8
%45 = load %struct.passwd*, %struct.passwd** %6
%46 = getelementptr inbounds %struct.passwd, %struct.passwd* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %47)
%49 = call i64 (...) @getuid()
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %38
%52 = load i32*, i32** %8
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
br label %57
54: ; preds = %38
%55 = load i32*, i32** %8
%56 = call i32 (i32*, i8*, ...) @fprintf(i32* %55, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
br label %57
57: ; preds = %54, %51
%58 = load i32*, i32** %8
%59 = call i64 (...) @getpid()
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %58, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i64 %59)
%61 = load i32*, i32** %8
%62 = call i32 @pclose(i32* %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %69
64: ; preds = %57
%65 = load i32*, i32** @stderr
%66 = call i32 (i32*, i8*, ...) @fprintf(i32* %65, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %57
%70 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
br label %71
71: ; preds = %69
%72 = load i32, i32* @EXIT_SUCCESS
ret i32 %72
}

LLVM IR (-O2):
%struct.passwd = type { i8*, i32 }
@stderr = global i32* null
@.str = [26 x i8] c"Err.: %d getpwuid() - %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"mailx -s 'Un messaggio dal processo %ld' %s\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [23 x i8] c"Err.: %d fopen() - %s\0A\00"
@.str.4 = [26 x i8] c"Questo e' il comando %s \0A\00"
@.str.5 = [22 x i8] c"Sto lavorando per %s\0A\00"
@.str.6 = [22 x i8] c"Dovresti essere root\0A\00"
@.str.7 = [10 x i8] c"Sei root\0A\00"
@.str.8 = [18 x i8] c"Grazie, PID: %ld\0A\00"
@.str.9 = [8 x i8] c"errore\0A\00"
@.str.10 = [23 x i8] c"Messaggio a studente 1\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @geteuid()
%6 = tail call %struct.passwd* @getpwuid(i32 %5)
%7 = icmp eq %struct.passwd* %6, null
br i1 %7, label %8, label %15
8: ; preds = %2
%9 = load i32*, i32** @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = tail call i64 (...) @getpid()
%17 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i64 %16, i32 %18)
%20 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %29
22: ; preds = %15
%23 = load i32*, i32** @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %24, i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %15
%30 = load i8*, i8** %1
%31 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %6, i64 0, i32 0
%33 = load i8*, i8** %32
%34 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %33)
%35 = tail call i64 (...) @getuid()
%36 = icmp eq i64 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %29
%38 = tail call i64 @fwrite(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i64 21, i64 1, i32* %20)
br label %41
39: ; preds = %29
%40 = tail call i64 @fwrite(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i64 9, i64 1, i32* %20)
br label %41
41: ; preds = %39, %37
%42 = tail call i64 (...) @getpid()
%43 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i64 %42)
%44 = tail call i32 @pclose(i32* %20)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i32*, i32** @stderr
%48 = tail call i64 @fwrite(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i64 7, i64 1, i32* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %41
%52 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0))
%53 = load i32, i32* @EXIT_SUCCESS
ret i32 %53
}