#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ ehdr;
struct TYPE_4__ {
  int e_entry;
};
typedef TYPE_1__ Elf32_Ehdr;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ lelf(int, TYPE_1__ *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ stderr;
int strtol(char *, int /*<<< orphan*/ *, int);
int write(int, TYPE_1__ *, int);
int main(int argc, char *argv[]) {
  int fd;
  Elf32_Ehdr ehdr;
  if (argc != 2 && argc != 3) {
    fprintf(stderr, "usage: elf-entry filename [entry]\n");
    exit(1);
  }
  if (argc == 2) {
    fd = open(argv[1], O_RDONLY);
    if (fd < 0) {
      perror("open");
      exit(1);
    }
    lelf(fd, &ehdr);
    printf("Entry point (%s): 0x%x\n", argv[1], ehdr.e_entry);
  } else {
    fd = open(argv[1], O_RDWR);
    if (fd < 0) {
      perror("open");
      exit(1);
    }
    lelf(fd, &ehdr);
    if (lseek(fd, 0, SEEK_SET) < 0) {
      perror("lseek");
      exit(1);
    }
    ehdr.e_entry = strtol(argv[2], NULL, 16);
    if (write(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
      perror("write");
      exit(1);
    }
    printf("Entry point (%s): 0x%x\n", argv[1], ehdr.e_entry);
  }
  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)
je     11d9 <main+0x49>
cmpl   $0x3,-0x8(%rbp)
je     11d9 <main+0x49>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1244 <main+0xb4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    121c <main+0x8c>
lea    0xe16(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <lelf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xdee(%rip),%rdi
call   1040 <printf@plt>
jmp    131f <main+0x18f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    127d <main+0xed>
lea    0xdb5(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <lelf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    12bc <main+0x12c>
lea    0xd93(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cltq
cmp    $0x4,%rax
je     1308 <main+0x178>
lea    0xd4d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x18(%rbp),%edx
lea    0xd0e(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ecx
and    $0xfffffffe,%ecx
cmp    $0x2,%ecx
jne    11e1 <main+0x51>
mov    %rsi,%rbx
mov    %edi,%eax
mov    0x8(%rsi),%rdi
cmp    $0x2,%eax
jne    1200 <main+0x70>
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1212 <main+0x82>
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <lelf@plt>
mov    (%rsp),%edx
mov    0x8(%rbx),%rsi
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jns    1228 <main+0x98>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <lelf@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %rax,%rax
jns    1254 <main+0xc4>
lea    0xdee(%rip),%rdi
jmp    1219 <main+0x89>
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     11c7 <main+0x37>
lea    0xdc0(%rip),%rdi
jmp    1219 <main+0x89>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"usage: elf-entry filename [entry]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [24 x i8] c"Entry point (%s): 0x%x\0A\00"
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.TYPE_4__
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10, %2
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %40
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = load i32, i32* %6
%33 = call i32 @lelf(i32 %32, %struct.TYPE_4__* %7)
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %36, i32 %38)
br label %81
40: ; preds = %17
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_RDWR
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %40
%52 = load i32, i32* %6
%53 = call i32 @lelf(i32 %52, %struct.TYPE_4__* %7)
%54 = load i32, i32* %6
%55 = load i32, i32* @SEEK_SET
%56 = call i64 @lseek(i32 %54, i32 0, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %51
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %51
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = load i8*, i8** %63
%65 = call i32 @strtol(i8* %64, i32* null, i32 16)
%66 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 0
store i32 %65, i32* %66
%67 = load i32, i32* %6
%68 = call i32 @write(i32 %67, %struct.TYPE_4__* %7, i32 4)
%69 = sext i32 %68 to i64
%70 = icmp ne i64 %69, 4
br i1 %70, label %71, label %74
71: ; preds = %61
%72 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %61
%75 = load i8**, i8*** %5
%76 = getelementptr inbounds i8*, i8** %75, i64 1
%77 = load i8*, i8** %76
%78 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %7, i32 0, i32 0
%79 = load i32, i32* %78
%80 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %77, i32 %79)
br label %81
81: ; preds = %74, %31
%82 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32 }
@stderr = global i32 0
@.str = [35 x i8] c"usage: elf-entry filename [entry]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [24 x i8] c"Entry point (%s): 0x%x\0A\00"
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.TYPE_4__
%4 = bitcast %struct.TYPE_4__* %3 to i8*
%5 = and i32 %0, -2
%6 = icmp eq i32 %5, 2
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = icmp eq i32 %0, 2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
br i1 %12, label %15, label %26
15: ; preds = %11
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* %14, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = call i32 @lelf(i32 %17, %struct.TYPE_4__* %3)
%24 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i64 0, i32 0
%25 = load i32, i32* %24
br label %53
26: ; preds = %11
%27 = load i32, i32* @O_RDWR
%28 = tail call i32 @open(i8* %14, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = call i32 @lelf(i32 %28, %struct.TYPE_4__* %3)
%35 = load i32, i32* @SEEK_SET
%36 = call i64 @lseek(i32 %28, i32 0, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %33
%42 = getelementptr inbounds i8*, i8** %1, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @strtol(i8* nocapture %43, i32* null, i32 16)
%45 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %3, i64 0, i32 0
store i32 %44, i32* %45
%46 = call i32 @write(i32 %28, %struct.TYPE_4__* %3, i32 4)
%47 = icmp eq i32 %46, 4
br i1 %47, label %51, label %48
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %41
%52 = load i32, i32* %45
br label %53
53: ; preds = %51, %22
%54 = phi i32 [ %52, %51 ], [ %25, %22 ]
%55 = load i8*, i8** %13
%56 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %55, i32 %54)
%57 = call i32 @exit(i32 0)
unreachable
}